Skip to content

Commit e77fa1e

Browse files
committed
Add improved docs
1 parent 41cfd0c commit e77fa1e

File tree

2 files changed

+118
-58
lines changed

2 files changed

+118
-58
lines changed

lib/index.js

Lines changed: 21 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -8,46 +8,48 @@
88
/**
99
* @typedef {Content | Root} Node
1010
* @typedef {Extract<Node, UnistParent>} Parent
11-
*
11+
*/
12+
13+
/**
1214
* @callback TestFunction
13-
* Function called with headings to check if it’s the one to look for.
15+
* Check if a node matches.
1416
* @param {string} value
1517
* Plain-text heading.
1618
* @param {Heading} node
1719
* Heading node.
18-
* @returns {boolean}
20+
* @returns {boolean | null | undefined}
1921
* Whether this is the heading that is searched for.
2022
*
2123
* @typedef {string | RegExp | TestFunction} Test
22-
* A test.
23-
*
24-
* @typedef Options
25-
* Configuration (optional).
26-
* @property {Test} test
27-
* Heading to look for.
24+
* Test for a heading.
2825
*
2926
* When `string`, wrapped in `new RegExp('^(' + value + ')$', 'i')`;
3027
* when `RegExp`, wrapped in `(value) => expression.test(value)`
28+
*
29+
* @typedef Options
30+
* Configuration.
31+
* @property {Test} test
32+
* Test for a heading.
3133
* @property {boolean | null | undefined} [ignoreFinalDefinitions=false]
3234
* Ignore final definitions otherwise in the section.
3335
*
3436
* @typedef ZoneInfo
3537
* Extra info.
36-
* @property {Parent | null} parent
37-
* Parent of the range.
38+
* @property {Parent} parent
39+
* Parent of the section.
3840
* @property {number} start
39-
* index of `start` in `parent`
41+
* Index of `start` in `parent`.
4042
* @property {number | null} end
41-
* index of `end` in `parent`
43+
* Index of `end` in `parent`.
4244
*
4345
* @callback Handler
44-
* Callback called when a range is found.
46+
* Callback called when a section is found.
4547
* @param {Heading} start
46-
* Start of range (a heading node matching `test`).
48+
* Start of section (a heading node matching `test`).
4749
* @param {Array<Node>} between
4850
* Nodes between `start` and `end`.
4951
* @param {Node | undefined} end
50-
* End of range, if any.
52+
* End of section, if any.
5153
* @param {ZoneInfo} scope
5254
* Extra info.
5355
* @returns {Array<Node | null | undefined> | null | undefined | void}
@@ -61,10 +63,10 @@
6163
import {toString} from 'mdast-util-to-string'
6264

6365
/**
64-
* Search `tree` and transform a section without affecting other parts with
66+
* Search `tree` for a heading matching `test` and change its “section” with
6567
* `handler`.
6668
*
67-
* A “section” is a heading that passes `test`, until the next heading of the
69+
* A “section” ranges from the matched heading until the next heading of the
6870
* same or lower depth, or the end of the document.
6971
*
7072
* If `ignoreFinalDefinitions: true`, final definitions “in” the section are
@@ -75,7 +77,7 @@ import {toString} from 'mdast-util-to-string'
7577
* @param {Test | Options} options
7678
* Configuration.
7779
* @param {Handler} handler
78-
* Handle a range.
80+
* Handle a section.
7981
* @returns {void}
8082
* Nothing.
8183
*/

readme.md

Lines changed: 97 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,11 @@
1818
* [Use](#use)
1919
* [API](#api)
2020
* [`headingRange(tree, test|options, handler)`](#headingrangetree-testoptions-handler)
21+
* [`Handler`](#handler)
22+
* [`Options`](#options)
23+
* [`Test`](#test)
24+
* [`TestFunction`](#testfunction)
25+
* [`ZoneInfo`](#zoneinfo)
2126
* [Types](#types)
2227
* [Compatibility](#compatibility)
2328
* [Security](#security)
@@ -45,7 +50,7 @@ to mark the start and end of sections.
4550
## Install
4651

4752
This package is [ESM only][esm].
48-
In Node.js (version 12.20+, 14.14+, or 16.0+), install with [npm][]:
53+
In Node.js (version 14.14+ and 16.0+), install with [npm][]:
4954

5055
```sh
5156
npm install mdast-util-heading-range
@@ -114,77 +119,122 @@ Qux.
114119

115120
## API
116121

117-
This package exports the identifier `headingRange`.
122+
This package exports the identifier [`headingRange`][api-headingrange].
118123
There is no default export.
119124

120125
### `headingRange(tree, test|options, handler)`
121126

122-
Search `tree` ([`Node`][node]) and transform a section with `handler`
123-
([`Function`][handler]).
127+
Search `tree` for a heading matching `test` and change its “section with
128+
`handler`.
124129

125-
##### `options`
130+
A “section” ranges from the matched heading until the next heading of the
131+
same or lower depth, or the end of the document.
126132

127-
Configuration (optional).
133+
If `ignoreFinalDefinitions: true`, final definitions “in” the section are
134+
excluded.
128135

129-
###### `options.test`
136+
###### Parameters
130137

131-
Heading to look for (`string`, `RegExp`, [`Function`][test]).
132-
When `string`, wrapped in `new RegExp('^(' + value + ')$', 'i')`;
133-
when `RegExp`, wrapped in `function (value) {expression.test(value)}`
138+
* `tree` ([`Node`][node])
139+
— tree to change
140+
* `test` ([`Test`][api-test])
141+
— same as passing `{test: Test}`
142+
* `options` ([`Options`][api-options])
143+
— configuration
144+
* `handler` ([`Handler`][api-handler])
145+
— handle a section
146+
147+
###### Returns
134148

135-
###### `options.ignoreFinalDefinitions`
149+
Nothing (`void`).
136150

137-
Ignore trailing definitions otherwise in the section (`boolean`, default:
138-
`false`).
151+
### `Handler`
139152

140-
#### `function test(value, node)`
153+
Callback called when a section is found (TypeScript type).
141154

142-
Function called for each heading with its content (`string`) and `node`
143-
itself ([`Heading`][heading]) to check if it’s the one to look for.
155+
###### Parameters
156+
157+
* `start` ([`Heading`][heading])
158+
— start of section (a heading node matching `test`)
159+
* `nodes` ([`Array<Node>`][node])
160+
— nodes between `start` and `end`
161+
* `end` ([`Node`][node] or `undefined`)
162+
— end of section, if any
163+
* `info` ([`ZoneInfo`][api-zoneinfo])
164+
— extra info
144165

145166
###### Returns
146167

147-
Whether to use this heading (`boolean`).
168+
Results (`Array<Node | null | undefined>`, optional).
169+
170+
If nothing is returned, nothing will be changed.
171+
If an array of nodes (can include `null` and `undefined`) is returned, the
172+
original section will be replaced by those nodes.
173+
174+
### `Options`
175+
176+
Configuration (TypeScript type).
177+
178+
###### Fields
179+
180+
* `test` ([`Test`][api-test])
181+
— test for a heading
182+
* `ignoreFinalDefinitions` (`boolean`, default: `false`)
183+
— ignore final definitions otherwise in the section
184+
185+
### `Test`
186+
187+
Test for a heading (TypeScript type).
148188

149-
#### `function handler(start, nodes, end, info)`
189+
When `string`, wrapped in `new RegExp('^(' + value + ')$', 'i')`;
190+
when `RegExp`, wrapped in `(value) => expression.test(value)`
150191

151-
Callback called when a range is found.
192+
###### Type
152193

153-
##### Parameters
194+
```ts
195+
export type Test = string | RegExp | TestFunction
196+
```
154197
155-
Arguments.
198+
### `TestFunction`
156199
157-
###### `start`
200+
Check if a node matches (TypeScript type).
158201
159-
Start of range ([`Heading`][heading]).
202+
###### Parameters
160203
161-
###### `nodes`
204+
* `value` (`string`)
205+
— plain-text heading
206+
* `node` ([`Heading`][heading])
207+
— heading node
162208
163-
Nodes between `start` and `end` ([`Array<Node>`][node]).
209+
###### Returns
164210
165-
###### `end`
211+
Whether this is the heading that is searched for (`boolean`, optional).
166212
167-
End of range, if any ([`Node?`][node]).
213+
### `ZoneInfo`
168214
169-
###### `info`
215+
Extra info (TypeScript type).
170216
171-
Extra info (`Object`):
217+
###### Fields
172218
173-
* `parent` ([`Node`][node]) — parent of the range
174-
* `start` (`number`) — index of `start` in `parent`
175-
* `end` (`number?`) — index of `end` in `parent`
219+
* `parent` ([`Node`][node])
220+
— parent of the section
221+
* `start` (`number`)
222+
— index of `start` in `parent`
223+
* `end` (`number` or `null`)
224+
— index of `end` in `parent`
176225
177226
## Types
178227
179228
This package is fully typed with [TypeScript][].
180-
This package exports the types `Handler`, `Options`, `TestFunction`, `Test`,
181-
and `ZoneInfo`.
229+
This package exports the types [`Handler`][api-handler],
230+
[`Options`][api-options], [`Test`][api-test],
231+
[`TestFunction`][api-testfunction], and [`ZoneInfo`][api-zoneinfo].
182232
183233
## Compatibility
184234
185235
Projects maintained by the unified collective are compatible with all maintained
186236
versions of Node.js.
187-
As of now, that is Node.js 12.20+, 14.14+, and 16.0+.
237+
As of now, that is Node.js 14.14+ and 16.0+.
188238
Our projects sometimes work with older versions, but this is not guaranteed.
189239
190240
## Security
@@ -286,12 +336,8 @@ abide by its terms.
286336

287337
[node]: https://github.com/syntax-tree/unist#node
288338

289-
[handler]: #function-handlerstart-nodes-end-info
290-
291339
[heading]: https://github.com/syntax-tree/mdast#heading
292340

293-
[test]: #function-testvalue-node
294-
295341
[xss]: https://en.wikipedia.org/wiki/Cross-site_scripting
296342

297343
[hast]: https://github.com/syntax-tree/hast
@@ -301,3 +347,15 @@ abide by its terms.
301347
[hast-util-sanitize]: https://github.com/syntax-tree/hast-util-sanitize
302348

303349
[remark-toc]: https://github.com/remarkjs/remark-toc
350+
351+
[api-headingrange]: #headingrangetree-testoptions-handler
352+
353+
[api-handler]: #handler
354+
355+
[api-options]: #options
356+
357+
[api-test]: #test
358+
359+
[api-testfunction]: #testfunction
360+
361+
[api-zoneinfo]: #zoneinfo

0 commit comments

Comments
 (0)