88[ ![ Backers] [ backers-badge ]] [ collective ]
99[ ![ Chat] [ chat-badge ]] [ chat ]
1010
11- [ ** mdast** ] [ mdast ] utility to generate table of contents.
11+ [ mdast] [ ] utility to generate a table of contents.
1212
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.
1431
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
1741
18- [ npm] [ ] :
42+ This package is [ ESM only] [ esm ] .
43+ In Node.js (version 12.20+, 14.14+, or 16.0+), install with [ npm] [ ] :
1944
2045``` sh
2146npm install mdast-util-toc
2247```
2348
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+
2463## Use
2564
2665Dependencies:
2766
2867``` javascript
29- /** @typedef {import('mdast').Root} Root */
68+ /**
69+ * @typedef {import('mdast').Root} Root
70+ */
71+
3072import {u } from ' unist-builder'
3173import {toc } from ' mdast-util-toc'
3274```
@@ -63,25 +105,27 @@ Yields:
63105
64106## API
65107
66- This package exports the following identifiers: ` toc ` .
108+ This package exports the identifier ` toc ` .
67109There is no default export.
68110
69- ### ` toc(tree [, options]) `
111+ ### ` toc(node [, options]) `
70112
71- Generate a Table of Contents from a [ tree ] [ ] .
113+ Generate a table of contents from [ ` node ` ] [ node ] .
72114
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.
75117If no ` heading ` is specified, creates a table of contents for all headings in
76118` tree ` .
77119` tree ` is not changed.
78120
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 ] .
82124
83125##### ` options `
84126
127+ Configuration (optional).
128+
85129###### ` options.heading `
86130
87131[ Heading] [ ] to look for (` string ` ), wrapped in `new RegExp('^(' + value + ')$',
@@ -103,58 +147,66 @@ contents.
103147
104148###### ` options.tight `
105149
106- Whether to compile list- items tightly (` boolean? ` , default: ` false ` ).
150+ Whether to compile list items tightly (` boolean? ` , default: ` false ` ).
107151
108152###### ` options.ordered `
109153
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 ` ).
111155
112156###### ` options.prefix `
113157
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 ` ).
116160Useful for example when later going from [ mdast] [ ] to [ hast] [ ] and sanitizing
117161with [ ` hast-util-sanitize ` ] [ sanitize ] .
118162
119163###### ` options.parents `
120164
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.
125169
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:
128172
129173``` js
130174toc (tree, {parents: [' root' , ' blockquote' ]})
131175```
132176
133177##### Returns
134178
135- An object representing the table of contents.
136-
137- ###### Properties
179+ An object representing the table of contents:
138180
139181* ` 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] [ ] .
141183 ` -1 ` if no heading was found, ` null ` if no ` heading ` was given
142184* ` 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
148189* ` 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.
152205
153206## Security
154207
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.
158210
159211Injecting ` map ` into the syntax tree may open you up to XSS attacks as existing
160212nodes are copied into the table of contents.
@@ -180,22 +232,21 @@ Yields in `map`:
180232 - [Charlie](#charlie)
181233```
182234
183- Always use [ ` hast-util-santize ` ] [ sanitize ] when transforming to
184- [ ** hast** ] [ hast ] .
235+ Always use [ ` hast-util-santize ` ] [ sanitize ] when transforming to [ hast] [ ] .
185236
186237## Related
187238
188239* [ ` github-slugger ` ] ( https://github.com/Flet/github-slugger )
189- — Generate a slug just like GitHub does
240+ — generate a slug just like GitHub does
190241* [ ` unist-util-visit ` ] ( https://github.com/syntax-tree/unist-util-visit )
191242 — visit nodes
192243* [ ` unist-util-visit-parents ` ] ( https://github.com/syntax-tree/unist-util-visit-parents )
193244 — like ` visit ` , but with a stack of parents
194245
195246## Contribute
196247
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.
199250See [ ` support.md ` ] [ support ] for ways to get help.
200251
201252This project has a [ code of conduct] [ coc ] .
@@ -236,15 +287,23 @@ abide by its terms.
236287
237288[ npm ] : https://docs.npmjs.com/cli/install
238289
290+ [ esm ] : https://gist.github.com/sindresorhus/a39789f98801d908bbc7ff3ecc99d99c
291+
292+ [ esmsh ] : https://esm.sh
293+
294+ [ typescript ] : https://www.typescriptlang.org
295+
239296[ license ] : license
240297
241298[ author ] : https://barrythepenguin.github.io
242299
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
244303
245- [ support ] : https://github.com/syntax-tree/.github/blob/HEAD /support.md
304+ [ support ] : https://github.com/syntax-tree/.github/blob/main /support.md
246305
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
248307
249308[ mdast ] : https://github.com/syntax-tree/mdast
250309
@@ -254,18 +313,14 @@ abide by its terms.
254313
255314[ is ] : https://github.com/syntax-tree/unist-util-is
256315
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-
263316[ heading ] : https://github.com/syntax-tree/mdast#heading
264317
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
268319
269320[ parents ] : #optionsparents
270321
271322[ 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