8
8
[ ![ Backers] [ backers-badge ]] [ collective ]
9
9
[ ![ Chat] [ chat-badge ]] [ chat ]
10
10
11
- [ ** mdast** ] [ mdast ] utility to generate table of contents.
11
+ [ mdast] [ ] utility to generate a table of contents.
12
12
13
- ## Install
13
+ ## Contents
14
+
15
+ * [ What is this?] ( #what-is-this )
16
+ * [ When should I use this?] ( #when-should-i-use-this )
17
+ * [ Install] ( #install )
18
+ * [ Use] ( #use )
19
+ * [ API] ( #api )
20
+ * [ ` toc(node[, options]) ` ] ( #tocnode-options )
21
+ * [ Types] ( #types )
22
+ * [ Compatibility] ( #compatibility )
23
+ * [ Security] ( #security )
24
+ * [ Related] ( #related )
25
+ * [ Contribute] ( #contribute )
26
+ * [ License] ( #license )
27
+
28
+ ## What is this?
29
+
30
+ This package is a utility that generates a table of contents from a document.
14
31
15
- This package is [ ESM only] ( https://gist.github.com/sindresorhus/a39789f98801d908bbc7ff3ecc99d99c ) :
16
- Node 12+ is needed to use it and it must be ` import ` ed instead of ` require ` d.
32
+ ## When should I use this?
33
+
34
+ This utility is useful to generate a section so users can more easily navigate
35
+ through a document.
36
+
37
+ This package is wrapped in [ ` remark-toc ` ] [ remark-toc ] for ease of use with
38
+ [ remark] [ ] , where it also injects the table of contents into the document.
39
+
40
+ ## Install
17
41
18
- [ npm] [ ] :
42
+ This package is [ ESM only] [ esm ] .
43
+ In Node.js (version 12.20+, 14.14+, or 16.0+), install with [ npm] [ ] :
19
44
20
45
``` sh
21
46
npm install mdast-util-toc
22
47
```
23
48
49
+ In Deno with [ ` esm.sh ` ] [ esmsh ] :
50
+
51
+ ``` js
52
+ import {toc } from ' https://esm.sh/mdast-util-toc@6'
53
+ ```
54
+
55
+ In browsers with [ ` esm.sh ` ] [ esmsh ] :
56
+
57
+ ``` html
58
+ <script type =" module" >
59
+ import {toc } from ' https://esm.sh/mdast-util-toc@6?bundle'
60
+ </script >
61
+ ```
62
+
24
63
## Use
25
64
26
65
Dependencies:
27
66
28
67
``` javascript
29
- /** @typedef {import('mdast').Root} Root */
68
+ /**
69
+ * @typedef {import('mdast').Root} Root
70
+ */
71
+
30
72
import {u } from ' unist-builder'
31
73
import {toc } from ' mdast-util-toc'
32
74
```
@@ -63,25 +105,27 @@ Yields:
63
105
64
106
## API
65
107
66
- This package exports the following identifiers: ` toc ` .
108
+ This package exports the identifier ` toc ` .
67
109
There is no default export.
68
110
69
- ### ` toc(tree [, options]) `
111
+ ### ` toc(node [, options]) `
70
112
71
- Generate a Table of Contents from a [ tree ] [ ] .
113
+ Generate a table of contents from [ ` node ` ] [ node ] .
72
114
73
- Looks for the first [ heading] [ ] matching ` options.heading ` (case insensitive),
74
- and returns a table of contents ([ list] [ ] ) for all following headings.
115
+ Looks for the first heading matching ` options.heading ` (case insensitive) and
116
+ returns a table of contents (a list) for all following headings.
75
117
If no ` heading ` is specified, creates a table of contents for all headings in
76
118
` tree ` .
77
119
` tree ` is not changed.
78
120
79
- Links to headings are based on GitHub’s style.
80
- Only top-level headings (those not in [ blockquote ] [ ] s or [ list ] [ ] s ), are used.
81
- This default behavior can be changed by passing [ ` parents ` ] [ parents ] .
121
+ Links in the list to headings are based on GitHub’s style.
122
+ Only top-level headings (those not in blockquotes or lists ), are used.
123
+ This default behavior can be changed by passing [ ` options. parents` ] [ parents ] .
82
124
83
125
##### ` options `
84
126
127
+ Configuration (optional).
128
+
85
129
###### ` options.heading `
86
130
87
131
[ Heading] [ ] to look for (` string ` ), wrapped in `new RegExp('^(' + value + ')$',
@@ -103,58 +147,66 @@ contents.
103
147
104
148
###### ` options.tight `
105
149
106
- Whether to compile list- items tightly (` boolean? ` , default: ` false ` ).
150
+ Whether to compile list items tightly (` boolean? ` , default: ` false ` ).
107
151
108
152
###### ` options.ordered `
109
153
110
- Whether to compile list- items as an ordered list (` boolean? ` , default: ` false ` ).
154
+ Whether to compile list items as an ordered list (` boolean? ` , default: ` false ` ).
111
155
112
156
###### ` options.prefix `
113
157
114
- Add a prefix to links to headings in the table of contents (` string? ` ,
115
- default: ` null ` ).
158
+ Add a prefix to links to headings in the table of contents (` string? ` , default:
159
+ ` null ` ).
116
160
Useful for example when later going from [ mdast] [ ] to [ hast] [ ] and sanitizing
117
161
with [ ` hast-util-sanitize ` ] [ sanitize ] .
118
162
119
163
###### ` options.parents `
120
164
121
- Allows headings to be children of certain node [ type ] [ ] s (default: the to ` toc `
122
- given ` tree ` , to only allow top-level headings).
123
- Internally, uses [ unist-util-is] [ is ] to check , so ` parents ` can be any
124
- [ ` is ` -compatible] [ is ] test.
165
+ Allow headings to be children of certain node types (default: the to ` toc ` given
166
+ ` node ` , to only allow top-level headings).
167
+ Internally, uses [ ` unist-util-is ` ] [ is ] , so ` parents ` can be any
168
+ ` is ` -compatible test.
125
169
126
- For example, this would allow headings under either ` root ` or ` blockquote ` to be
127
- used:
170
+ For example, the following code would allow headings under either ` root ` or
171
+ ` blockquote ` to be used:
128
172
129
173
``` js
130
174
toc (tree, {parents: [' root' , ' blockquote' ]})
131
175
```
132
176
133
177
##### Returns
134
178
135
- An object representing the table of contents.
136
-
137
- ###### Properties
179
+ An object representing the table of contents:
138
180
139
181
* ` index ` (` number? ` )
140
- — [ Index ] [ ] of the node right after the table of contents [ heading] [ ] .
182
+ — index of the node right after the table of contents [ heading] [ ] .
141
183
` -1 ` if no heading was found, ` null ` if no ` heading ` was given
142
184
* ` endIndex ` (` number? ` )
143
- — [ Index] [ ] of the first node after ` heading ` that is not part of its
144
- section.
145
- ` -1 ` if no heading was found, ` null ` if no ` heading ` was given,
146
- same as ` index ` if there are no nodes between ` heading ` and the
147
- first heading in the TOC
185
+ — index of the first node after ` heading ` that is not part of its section.
186
+ ` -1 ` if no heading was found, ` null ` if no ` heading ` was given, same as
187
+ ` index ` if there are no nodes between ` heading ` and the first heading in the
188
+ table of contents
148
189
* ` map ` (` Node? ` )
149
- — [ List] [ ] representing the generated table of contents.
150
- ` null ` if no table of contents could be created, either because
151
- no heading was found or because no following headings were found
190
+ — list representing the generated table of contents.
191
+ ` null ` if no table of contents could be created, either because no heading
192
+ was found or because no following headings were found
193
+
194
+ ## Types
195
+
196
+ This package is fully typed with [ TypeScript] [ ] .
197
+ It exports the types ` Options ` and ` Result ` .
198
+
199
+ ## Compatibility
200
+
201
+ Projects maintained by the unified collective are compatible with all maintained
202
+ versions of Node.js.
203
+ As of now, that is Node.js 12.20+, 14.14+, and 16.0+.
204
+ Our projects sometimes work with older versions, but this is not guaranteed.
152
205
153
206
## Security
154
207
155
- Use of ` mdast-util-toc ` does not involve [ ** hast** ] [ hast ] , user content, or
156
- change the tree, so there are no openings for [ cross-site scripting (XSS)] [ xss ]
157
- attacks.
208
+ Use of ` mdast-util-toc ` does not involve [ hast] [ ] , user content, or change the
209
+ tree, so there are no openings for [ cross-site scripting (XSS)] [ xss ] attacks.
158
210
159
211
Injecting ` map ` into the syntax tree may open you up to XSS attacks as existing
160
212
nodes are copied into the table of contents.
@@ -180,22 +232,21 @@ Yields in `map`:
180
232
- [Charlie](#charlie)
181
233
```
182
234
183
- Always use [ ` hast-util-santize ` ] [ sanitize ] when transforming to
184
- [ ** hast** ] [ hast ] .
235
+ Always use [ ` hast-util-santize ` ] [ sanitize ] when transforming to [ hast] [ ] .
185
236
186
237
## Related
187
238
188
239
* [ ` github-slugger ` ] ( https://github.com/Flet/github-slugger )
189
- — Generate a slug just like GitHub does
240
+ — generate a slug just like GitHub does
190
241
* [ ` unist-util-visit ` ] ( https://github.com/syntax-tree/unist-util-visit )
191
242
— visit nodes
192
243
* [ ` unist-util-visit-parents ` ] ( https://github.com/syntax-tree/unist-util-visit-parents )
193
244
— like ` visit ` , but with a stack of parents
194
245
195
246
## Contribute
196
247
197
- See [ ` contributing.md ` in ` syntax-tree/.github ` ] [ contributing ] for ways to get
198
- started.
248
+ See [ ` contributing.md ` ] [ contributing ] in [ ` syntax-tree/.github ` ] [ health ] for
249
+ ways to get started.
199
250
See [ ` support.md ` ] [ support ] for ways to get help.
200
251
201
252
This project has a [ code of conduct] [ coc ] .
@@ -236,15 +287,23 @@ abide by its terms.
236
287
237
288
[ npm ] : https://docs.npmjs.com/cli/install
238
289
290
+ [ esm ] : https://gist.github.com/sindresorhus/a39789f98801d908bbc7ff3ecc99d99c
291
+
292
+ [ esmsh ] : https://esm.sh
293
+
294
+ [ typescript ] : https://www.typescriptlang.org
295
+
239
296
[ license ] : license
240
297
241
298
[ author ] : https://barrythepenguin.github.io
242
299
243
- [ contributing ] : https://github.com/syntax-tree/.github/blob/HEAD/contributing.md
300
+ [ health ] : https://github.com/syntax-tree/.github
301
+
302
+ [ contributing ] : https://github.com/syntax-tree/.github/blob/main/contributing.md
244
303
245
- [ support ] : https://github.com/syntax-tree/.github/blob/HEAD /support.md
304
+ [ support ] : https://github.com/syntax-tree/.github/blob/main /support.md
246
305
247
- [ coc ] : https://github.com/syntax-tree/.github/blob/HEAD /code-of-conduct.md
306
+ [ coc ] : https://github.com/syntax-tree/.github/blob/main /code-of-conduct.md
248
307
249
308
[ mdast ] : https://github.com/syntax-tree/mdast
250
309
@@ -254,18 +313,14 @@ abide by its terms.
254
313
255
314
[ is ] : https://github.com/syntax-tree/unist-util-is
256
315
257
- [ tree ] : https://github.com/syntax-tree/unist#tree
258
-
259
- [ type ] : https://github.com/syntax-tree/unist#type
260
-
261
- [ index ] : https://github.com/syntax-tree/unist#index
262
-
263
316
[ heading ] : https://github.com/syntax-tree/mdast#heading
264
317
265
- [ list ] : https://github.com/syntax-tree/mdast#list
266
-
267
- [ blockquote ] : https://github.com/syntax-tree/mdast#blockquote
318
+ [ node ] : https://github.com/syntax-tree/mdast#node
268
319
269
320
[ parents ] : #optionsparents
270
321
271
322
[ xss ] : https://en.wikipedia.org/wiki/Cross-site_scripting
323
+
324
+ [ remark ] : https://github.com/remarkjs/remark
325
+
326
+ [ remark-toc ] : https://github.com/remarkjs/remark-toc
0 commit comments