Skip to content

Commit b71a526

Browse files
committed
Add improved docs
1 parent 75078c4 commit b71a526

File tree

1 file changed

+112
-43
lines changed

1 file changed

+112
-43
lines changed

readme.md

Lines changed: 112 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -8,19 +8,63 @@
88
[![Backers][backers-badge]][collective]
99
[![Chat][chat-badge]][chat]
1010

11-
[**mdast**][mdast] utility to use headings as ranges.
11+
[mdast][] utility to find headings and replace the content in their section.
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+
* [`headingRange(tree, test|options, handler)`](#headingrangetree-testoptions-handler)
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 lets you find a certain heading, then takes the
31+
content in their section (from it to the next heading of the same or lower
32+
depth), and calls a given handler with the result, so that you can change or
33+
replace things.
34+
35+
## When should I use this?
36+
37+
This utility is typically useful when you have certain sections that can be
38+
generated.
39+
For example, this utility is used by [`remark-toc`][remark-toc] to update the
40+
above `Contents` heading.
41+
42+
A similar package, [`mdast-zone`][mdast-zone], does the same but uses comments
43+
to mark the start and end of sections.
1444

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.
45+
## Install
1746

18-
[npm][]:
47+
This package is [ESM only][esm].
48+
In Node.js (version 12.20+, 14.14+, or 16.0+), install with [npm][]:
1949

2050
```sh
2151
npm install mdast-util-heading-range
2252
```
2353

54+
In Deno with [`esm.sh`][esmsh]:
55+
56+
```js
57+
import {headingRange} from 'https://esm.sh/mdast-util-heading-range@3'
58+
```
59+
60+
In browsers with [`esm.sh`][esmsh]:
61+
62+
```html
63+
<script type="module">
64+
import {headingRange} from 'https://esm.sh/mdast-util-heading-range@3?bundle'
65+
</script>
66+
```
67+
2468
## Use
2569

2670
Say we have the following file, `example.md`:
@@ -33,23 +77,21 @@ Bar.
3377
# Baz
3478
```
3579

36-
And our script, `example.js`, looks as follows:
80+
…and a module `example.js`:
3781

3882
```js
39-
import {readSync} from 'to-vfile'
83+
import {read} from 'to-vfile'
4084
import {remark} from 'remark'
4185
import {headingRange} from 'mdast-util-heading-range'
4286

43-
const file = readSync('example.md')
87+
const file = await remark()
88+
.use(myPluginThatReplacesFoo)
89+
.process(await read('example.md'))
4490

45-
remark()
46-
.use(plugin)
47-
.process(file)
48-
.then((file) => {
49-
console.log(String(file))
50-
})
91+
console.log(String(file))
5192

52-
function plugin() {
93+
/** @type {import('unified').Plugin<[], import('mdast').Root>} */
94+
function myPluginThatReplacesFoo() {
5395
return (tree) => {
5496
headingRange(tree, 'foo', (start, nodes, end) => [
5597
start,
@@ -60,7 +102,7 @@ function plugin() {
60102
}
61103
```
62104

63-
Now, running `node example` yields:
105+
…now running `node example.js` yields:
64106

65107
```markdown
66108
# Foo
@@ -72,20 +114,18 @@ Qux.
72114

73115
## API
74116

75-
This package exports the following identifiers: `headingRange`.
117+
This package exports the identifier `headingRange`.
76118
There is no default export.
77119

78120
### `headingRange(tree, test|options, handler)`
79121

80-
Search `tree` ([`Node`][node]) and transform a section without affecting other
81-
parts with `handler` ([`Function`][handler]).
82-
A “section” is a heading that passes `test`, until the next heading of the same
83-
or lower depth, or the end of the document.
84-
If `ignoreFinalDefinitions: true`, final definitions “in” the section are
85-
excluded.
122+
Search `tree` ([`Node`][node]) and transform a section with `handler`
123+
([`Function`][handler]).
86124

87125
##### `options`
88126

127+
Configuration (optional).
128+
89129
###### `options.test`
90130

91131
Heading to look for (`string`, `RegExp`, [`Function`][test]).
@@ -94,42 +134,58 @@ when `RegExp`, wrapped in `function (value) {expression.test(value)}`
94134

95135
###### `options.ignoreFinalDefinitions`
96136

97-
Ignore final definitions otherwise in the section (`boolean`, default: `false`).
137+
Ignore trailing definitions otherwise in the section (`boolean`, default:
138+
`false`).
98139

99140
#### `function test(value, node)`
100141

101-
Function invoked for each heading with its content (`string`) and `node`
142+
Function called for each heading with its content (`string`) and `node`
102143
itself ([`Heading`][heading]) to check if it’s the one to look for.
103144

104145
###### Returns
105146

106-
`Boolean?`, `true` if this is the heading to use.
147+
Whether to use this heading (`boolean`).
107148

108-
#### `function handler(start, nodes, end?, scope)`
149+
#### `function handler(start, nodes, end, info)`
109150

110-
Callback invoked when a range is found.
151+
Callback called when a range is found.
111152

112153
##### Parameters
113154

155+
Arguments.
156+
114157
###### `start`
115158

116159
Start of range ([`Heading`][heading]).
117160

118161
###### `nodes`
119162

120-
Nodes between `start` and `end` ([`Array.<Node>`][node]).
163+
Nodes between `start` and `end` ([`Array<Node>`][node]).
121164

122165
###### `end`
123166

124167
End of range, if any ([`Node?`][node]).
125168

126-
###### `scope`
169+
###### `info`
127170

128171
Extra info (`Object`):
129172

130-
* `parent` ([`Node`][node]) — Parent of the range
131-
* `start` (`number`) — Index of `start` in `parent`
132-
* `end` (`number?`) — Index of `end` in `parent`
173+
* `parent` ([`Node`][node]) — parent of the range
174+
* `start` (`number`) — index of `start` in `parent`
175+
* `end` (`number?`) — index of `end` in `parent`
176+
177+
## Types
178+
179+
This package is fully typed with [TypeScript][].
180+
This package exports the types `Handler`, `Options`, `TestFunction`, `Test`,
181+
and `ZoneInfo`.
182+
183+
## Compatibility
184+
185+
Projects maintained by the unified collective are compatible with all maintained
186+
versions of Node.js.
187+
As of now, that is Node.js 12.20+, 14.14+, and 16.0+.
188+
Our projects sometimes work with older versions, but this is not guaranteed.
133189

134190
## Security
135191

@@ -140,6 +196,7 @@ The following example shows how a script is injected that could run when loaded
140196
in a browser.
141197

142198
```js
199+
/** @type {import('mdast-util-heading-range').Handler} */
143200
function handler(start, nodes, end) {
144201
return [start, {type: 'html', value: 'alert(1)'}, end]
145202
}
@@ -156,21 +213,21 @@ Yields:
156213
```
157214

158215
Either do not use user input in `handler` or use
159-
[`hast-util-santize`][sanitize].
216+
[`hast-util-santize`][hast-util-sanitize].
160217

161218
## Related
162219

163220
* [`mdast-zone`](https://github.com/syntax-tree/mdast-zone)
164-
— comments as ranges or markers instead of headings
221+
similar but uses comments to mark the start and end of sections
165222

166223
## Contribute
167224

168-
See [`contributing.md` in `syntax-tree/.github`][contributing] for ways to get
169-
started.
225+
See [`contributing.md`][contributing] in [`syntax-tree/.github`][health] for
226+
ways to get started.
170227
See [`support.md`][support] for ways to get help.
171228

172229
This project has a [code of conduct][coc].
173-
By interacting with this repository, organization, or community you agree to
230+
By interacting with this repository, organisation, or community you agree to
174231
abide by its terms.
175232

176233
## License
@@ -207,21 +264,29 @@ abide by its terms.
207264

208265
[npm]: https://docs.npmjs.com/cli/install
209266

267+
[esm]: https://gist.github.com/sindresorhus/a39789f98801d908bbc7ff3ecc99d99c
268+
269+
[esmsh]: https://esm.sh
270+
271+
[typescript]: https://www.typescriptlang.org
272+
210273
[license]: license
211274

212275
[author]: https://wooorm.com
213276

214-
[contributing]: https://github.com/syntax-tree/.github/blob/HEAD/contributing.md
277+
[health]: https://github.com/syntax-tree/.github
215278

216-
[support]: https://github.com/syntax-tree/.github/blob/HEAD/support.md
279+
[contributing]: https://github.com/syntax-tree/.github/blob/main/contributing.md
217280

218-
[coc]: https://github.com/syntax-tree/.github/blob/HEAD/code-of-conduct.md
281+
[support]: https://github.com/syntax-tree/.github/blob/main/support.md
282+
283+
[coc]: https://github.com/syntax-tree/.github/blob/main/code-of-conduct.md
219284

220285
[mdast]: https://github.com/syntax-tree/mdast
221286

222287
[node]: https://github.com/syntax-tree/unist#node
223288

224-
[handler]: #function-handlerstart-nodes-end-scope
289+
[handler]: #function-handlerstart-nodes-end-info
225290

226291
[heading]: https://github.com/syntax-tree/mdast#heading
227292

@@ -231,4 +296,8 @@ abide by its terms.
231296

232297
[hast]: https://github.com/syntax-tree/hast
233298

234-
[sanitize]: https://github.com/syntax-tree/hast-util-sanitize
299+
[mdast-zone]: https://github.com/syntax-tree/mdast-zone
300+
301+
[hast-util-sanitize]: https://github.com/syntax-tree/hast-util-sanitize
302+
303+
[remark-toc]: https://github.com/remarkjs/remark-toc

0 commit comments

Comments
 (0)