|
1 | | -[](https://npmjs.com/package/mdast-normalize-headings) |
| 1 | +# mdast-normalize-headings [![Build Status][travis-badge]][travis] [![Coverage Status][codecov-badge]][codecov] |
2 | 2 |
|
3 | | -# mdast-normalize-headings |
| 3 | +Providing multiple top-level headings per single markdown document is confusing |
| 4 | +for tools that assume that there is only a single top-level heading that |
| 5 | +contains some meta-information (usually title) about the document. |
4 | 6 |
|
5 | | -[![Build Status][travis-badge]][travis] [![Dependency Status][david-badge]][david] |
| 7 | +This [**mdast**][mdast] utility makes sure that there is only one top-level |
| 8 | +heading in the document by adjusting headings depths accordingly. |
6 | 9 |
|
7 | | -Providing multiple top-level headings per single Markdown document is confusing for tools that assume that there is only a single top-level heading that contains some meta-information (usually title) about the document. |
| 10 | +Originally extracted from [`remark-man`][man]. |
8 | 11 |
|
9 | | -This [mdast][] transformer makes sure that there is only one top-level heading in the document by adjusting headings depths accordingly. |
| 12 | +## Installation |
10 | 13 |
|
11 | | -Originally extracted from [remark-man][]. |
| 14 | +[npm][]: |
12 | 15 |
|
13 | | -[mdast]: https://github.com/syntax-tree/mdast |
14 | | -[remark]: https://github.com/wooorm/remark |
15 | | -[remark-man]: https://github.com/wooorm/remark-man |
16 | | -[remark-normalize-headings]: https://github.com/eush77/remark-normalize-headings |
| 16 | +```bash |
| 17 | +npm install mdast-normalize-headings |
| 18 | +``` |
| 19 | + |
| 20 | +## Usage |
| 21 | + |
| 22 | +```js |
| 23 | +var u = require('unist-builder') |
| 24 | +var normalizeHeadings = require('mdast-normalize-headings') |
| 25 | + |
| 26 | +var tree = u('root', [ |
| 27 | + u('heading', {depth: 1}, [u('text', 'title')]), |
| 28 | + u('heading', {depth: 2}, [u('text', 'description')]), |
| 29 | + u('heading', {depth: 1}, [u('text', 'example')]) |
| 30 | +]) |
17 | 31 |
|
18 | | -[travis]: https://travis-ci.org/eush77/mdast-normalize-headings |
19 | | -[travis-badge]: https://travis-ci.org/eush77/mdast-normalize-headings.svg |
20 | | -[david]: https://david-dm.org/eush77/mdast-normalize-headings |
21 | | -[david-badge]: https://david-dm.org/eush77/mdast-normalize-headings.png |
| 32 | +console.log(tree) |
22 | 33 |
|
23 | | -## Example |
| 34 | +normalizeHeadings(tree) |
| 35 | + |
| 36 | +console.log(tree) |
| 37 | +``` |
| 38 | + |
| 39 | +Yields: |
24 | 40 |
|
25 | 41 | ```js |
26 | | -var normalizeHeadings = require('mdast-normalize-headings'); |
27 | | - |
28 | | -ast |
29 | | -//=> { |
30 | | -// "type": "root", |
31 | | -// "children": [ |
32 | | -// { |
33 | | -// "type": "heading", |
34 | | -// "depth": 1, |
35 | | -// "children": [ |
36 | | -// { |
37 | | -// "type": "text", |
38 | | -// "value": "title" |
39 | | -// } |
40 | | -// ] |
41 | | -// }, |
42 | | -// { |
43 | | -// "type": "heading", |
44 | | -// "depth": 2, |
45 | | -// "children": [ |
46 | | -// { |
47 | | -// "type": "text", |
48 | | -// "value": "description" |
49 | | -// } |
50 | | -// ] |
51 | | -// }, |
52 | | -// { |
53 | | -// "type": "heading", |
54 | | -// "depth": 1, |
55 | | -// "children": [ |
56 | | -// { |
57 | | -// "type": "text", |
58 | | -// "value": "example" |
59 | | -// } |
60 | | -// ] |
61 | | -// } |
62 | | -// ] |
63 | | -// } |
64 | | - |
65 | | -normalizeHeadings(ast) |
66 | | -//=> { |
67 | | -// "type": "root", |
68 | | -// "children": [ |
69 | | -// { |
70 | | -// "type": "heading", |
71 | | -// "depth": 1, |
72 | | -// "children": [ |
73 | | -// { |
74 | | -// "type": "text", |
75 | | -// "value": "title" |
76 | | -// } |
77 | | -// ] |
78 | | -// }, |
79 | | -// { |
80 | | -// "type": "heading", |
81 | | -// "depth": 3, |
82 | | -// "children": [ |
83 | | -// { |
84 | | -// "type": "text", |
85 | | -// "value": "description" |
86 | | -// } |
87 | | -// ] |
88 | | -// }, |
89 | | -// { |
90 | | -// "type": "heading", |
91 | | -// "depth": 2, |
92 | | -// "children": [ |
93 | | -// { |
94 | | -// "type": "text", |
95 | | -// "value": "example" |
96 | | -// } |
97 | | -// ] |
98 | | -// } |
99 | | -// ] |
100 | | -// } |
| 42 | +{ type: 'root', |
| 43 | + children: |
| 44 | + [ { type: 'heading', depth: 1, children: [Array] }, |
| 45 | + { type: 'heading', depth: 2, children: [Array] }, |
| 46 | + { type: 'heading', depth: 1, children: [Array] } ] } |
| 47 | +{ type: 'root', |
| 48 | + children: |
| 49 | + [ { type: 'heading', depth: 1, children: [Array] }, |
| 50 | + { type: 'heading', depth: 3, children: [Array] }, |
| 51 | + { type: 'heading', depth: 2, children: [Array] } ] } |
101 | 52 | ``` |
102 | 53 |
|
103 | 54 | ## API |
104 | 55 |
|
105 | | -#### `normalizeHeadings(ast)` |
| 56 | +### `normalizeHeadings(tree)` |
106 | 57 |
|
107 | | -Modifies AST in-place. Returns `ast`. |
| 58 | +Modifies tree in-place. Returns `tree`. |
108 | 59 |
|
109 | 60 | ## Related |
110 | 61 |
|
111 | | -- [remark-normalize-headings][] — [remark][] plugin wrapper. |
| 62 | +* [`remark-normalize-headings`][normalize-headings] |
| 63 | + — [**remark**][remark] plugin wrapper |
112 | 64 |
|
113 | | -## Install |
| 65 | +## Contribute |
114 | 66 |
|
115 | | -``` |
116 | | -npm install mdast-normalize-headings |
117 | | -``` |
| 67 | +See [`contributing.md` in `syntax-tree/unist`][contributing] for ways to get |
| 68 | +started. |
| 69 | + |
| 70 | +This organisation has a [Code of Conduct][coc]. By interacting with this |
| 71 | +repository, organisation, or community you agree to abide by its terms. |
118 | 72 |
|
119 | 73 | ## License |
120 | 74 |
|
121 | | -MIT |
| 75 | +[MIT][license] © Eugene Sharygin |
| 76 | + |
| 77 | +<!-- Definitions --> |
| 78 | + |
| 79 | +[travis-badge]: https://img.shields.io/travis/syntax-tree/mdast-normalize-headings.svg |
| 80 | + |
| 81 | +[travis]: https://travis-ci.org/syntax-tree/mdast-normalize-headings |
| 82 | + |
| 83 | +[codecov-badge]: https://img.shields.io/codecov/c/github/syntax-tree/mdast-normalize-headings.svg |
| 84 | + |
| 85 | +[codecov]: https://codecov.io/github/syntax-tree/mdast-normalize-headings |
| 86 | + |
| 87 | +[npm]: https://docs.npmjs.com/cli/install |
| 88 | + |
| 89 | +[license]: license |
| 90 | + |
| 91 | +[contributing]: https://github.com/syntax-tree/unist/blob/master/contributing.md |
| 92 | + |
| 93 | +[coc]: https://github.com/syntax-tree/unist/blob/master/code-of-conduct.md |
| 94 | + |
| 95 | +[mdast]: https://github.com/syntax-tree/mdast |
| 96 | + |
| 97 | +[remark]: https://github.com/remarkjs/remark |
| 98 | + |
| 99 | +[man]: https://github.com/remarkjs/remark-man |
| 100 | + |
| 101 | +[normalize-headings]: https://github.com/remarkjs/remark-normalize-headings |
0 commit comments