Skip to content

Commit dbb8c51

Browse files
committedJan 25, 2023
Add improved docs
1 parent 92f8a77 commit dbb8c51

File tree

4 files changed

+96
-90
lines changed

4 files changed

+96
-90
lines changed
 

‎lib/contents.js

+4-3
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,13 @@
1010
* @typedef ContentsOptions
1111
* Build configuration.
1212
* @property {boolean | null | undefined} [tight=false]
13-
* Whether to compile list-items tightly.
13+
* Whether to compile list items tightly.
1414
* @property {boolean | null | undefined} [ordered=false]
15-
* Whether to compile list-items as an ordered list, otherwise they are
15+
* Whether to compile list items as an ordered list, otherwise they are
1616
* unordered.
17-
* @property {string | null | undefined} [prefix=null]
17+
* @property {string | null | undefined} [prefix=undefined]
1818
* Add a prefix to links to headings in the table of contents.
19+
*
1920
* Useful for example when later going from mdast to hast and sanitizing with
2021
* `hast-util-sanitize`.
2122
*/

‎lib/index.js

+9-3
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,17 @@
1818
* @typedef Result
1919
* Results.
2020
* @property {number | null} index
21-
* Where the contents section starts, if looking for a heading.
21+
* Index of the node right after the table of contents heading, `-1` if no
22+
* heading was found, `null` if no `heading` was given.
2223
* @property {number | null} endIndex
23-
* Where the contents section ends, if looking for a heading.
24+
* Index of the first node after `heading` that is not part of its section,
25+
* `-1` if no heading was found, `null` if no `heading` was given, same as
26+
* `index` if there are no nodes between `heading` and the first heading in
27+
* the table of contents.
2428
* @property {List | null} map
25-
* Built table of contents (`List`).
29+
* List representing the generated table of contents, `null` if no table of
30+
* contents could be created, either because no heading was found or because
31+
* no following headings were found.
2632
*/
2733

2834
import {search} from './search.js'

‎lib/search.js

+6-5
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,14 @@
1515
/**
1616
* @typedef SearchOptions
1717
* Search configuration.
18+
* @property {Rank | null | undefined} [maxDepth=6]
19+
* Maximum heading depth to include in the table of contents.
20+
*
21+
* This is inclusive: when set to `3`, level three headings are included
22+
* (those with three hashes, `###`).
1823
* @property {string | null | undefined} [skip]
1924
* Headings to skip, wrapped in `new RegExp('^(' + value + ')$', 'i')`.
25+
*
2026
* Any heading matching this expression will not be present in the table of
2127
* contents.
2228
* @property {Test} [parents]
@@ -25,11 +31,6 @@
2531
*
2632
* Internally, uses `unist-util-is` to check, so `parents` can be any
2733
* `is`-compatible test.
28-
* @property {Rank | null | undefined} [maxDepth=6]
29-
* Maximum heading depth to include in the table of contents.
30-
*
31-
* This is inclusive: when set to `3`, level three headings are included
32-
* (those with three hashes, `###`).
3334
*
3435
* @typedef SearchEntry
3536
* Entry.

‎readme.md

+77-79
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,9 @@
1717
* [Install](#install)
1818
* [Use](#use)
1919
* [API](#api)
20-
* [`toc(node[, options])`](#tocnode-options)
20+
* [`toc(tree[, options])`](#toctree-options)
21+
* [`Options`](#options)
22+
* [`Result`](#result)
2123
* [Types](#types)
2224
* [Compatibility](#compatibility)
2325
* [Security](#security)
@@ -40,7 +42,7 @@ This package is wrapped in [`remark-toc`][remark-toc] for ease of use with
4042
## Install
4143

4244
This package is [ESM only][esm].
43-
In Node.js (version 12.20+, 14.14+, or 16.0+), install with [npm][]:
45+
In Node.js (version 14.14+ and 16.0+), install with [npm][]:
4446

4547
```sh
4648
npm install mdast-util-toc
@@ -105,12 +107,12 @@ Yields:
105107

106108
## API
107109

108-
This package exports the identifier `toc`.
110+
This package exports the identifier [`toc`][api-toc].
109111
There is no default export.
110112

111-
### `toc(node[, options])`
113+
### `toc(tree[, options])`
112114

113-
Generate a table of contents from [`node`][node].
115+
Generate a table of contents from `tree`.
114116

115117
Looks for the first heading matching `options.heading` (case insensitive) and
116118
returns a table of contents (a list) for all following headings.
@@ -120,87 +122,77 @@ If no `heading` is specified, creates a table of contents for all headings in
120122

121123
Links in the list to headings are based on GitHub’s style.
122124
Only top-level headings (those not in blockquotes or lists), are used.
123-
This default behavior can be changed by passing [`options.parents`][parents].
124-
125-
##### `options`
126-
127-
Configuration (optional).
128-
129-
###### `options.heading`
130-
131-
[Heading][] to look for (`string`), wrapped in `new RegExp('^(' + value + ')$',
132-
'i')`.
133-
134-
###### `options.maxDepth`
135-
136-
Maximum heading depth to include in the table of contents (`number`, default:
137-
`6`),
138-
This is inclusive: when set to `3`, level three headings are included (those
139-
with three hashes, `###`).
140-
141-
###### `options.skip`
142-
143-
Headings to skip (`string`, optional), wrapped in
144-
`new RegExp('^(' + value + ')$', 'i')`.
145-
Any heading matching this expression will not be present in the table of
146-
contents.
147-
148-
###### `options.tight`
149-
150-
Whether to compile list items tightly (`boolean?`, default: `false`).
151-
152-
###### `options.ordered`
153-
154-
Whether to compile list items as an ordered list (`boolean?`, default: `false`).
155-
156-
###### `options.prefix`
157-
158-
Add a prefix to links to headings in the table of contents (`string?`, default:
159-
`null`).
160-
Useful for example when later going from [mdast][] to [hast][] and sanitizing
161-
with [`hast-util-sanitize`][sanitize].
162-
163-
###### `options.parents`
164-
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.
169-
170-
For example, the following code would allow headings under either `root` or
171-
`blockquote` to be used:
172-
173-
```js
174-
toc(tree, {parents: ['root', 'blockquote']})
175-
```
176-
177-
##### Returns
178-
179-
An object representing the table of contents:
180-
181-
* `index` (`number?`)
182-
— index of the node right after the table of contents [heading][].
183-
`-1` if no heading was found, `null` if no `heading` was given
184-
* `endIndex` (`number?`)
185-
— index of the first node after `heading` that is not part of its section.
125+
This default behavior can be changed by passing `options.parents`.
126+
127+
###### Parameters
128+
129+
* `tree` ([`Node`][node])
130+
— tree to search and generate from
131+
* `options` ([`Options`][api-options], optional)
132+
— configuration
133+
134+
###### Returns
135+
136+
Results ([`Result`][api-result]).
137+
138+
### `Options`
139+
140+
Configuration (TypeScript type).
141+
142+
###### Fields
143+
144+
* `heading` (`string`, optional)
145+
— heading to look for, wrapped in `new RegExp('^(' + value + ')$', 'i')`
146+
* `maxDepth` (`number`, default: `6`)
147+
— maximum heading depth to include in the table of contents.
148+
This is inclusive: when set to `3`, level three headings are included
149+
(those with three hashes, `###`)
150+
* `skip` (`string`, optional)
151+
— headings to skip, wrapped in `new RegExp('^(' + value + ')$', 'i')`.
152+
Any heading matching this expression will not be present in the table of
153+
contents
154+
* `parents` ([`Test`][test], default: `tree`)
155+
— allow headings to be children of certain node types.
156+
Can by any [`unist-util-is`][is] compatible test
157+
* `tight` (`boolean`, default: `false`)
158+
— whether to compile list items tightly
159+
* `ordered` (`boolean`, default: `false`)
160+
— whether to compile list items as an ordered list, otherwise they are
161+
unordered
162+
* `prefix` (`string`, optional)
163+
— add a prefix to links to headings in the table of contents.
164+
Useful for example when later going from mdast to hast and sanitizing with
165+
`hast-util-sanitize`.
166+
167+
### `Result`
168+
169+
Results (TypeScript type).
170+
171+
###### Fields
172+
173+
* `index` (`number` or `null`)
174+
— index of the node right after the table of contents heading, `-1` if no
175+
heading was found, `null` if no `heading` was given
176+
* `endIndex` (`number` or `null`)
177+
— index of the first node after `heading` that is not part of its section,
186178
`-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
189-
* `map` (`Node?`)
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
179+
`index` if there are no nodes between `heading` and the first heading in
180+
the table of contents
181+
* `map` ([`List`][list] or `null`)
182+
— list representing the generated table of contents, `null` if no table of
183+
contents could be created, either because no heading was found or because
184+
no following headings were found
193185

194186
## Types
195187

196188
This package is fully typed with [TypeScript][].
197-
It exports the types `Options` and `Result`.
189+
It exports the types [`Options`][api-options] and [`Result`][api-result].
198190

199191
## Compatibility
200192

201193
Projects maintained by the unified collective are compatible with all maintained
202194
versions of Node.js.
203-
As of now, that is Node.js 12.20+, 14.14+, and 16.0+.
195+
As of now, that is Node.js 14.14+ and 16.0+.
204196
Our projects sometimes work with older versions, but this is not guaranteed.
205197

206198
## Security
@@ -313,14 +305,20 @@ abide by its terms.
313305

314306
[is]: https://github.com/syntax-tree/unist-util-is
315307

316-
[heading]: https://github.com/syntax-tree/mdast#heading
308+
[list]: https://github.com/syntax-tree/mdast#list
317309

318310
[node]: https://github.com/syntax-tree/mdast#node
319311

320-
[parents]: #optionsparents
321-
322312
[xss]: https://en.wikipedia.org/wiki/Cross-site_scripting
323313

324314
[remark]: https://github.com/remarkjs/remark
325315

326316
[remark-toc]: https://github.com/remarkjs/remark-toc
317+
318+
[test]: https://github.com/syntax-tree/unist-util-is#test
319+
320+
[api-toc]: #toctree-options
321+
322+
[api-options]: #options
323+
324+
[api-result]: #result

0 commit comments

Comments
 (0)
Please sign in to comment.