|
3 | 3 | [![Build][build-badge]][build] |
4 | 4 | [![Coverage][coverage-badge]][coverage] |
5 | 5 | [![Downloads][downloads-badge]][downloads] |
6 | | -[![Chat][chat-badge]][chat] |
| 6 | +[![Size][size-badge]][size] |
7 | 7 | [![Sponsors][sponsors-badge]][collective] |
8 | 8 | [![Backers][backers-badge]][collective] |
| 9 | +[![Chat][chat-badge]][chat] |
9 | 10 |
|
10 | | -Generate a Table of Contents from **[mdast][]** trees. |
| 11 | +[**mdast**][mdast] utility to generate table of contents. |
11 | 12 |
|
12 | | -## Installation |
| 13 | +## Install |
13 | 14 |
|
14 | 15 | [npm][]: |
15 | 16 |
|
16 | | -```bash |
| 17 | +```sh |
17 | 18 | npm install mdast-util-toc |
18 | 19 | ``` |
19 | 20 |
|
@@ -63,131 +64,151 @@ Yields: |
63 | 64 |
|
64 | 65 | ## API |
65 | 66 |
|
66 | | -### `toc(node[, options])` |
| 67 | +### `toc(tree[, options])` |
67 | 68 |
|
68 | | -Generate a Table of Contents from a Markdown document. |
| 69 | +Generate a Table of Contents from a [tree][]. |
69 | 70 |
|
70 | | -Looks for the first heading matching `options.heading` (case insensitive, |
| 71 | +Looks for the first [heading][] matching `options.heading` (case insensitive, |
71 | 72 | supports alt/title attributes for links and images too), and returns a table |
72 | | -of contents for all following headings. |
| 73 | +of contents ([list][]) for all following headings. |
73 | 74 | If no `heading` is specified, creates a table of contents for all headings in |
74 | | -`node`. |
| 75 | +`tree`. |
| 76 | +`tree` is not changed. |
75 | 77 |
|
76 | 78 | Links to headings are based on GitHub’s style. |
77 | | -Only top-level headings (those not in blockquotes or lists), are used. |
78 | | -(Change this default behavior by using option `parents` as described below) |
79 | | -The given node is not modified. |
| 79 | +Only top-level headings (those not in [blockquote][]s or [list][]s), are used. |
| 80 | +This default behavior can be changed by passing [`parents`][parents]. |
80 | 81 |
|
81 | 82 | ##### `options` |
82 | 83 |
|
83 | 84 | ###### `options.heading` |
84 | 85 |
|
85 | | -Heading to look for (`string`), wrapped in `new RegExp('^(' + value + ')$', 'i')`. |
| 86 | +[Heading][] to look for (`string`), wrapped in `new RegExp('^(' + value + ')$', |
| 87 | +'i')`. |
86 | 88 |
|
87 | 89 | ###### `options.maxDepth` |
88 | 90 |
|
89 | 91 | Maximum heading depth to include in the table of contents (`number`, default: |
90 | 92 | `6`), |
91 | | -This is inclusive, thus, when set to `3`, level three headings, are included |
92 | | -(those with three hashes, `###`). |
| 93 | +This is inclusive: when set to `3`, level three headings are included (those |
| 94 | +with three hashes, `###`). |
93 | 95 |
|
94 | 96 | ###### `options.tight` |
95 | 97 |
|
96 | 98 | Whether to compile list-items tightly (`boolean?`, default: `false`). |
97 | 99 |
|
98 | 100 | ###### `options.prefix` |
99 | 101 |
|
100 | | -Add a prefix to links to headings (`string?`, default: `null`). |
101 | | -Useful for example when later going from mdast to hast and sanitizing with |
102 | | -[`hast-util-sanitize`][hast-util-sanitize]. |
| 102 | +Add a prefix to links to headings in the table of contents (`string?`, |
| 103 | +default: `null`). |
| 104 | +Useful for example when later going from [mdast][] to [hast][] and sanitizing |
| 105 | +with [`hast-util-sanitize`][hast-util-sanitize]. |
103 | 106 |
|
104 | 107 | ###### `options.parents` |
105 | 108 |
|
106 | | -Allows headings to be children of certain node types. |
107 | | -Internally, it uses |
108 | | -[unist-util-is](https://github.com/syntax-tree/unist-util-is) to check. |
109 | | -Hence all types that can be passed in as first parameter can be used here, |
110 | | -including `Function`, `string`, `Object` and `Array.<Test>`. |
111 | | -Check |
112 | | -[documentation](https://github.com/syntax-tree/unist-util-is#readme) |
113 | | -for details. |
114 | | -(default: the first parameter `node`, which only allows top-level headings) |
| 109 | +Allows headings to be children of certain node [type][]s (default: the to `toc` |
| 110 | +given `tree`, to only allow top-level headings). |
| 111 | +Internally, uses [unist-util-is][is] to check, so `parents` can be any |
| 112 | +[`is`-compatible][is] test. |
115 | 113 |
|
116 | | -Example: |
| 114 | +For example, this would allow headings under either `root` or `blockquote` to be |
| 115 | +used: |
117 | 116 |
|
118 | | -```json |
119 | | -{ |
120 | | - "parents": ["root", "blockquote"] |
121 | | -} |
| 117 | +```js |
| 118 | +toc(tree, {parents: ['root', 'blockquote']}) |
122 | 119 | ``` |
123 | 120 |
|
124 | | -This would allow headings under either `root` or `blockquote` to be used. |
125 | | - |
126 | 121 | ##### Returns |
127 | 122 |
|
128 | 123 | An object representing the table of contents. |
129 | 124 |
|
130 | 125 | ###### Properties |
131 | 126 |
|
132 | 127 | * `index` (`number?`) |
133 | | - — Position of the `heading` in `node`. `-1` if no heading |
134 | | - was found, `null` if no heading was given |
| 128 | + — [Index][] of the found table of contents [heading][] in `tree`. |
| 129 | + `-1` if no heading was found, `null` if no `heading` was given |
135 | 130 | * `endIndex` (`number?`) |
136 | | - — Position of the last node after `heading` before the TOC starts. |
137 | | - `-1` if no heading was found, `null` if no heading was given, |
| 131 | + — [Index][] of the last node after `heading` before the TOC starts. |
| 132 | + `-1` if no heading was found, `null` if no `heading` was given, |
138 | 133 | same as `index` if there are no nodes between `heading` and the |
139 | 134 | first heading in the TOC |
140 | 135 | * `map` (`Node?`) |
141 | | - — List node representing the generated table of contents. |
| 136 | + — [List][] representing the generated table of contents. |
142 | 137 | `null` if no table of contents could be created, either because |
143 | | - `heading` didn’t exist, or because no following headings were found |
| 138 | + no heading was found or because no following headings were found |
144 | 139 |
|
145 | 140 | ## Contribute |
146 | 141 |
|
147 | | -See [`contributing.md` in `syntax-tree/mdast`][contributing] for ways to get |
| 142 | +See [`contributing.md` in `syntax-tree/.github`][contributing] for ways to get |
148 | 143 | started. |
| 144 | +See [`support.md`][support] for ways to get help. |
149 | 145 |
|
150 | | -This organisation has a [Code of Conduct][coc]. By interacting with this |
151 | | -repository, organisation, or community you agree to abide by its terms. |
| 146 | +This project has a [Code of Conduct][coc]. |
| 147 | +By interacting with this repository, organisation, or community you agree to |
| 148 | +abide by its terms. |
152 | 149 |
|
153 | 150 | ## License |
154 | 151 |
|
155 | 152 | [MIT][license] © [Jonathan Haines][author] |
156 | 153 |
|
157 | 154 | <!-- Definitions --> |
158 | 155 |
|
159 | | -[build-badge]: https://img.shields.io/travis/syntax-tree/mdast-util-toc.svg |
| 156 | +[build-badge]: https://img.shields.io/travis/syntax-tree/nlcst-is-literal.svg |
160 | 157 |
|
161 | | -[build]: https://travis-ci.org/syntax-tree/mdast-util-toc |
| 158 | +[build]: https://travis-ci.org/syntax-tree/nlcst-is-literal |
162 | 159 |
|
163 | | -[coverage-badge]: https://img.shields.io/codecov/c/github/syntax-tree/mdast-util-toc.svg |
| 160 | +[coverage-badge]: https://img.shields.io/codecov/c/github/syntax-tree/nlcst-is-literal.svg |
164 | 161 |
|
165 | | -[coverage]: https://codecov.io/github/syntax-tree/mdast-util-toc |
| 162 | +[coverage]: https://codecov.io/github/syntax-tree/nlcst-is-literal |
166 | 163 |
|
167 | | -[downloads-badge]: https://img.shields.io/npm/dm/mdast-util-toc.svg |
| 164 | +[downloads-badge]: https://img.shields.io/npm/dm/nlcst-is-literal.svg |
168 | 165 |
|
169 | | -[downloads]: https://www.npmjs.com/package/mdast-util-toc |
| 166 | +[downloads]: https://www.npmjs.com/package/nlcst-is-literal |
170 | 167 |
|
171 | | -[chat-badge]: https://img.shields.io/badge/join%20the%20community-on%20spectrum-7b16ff.svg |
| 168 | +[size-badge]: https://img.shields.io/bundlephobia/minzip/nlcst-is-literal.svg |
172 | 169 |
|
173 | | -[chat]: https://spectrum.chat/unified/remark |
| 170 | +[size]: https://bundlephobia.com/result?p=nlcst-is-literal |
174 | 171 |
|
175 | 172 | [sponsors-badge]: https://opencollective.com/unified/sponsors/badge.svg |
176 | 173 |
|
177 | 174 | [backers-badge]: https://opencollective.com/unified/backers/badge.svg |
178 | 175 |
|
179 | 176 | [collective]: https://opencollective.com/unified |
180 | 177 |
|
| 178 | +[chat-badge]: https://img.shields.io/badge/join%20the%20community-on%20spectrum-7b16ff.svg |
| 179 | + |
| 180 | +[chat]: https://spectrum.chat/unified/syntax-tree |
| 181 | + |
181 | 182 | [npm]: https://docs.npmjs.com/cli/install |
182 | 183 |
|
183 | 184 | [license]: license |
184 | 185 |
|
185 | 186 | [author]: https://barrythepenguin.github.io |
186 | 187 |
|
187 | | -[mdast]: https://github.com/syntax-tree/mdast |
| 188 | +[contributing]: https://github.com/syntax-tree/.github/blob/master/contributing.md |
188 | 189 |
|
189 | | -[contributing]: https://github.com/syntax-tree/mdast/blob/master/contributing.md |
| 190 | +[support]: https://github.com/syntax-tree/.github/blob/master/support.md |
190 | 191 |
|
191 | | -[coc]: https://github.com/syntax-tree/mdast/blob/master/code-of-conduct.md |
| 192 | +[coc]: https://github.com/syntax-tree/.github/blob/master/code-of-conduct.md |
| 193 | + |
| 194 | +[mdast]: https://github.com/syntax-tree/mdast |
| 195 | + |
| 196 | +[hast]: https://github.com/syntax-tree/hast |
192 | 197 |
|
193 | 198 | [hast-util-sanitize]: https://github.com/syntax-tree/hast-util-sanitize |
| 199 | + |
| 200 | +[is]: https://github.com/syntax-tree/unist-util-is |
| 201 | + |
| 202 | +[tree]: https://github.com/syntax-tree/unist#tree |
| 203 | + |
| 204 | +[type]: https://github.com/syntax-tree/unist#type |
| 205 | + |
| 206 | +[index]: https://github.com/syntax-tree/unist#index |
| 207 | + |
| 208 | +[heading]: https://github.com/syntax-tree/mdast#heading |
| 209 | + |
| 210 | +[list]: https://github.com/syntax-tree/mdast#list |
| 211 | + |
| 212 | +[blockquote]: https://github.com/syntax-tree/mdast#blockquote |
| 213 | + |
| 214 | +[parents]: #optionsparents |
0 commit comments