8
8
[ ![ Backers] [ backers-badge ]] [ collective ]
9
9
[ ![ Chat] [ chat-badge ]] [ chat ]
10
10
11
- [ ** mdast** ] [ mdast ] utility to use headings as ranges .
11
+ [ mdast] [ ] utility to find headings and replace the content in their section .
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
+ * [ ` 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.
14
44
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
17
46
18
- [ npm] [ ] :
47
+ This package is [ ESM only] [ esm ] .
48
+ In Node.js (version 12.20+, 14.14+, or 16.0+), install with [ npm] [ ] :
19
49
20
50
``` sh
21
51
npm install mdast-util-heading-range
22
52
```
23
53
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
+
24
68
## Use
25
69
26
70
Say we have the following file, ` example.md ` :
33
77
# Baz
34
78
```
35
79
36
- And our script, ` example.js ` , looks as follows :
80
+ …and a module ` example.js ` :
37
81
38
82
``` js
39
- import {readSync } from ' to-vfile'
83
+ import {read } from ' to-vfile'
40
84
import {remark } from ' remark'
41
85
import {headingRange } from ' mdast-util-heading-range'
42
86
43
- const file = readSync (' example.md' )
87
+ const file = await remark ()
88
+ .use (myPluginThatReplacesFoo)
89
+ .process (await read (' example.md' ))
44
90
45
- remark ()
46
- .use (plugin)
47
- .process (file)
48
- .then ((file ) => {
49
- console .log (String (file))
50
- })
91
+ console .log (String (file))
51
92
52
- function plugin () {
93
+ /** @type {import('unified').Plugin<[], import('mdast').Root>} */
94
+ function myPluginThatReplacesFoo () {
53
95
return (tree ) => {
54
96
headingRange (tree, ' foo' , (start , nodes , end ) => [
55
97
start,
@@ -60,7 +102,7 @@ function plugin() {
60
102
}
61
103
```
62
104
63
- Now, running ` node example ` yields:
105
+ …now running ` node example.js ` yields:
64
106
65
107
``` markdown
66
108
# Foo
@@ -72,20 +114,18 @@ Qux.
72
114
73
115
## API
74
116
75
- This package exports the following identifiers: ` headingRange ` .
117
+ This package exports the identifier ` headingRange ` .
76
118
There is no default export.
77
119
78
120
### ` headingRange(tree, test|options, handler) `
79
121
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 ] ).
86
124
87
125
##### ` options `
88
126
127
+ Configuration (optional).
128
+
89
129
###### ` options.test `
90
130
91
131
Heading to look for (` string ` , ` RegExp ` , [ ` Function ` ] [ test ] ).
@@ -94,42 +134,58 @@ when `RegExp`, wrapped in `function (value) {expression.test(value)}`
94
134
95
135
###### ` options.ignoreFinalDefinitions `
96
136
97
- Ignore final definitions otherwise in the section (` boolean ` , default: ` false ` ).
137
+ Ignore trailing definitions otherwise in the section (` boolean ` , default:
138
+ ` false ` ).
98
139
99
140
#### ` function test(value, node) `
100
141
101
- Function invoked for each heading with its content (` string ` ) and ` node `
142
+ Function called for each heading with its content (` string ` ) and ` node `
102
143
itself ([ ` Heading ` ] [ heading ] ) to check if it’s the one to look for.
103
144
104
145
###### Returns
105
146
106
- ` Boolean? ` , ` true ` if this is the heading to use .
147
+ Whether to use this heading ( ` boolean ` ) .
107
148
108
- #### ` function handler(start, nodes, end?, scope ) `
149
+ #### ` function handler(start, nodes, end, info ) `
109
150
110
- Callback invoked when a range is found.
151
+ Callback called when a range is found.
111
152
112
153
##### Parameters
113
154
155
+ Arguments.
156
+
114
157
###### ` start `
115
158
116
159
Start of range ([ ` Heading ` ] [ heading ] ).
117
160
118
161
###### ` nodes `
119
162
120
- Nodes between ` start ` and ` end ` ([ ` Array. <Node> ` ] [ node ] ).
163
+ Nodes between ` start ` and ` end ` ([ ` Array<Node> ` ] [ node ] ).
121
164
122
165
###### ` end `
123
166
124
167
End of range, if any ([ ` Node? ` ] [ node ] ).
125
168
126
- ###### ` scope `
169
+ ###### ` info `
127
170
128
171
Extra info (` Object ` ):
129
172
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.
133
189
134
190
## Security
135
191
@@ -140,6 +196,7 @@ The following example shows how a script is injected that could run when loaded
140
196
in a browser.
141
197
142
198
``` js
199
+ /** @type {import('mdast-util-heading-range').Handler} */
143
200
function handler (start , nodes , end ) {
144
201
return [start, {type: ' html' , value: ' alert(1)' }, end]
145
202
}
@@ -156,21 +213,21 @@ Yields:
156
213
```
157
214
158
215
Either do not use user input in ` handler ` or use
159
- [ ` hast-util-santize ` ] [ sanitize ] .
216
+ [ ` hast-util-santize ` ] [ hast-util- sanitize] .
160
217
161
218
## Related
162
219
163
220
* [ ` 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
165
222
166
223
## Contribute
167
224
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.
170
227
See [ ` support.md ` ] [ support ] for ways to get help.
171
228
172
229
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
174
231
abide by its terms.
175
232
176
233
## License
@@ -207,21 +264,29 @@ abide by its terms.
207
264
208
265
[ npm ] : https://docs.npmjs.com/cli/install
209
266
267
+ [ esm ] : https://gist.github.com/sindresorhus/a39789f98801d908bbc7ff3ecc99d99c
268
+
269
+ [ esmsh ] : https://esm.sh
270
+
271
+ [ typescript ] : https://www.typescriptlang.org
272
+
210
273
[ license ] : license
211
274
212
275
[ author ] : https://wooorm.com
213
276
214
- [ contributing ] : https://github.com/syntax-tree/.github/blob/HEAD/contributing.md
277
+ [ health ] : https://github.com/syntax-tree/.github
215
278
216
- [ support ] : https://github.com/syntax-tree/.github/blob/HEAD/support .md
279
+ [ contributing ] : https://github.com/syntax-tree/.github/blob/main/contributing .md
217
280
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
219
284
220
285
[ mdast ] : https://github.com/syntax-tree/mdast
221
286
222
287
[ node ] : https://github.com/syntax-tree/unist#node
223
288
224
- [ handler ] : #function-handlerstart-nodes-end-scope
289
+ [ handler ] : #function-handlerstart-nodes-end-info
225
290
226
291
[ heading ] : https://github.com/syntax-tree/mdast#heading
227
292
@@ -231,4 +296,8 @@ abide by its terms.
231
296
232
297
[ hast ] : https://github.com/syntax-tree/hast
233
298
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