8
8
[ ![ Backers] [ backers-badge ]] [ collective ]
9
9
[ ![ Chat] [ chat-badge ]] [ chat ]
10
10
11
- [ ** mdast** ] [ mdast ] utility to treat HTML comments as ranges .
11
+ [ mdast] [ ] utility to find two comments and replace the content in them .
12
12
13
- Useful in [ ** remark ** ] [ remark ] plugins.
13
+ ## Contents
14
14
15
- ## Install
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
+ * [ ` zone(tree, name, handler) ` ] ( #zonetree-name-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 certain comments, then takes the
31
+ content between them, and calls a given handler with the result, so that you can
32
+ change or replace things.
33
+
34
+ ## When should I use this?
35
+
36
+ This utility is typically useful when you have certain sections that can be
37
+ generated.
38
+ Comments are a hidden part of markdown, so they can be used as processing
39
+ instructions.
40
+ You can use those comments to define what content to change or replace.
16
41
17
- This package is [ ESM only ] ( https://gist.github.com/sindresorhus/a39789f98801d908bbc7ff3ecc99d99c ) :
18
- Node 12+ is needed to use it and it must be ` import ` ed instead of ` require ` d .
42
+ A similar package, [ ` mdast-util-heading-range ` ] [ mdast-util-heading-range ] , does
43
+ the same but uses a heading to mark the start and end of sections .
19
44
20
- [ npm] [ ] :
45
+ ## Install
46
+
47
+ This package is [ ESM only] [ esm ] .
48
+ In Node.js (version 12.20+, 14.14+, or 16.0+), install with [ npm] [ ] :
21
49
22
50
``` sh
23
51
npm install mdast-zone
24
52
```
25
53
54
+ In Deno with [ ` esm.sh ` ] [ esmsh ] :
55
+
56
+ ``` js
57
+ import {zone } from ' https://esm.sh/mdast-zone@5'
58
+ ```
59
+
60
+ In browsers with [ ` esm.sh ` ] [ esmsh ] :
61
+
62
+ ``` html
63
+ <script type =" module" >
64
+ import {zone } from ' https://esm.sh/mdast-zone@5?bundle'
65
+ </script >
66
+ ```
67
+
26
68
## Use
27
69
28
70
Say we have the following file, ` example.md ` :
35
77
<!--foo end-->
36
78
```
37
79
38
- And our script, ` example.js ` , looks as follows :
80
+ …and a module ` example.js ` :
39
81
40
82
``` js
41
- import {readSync } from ' to-vfile'
83
+ import {read } from ' to-vfile'
42
84
import {remark } from ' remark'
43
85
import {zone } from ' mdast-zone'
44
86
45
- const file = readSync (' example.md' )
46
-
47
- remark ()
48
- .use (plugin)
49
- .process (file)
50
- .then ((file ) => {
51
- console .log (String (file))
52
- })
87
+ const file = await remark ()
88
+ .use (myPluginThatReplacesFoo)
89
+ .process (await read (' example.md' ))
53
90
54
- function plugin () {
55
- return transform
91
+ console .log (String (file))
56
92
57
- function transform (tree ) {
58
- zone (tree, ' foo' , mutate)
59
- }
60
-
61
- function mutate (start , nodes , end ) {
62
- return [
93
+ /** @type {import('unified').Plugin<[], import('mdast').Root>} */
94
+ function myPluginThatReplacesFoo () {
95
+ return (tree ) => {
96
+ zone (tree, ' foo' , (start , nodes , end ) => [
63
97
start,
64
- {type: ' paragraph' , children: [{type: ' text' , value: ' Bar' }]},
98
+ {type: ' paragraph' , children: [{type: ' text' , value: ' Bar. ' }]},
65
99
end
66
- ]
100
+ ])
67
101
}
68
102
}
69
103
```
70
104
71
- Now, running ` node example ` yields:
105
+ …now running ` node example.js ` yields:
72
106
73
107
``` markdown
74
108
<!--foo start-->
80
114
81
115
## API
82
116
83
- This package exports the following identifiers: ` zone ` .
117
+ This package exports the identifier ` zone ` .
84
118
There is no default export.
85
119
86
120
### ` zone(tree, name, handler) `
87
121
88
- Search ` tree ` for comment ranges (“zones”).
122
+ Search ` tree ` ([ ` Node ` ] [ node ] ) for comments marked ` name ` (` string ` ) and
123
+ transform a section with ` handler ` ([ ` Function ` ] [ handler ] ).
124
+
125
+ #### ` function handler(start, nodes, end, info) `
126
+
127
+ Callback called when a range is found.
89
128
90
- ###### Parameters
129
+ ##### Parameters
91
130
92
- * ` tree ` ([ ` Node ` ] [ node ] ) — [ Tree] [ ] to search for ranges
93
- * ` name ` (` string ` ) — Name of ranges to search for
94
- * ` handler ` ([ ` Function ` ] [ handler ] ) — Function invoked for each found range
131
+ Arguments.
95
132
96
- #### ` function handler( start, nodes, end) `
133
+ ###### ` start `
97
134
98
- Invoked with the two markers that determine a range: the first ` start `
99
- and the last ` end ` , and the content inside.
135
+ Start of range ([ ` Node ` ] [ node ] ), an [ HTML] [ ] (or MDX) comment node.
100
136
101
- ###### Parameters
137
+ ###### ` nodes `
102
138
103
- * ` start ` ([ ` Node ` ] [ node ] ) — Start of range (an [ HTML] [ ] comment node)
104
- * ` nodes ` ([ ` Array<Node> ` ] [ node ] ) — Nodes between ` start ` and ` end `
105
- * ` end ` ([ ` Node ` ] [ node ] ) — End of range (an [ HTML] [ ] comment node)
139
+ Nodes between ` start ` and ` end ` ([ ` Array<Node> ` ] [ node ] ).
140
+
141
+ ###### ` end `
142
+
143
+ End of range ([ ` Node ` ] [ node ] ), an [ HTML] [ ] (or MDX) comment node.
144
+
145
+ ###### ` info `
146
+
147
+ Extra info (` Object ` ):
148
+
149
+ * ` parent ` ([ ` Node ` ] [ node ] ) — parent of the range
150
+ * ` start ` (` number ` ) — index of ` start ` in ` parent `
151
+ * ` end ` (` number ` ) — index of ` end ` in ` parent `
106
152
107
153
###### Returns
108
154
109
- [ ` Array<Node>? ` ] [ node ] — List of nodes to replace ` start ` , ` nodes ` , and ` end `
110
- with, optional.
155
+ Optional list of nodes to replace ` start ` , ` nodes ` , and ` end ` with
156
+ ([ ` Array<Node>? ` ] [ node ] ).
157
+
158
+ ## Types
159
+
160
+ This package is fully typed with [ TypeScript] [ ] .
161
+ This package exports the types ` Handler ` and ` ZoneInfo ` .
162
+
163
+ ## Compatibility
164
+
165
+ Projects maintained by the unified collective are compatible with all maintained
166
+ versions of Node.js.
167
+ As of now, that is Node.js 12.20+, 14.14+, and 16.0+.
168
+ Our projects sometimes work with older versions, but this is not guaranteed.
111
169
112
170
## Security
113
171
114
172
Improper use of ` handler ` can open you up to a [ cross-site scripting (XSS)] [ xss ]
115
173
attack as the value it returns is injected into the syntax tree.
116
- This can become a problem if the tree is later transformed to [ ** hast** ] [ hast ] .
174
+ This can become a problem if the tree is later transformed to ** [ hast] [ ] ** .
117
175
The following example shows how a script is injected that could run when loaded
118
176
in a browser.
119
177
120
178
``` js
121
179
function handler (start , nodes , end ) {
122
- return [start, {type: ' html' , value: ' alert(1)' }, end]
180
+ return [start, {type: ' html' , value: ' <script> alert(1)</script> ' }, end]
123
181
}
124
182
```
125
183
@@ -133,17 +191,17 @@ Yields:
133
191
<!--foo end-->
134
192
```
135
193
136
- Either do not use user input or use [ ` hast-util-santize ` ] [ sanitize ] .
194
+ Either do not use user input or use [ ` hast-util-santize ` ] [ hast-util- sanitize] .
137
195
138
196
## Related
139
197
140
198
* [ ` mdast-util-heading-range ` ] ( https://github.com/syntax-tree/mdast-util-heading-range )
141
- — use headings as ranges instead of comments
199
+ — similar but uses headings to mark sections
142
200
143
201
## Contribute
144
202
145
- See [ ` contributing.md ` in ` syntax-tree/.github ` ] [ contributing ] for ways to get
146
- started.
203
+ See [ ` contributing.md ` ] [ contributing ] in [ ` syntax-tree/.github ` ] [ health ] for
204
+ ways to get started.
147
205
See [ ` support.md ` ] [ support ] for ways to get help.
148
206
149
207
This project has a [ code of conduct] [ coc ] .
@@ -184,30 +242,36 @@ abide by its terms.
184
242
185
243
[ npm ] : https://docs.npmjs.com/cli/install
186
244
245
+ [ esm ] : https://gist.github.com/sindresorhus/a39789f98801d908bbc7ff3ecc99d99c
246
+
247
+ [ esmsh ] : https://esm.sh
248
+
249
+ [ typescript ] : https://www.typescriptlang.org
250
+
187
251
[ license ] : license
188
252
189
253
[ author ] : https://wooorm.com
190
254
191
- [ contributing ] : https://github.com/syntax-tree/.github/blob/HEAD/contributing.md
255
+ [ health ] : https://github.com/syntax-tree/.github
192
256
193
- [ support ] : https://github.com/syntax-tree/.github/blob/HEAD/support .md
257
+ [ contributing ] : https://github.com/syntax-tree/.github/blob/main/contributing .md
194
258
195
- [ coc ] : https://github.com/syntax-tree/.github/blob/HEAD/code-of-conduct .md
259
+ [ support ] : https://github.com/syntax-tree/.github/blob/main/support .md
196
260
197
- [ mdast ] : https://github.com/syntax-tree/mdast
261
+ [ coc ] : https://github.com/syntax-tree/.github/blob/main/code-of-conduct.md
198
262
199
- [ remark ] : https://github.com/remarkjs/remark
263
+ [ xss ] : https://en.wikipedia.org/wiki/Cross-site_scripting
200
264
201
- [ handler ] : #function-handlerstart-nodes-end
265
+ [ mdast ] : https://github.com/syntax-tree/mdast
202
266
203
267
[ node ] : https://github.com/syntax-tree/mdast#nodes
204
268
205
- [ tree ] : https://github.com/syntax-tree/unist#tree
206
-
207
269
[ html ] : https://github.com/syntax-tree/mdast#html
208
270
209
- [ xss ] : https://en.wikipedia.org/wiki/Cross-site_scripting
271
+ [ mdast-util-heading-range ] : https://github.com/syntax-tree/mdast-util-heading-range
210
272
211
273
[ hast ] : https://github.com/syntax-tree/hast
212
274
213
- [ sanitize ] : https://github.com/syntax-tree/hast-util-sanitize
275
+ [ hast-util-sanitize ] : https://github.com/syntax-tree/hast-util-sanitize
276
+
277
+ [ handler ] : #function-handlerstart-nodes-end-info
0 commit comments