Skip to content

Commit a1138bc

Browse files
committed
Add improved docs
1 parent cec1f76 commit a1138bc

File tree

2 files changed

+136
-59
lines changed

2 files changed

+136
-59
lines changed

index.js

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,25 +3,38 @@
33
* @typedef {Extract<Node, import('mdast').Parent>} Parent
44
*
55
* @typedef ZoneInfo
6+
* Extra info.
7+
* @property {Parent} parent
8+
* Parent of the range.
69
* @property {number} start
10+
* Index of `start` in `parent`
711
* @property {number} end
8-
* @property {Parent|null} parent
12+
* Index of `end` in `parent`
913
*
1014
* @callback Handler
15+
* Callback called when a range is found.
1116
* @param {Node} start
17+
* Start of range.
1218
* @param {Array<Node>} between
19+
* Nodes between `start` and `end`.
1320
* @param {Node} end
21+
* End of range.
1422
* @param {ZoneInfo} info
23+
* Extra info.
1524
* @returns {Array<Node>|null|undefined|void}
25+
* Nodes to replace.
1626
*/
1727

1828
import {commentMarker} from 'mdast-comment-marker'
1929
import {visit} from 'unist-util-visit'
2030

2131
/**
2232
* @param {Node} node
33+
* Tree to search.
2334
* @param {string} name
35+
* Comment name to look for.
2436
* @param {Handler} callback
37+
* Callback called when a range is found.
2538
*/
2639
export function zone(node, name, callback) {
2740
/** @type {number|undefined} */

readme.md

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

11-
[**mdast**][mdast] utility to treat HTML comments as ranges.
11+
[mdast][] utility to find two comments and replace the content in them.
1212

13-
Useful in [**remark**][remark] plugins.
13+
## Contents
1414

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.
1641

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.
1944

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][]:
2149

2250
```sh
2351
npm install mdast-zone
2452
```
2553

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+
2668
## Use
2769

2870
Say we have the following file, `example.md`:
@@ -35,40 +77,32 @@ Foo
3577
<!--foo end-->
3678
```
3779

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

4082
```js
41-
import {readSync} from 'to-vfile'
83+
import {read} from 'to-vfile'
4284
import {remark} from 'remark'
4385
import {zone} from 'mdast-zone'
4486

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'))
5390

54-
function plugin() {
55-
return transform
91+
console.log(String(file))
5692

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) => [
6397
start,
64-
{type: 'paragraph', children: [{type: 'text', value: 'Bar'}]},
98+
{type: 'paragraph', children: [{type: 'text', value: 'Bar.'}]},
6599
end
66-
]
100+
])
67101
}
68102
}
69103
```
70104

71-
Now, running `node example` yields:
105+
…now running `node example.js` yields:
72106

73107
```markdown
74108
<!--foo start-->
@@ -80,46 +114,70 @@ Bar
80114

81115
## API
82116

83-
This package exports the following identifiers: `zone`.
117+
This package exports the identifier `zone`.
84118
There is no default export.
85119

86120
### `zone(tree, name, handler)`
87121

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.
89128

90-
###### Parameters
129+
##### Parameters
91130

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.
95132

96-
#### `function handler(start, nodes, end)`
133+
###### `start`
97134

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.
100136

101-
###### Parameters
137+
###### `nodes`
102138

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`
106152

107153
###### Returns
108154

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.
111169

112170
## Security
113171

114172
Improper use of `handler` can open you up to a [cross-site scripting (XSS)][xss]
115173
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][]**.
117175
The following example shows how a script is injected that could run when loaded
118176
in a browser.
119177

120178
```js
121179
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]
123181
}
124182
```
125183

@@ -133,17 +191,17 @@ Yields:
133191
<!--foo end-->
134192
```
135193

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].
137195

138196
## Related
139197

140198
* [`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
142200

143201
## Contribute
144202

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.
147205
See [`support.md`][support] for ways to get help.
148206

149207
This project has a [code of conduct][coc].
@@ -184,30 +242,36 @@ abide by its terms.
184242

185243
[npm]: https://docs.npmjs.com/cli/install
186244

245+
[esm]: https://gist.github.com/sindresorhus/a39789f98801d908bbc7ff3ecc99d99c
246+
247+
[esmsh]: https://esm.sh
248+
249+
[typescript]: https://www.typescriptlang.org
250+
187251
[license]: license
188252

189253
[author]: https://wooorm.com
190254

191-
[contributing]: https://github.com/syntax-tree/.github/blob/HEAD/contributing.md
255+
[health]: https://github.com/syntax-tree/.github
192256

193-
[support]: https://github.com/syntax-tree/.github/blob/HEAD/support.md
257+
[contributing]: https://github.com/syntax-tree/.github/blob/main/contributing.md
194258

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
196260

197-
[mdast]: https://github.com/syntax-tree/mdast
261+
[coc]: https://github.com/syntax-tree/.github/blob/main/code-of-conduct.md
198262

199-
[remark]: https://github.com/remarkjs/remark
263+
[xss]: https://en.wikipedia.org/wiki/Cross-site_scripting
200264

201-
[handler]: #function-handlerstart-nodes-end
265+
[mdast]: https://github.com/syntax-tree/mdast
202266

203267
[node]: https://github.com/syntax-tree/mdast#nodes
204268

205-
[tree]: https://github.com/syntax-tree/unist#tree
206-
207269
[html]: https://github.com/syntax-tree/mdast#html
208270

209-
[xss]: https://en.wikipedia.org/wiki/Cross-site_scripting
271+
[mdast-util-heading-range]: https://github.com/syntax-tree/mdast-util-heading-range
210272

211273
[hast]: https://github.com/syntax-tree/hast
212274

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

Comments
 (0)