Skip to content

Commit 668b2a7

Browse files
committed
Add improved docs
1 parent c1b1971 commit 668b2a7

File tree

2 files changed

+57
-31
lines changed

2 files changed

+57
-31
lines changed

lib/index.js

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -39,14 +39,19 @@ import {commentMarker} from 'mdast-comment-marker'
3939
import {visit} from 'unist-util-visit'
4040

4141
/**
42+
* Search `tree` for a start and end comments matching `name` and change their
43+
* “section” with `handler`.
44+
*
4245
* @param {Node} node
4346
* Tree to search.
4447
* @param {string} name
4548
* Comment name to look for.
46-
* @param {Handler} callback
47-
* Callback called when a range is found.
49+
* @param {Handler} handler
50+
* Handle a section.
51+
* @returns {void}
52+
* Nothing.
4853
*/
49-
export function zone(node, name, callback) {
54+
export function zone(node, name, handler) {
5055
/** @type {number | undefined} */
5156
let level
5257
/** @type {Node | undefined} */
@@ -78,7 +83,7 @@ export function zone(node, name, callback) {
7883
// @ts-expect-error: Assume `scope` is a valid parent of `node`.
7984
const start = scope.children.indexOf(marker)
8085

81-
const nodes = callback(
86+
const nodes = handler(
8287
marker,
8388
scope.children.slice(start + 1, index),
8489
node,

readme.md

Lines changed: 48 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@
1818
* [Use](#use)
1919
* [API](#api)
2020
* [`zone(tree, name, handler)`](#zonetree-name-handler)
21+
* [`Handler`](#handler)
22+
* [`ZoneInfo`](#zoneinfo)
2123
* [Types](#types)
2224
* [Compatibility](#compatibility)
2325
* [Security](#security)
@@ -45,7 +47,7 @@ the same but uses a heading to mark the start and end of sections.
4547
## Install
4648

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

5052
```sh
5153
npm install mdast-zone
@@ -114,57 +116,74 @@ Bar.
114116

115117
## API
116118

117-
This package exports the identifier `zone`.
119+
This package exports the identifier [`zone`][api-zone].
118120
There is no default export.
119121

120122
### `zone(tree, name, handler)`
121123

122-
Search `tree` ([`Node`][node]) for comments marked `name` (`string`) and
123-
transform a section with `handler` ([`Function`][handler]).
124+
Search `tree` for a start and end comments matching `name` and change their
125+
section with `handler`.
124126

125-
#### `function handler(start, nodes, end, info)`
127+
###### Parameters
126128

127-
Callback called when a range is found.
129+
* `tree` ([`Node`][node])
130+
— tree to change
131+
* `name` (`string`)
132+
— comment name to look for
133+
* `handler` ([`Handler`][api-handler])
134+
— handle a section
128135

129-
##### Parameters
136+
###### Returns
130137

131-
Arguments.
138+
Nothing (`void`).
132139

133-
###### `start`
140+
### `Handler`
134141

135-
Start of range ([`Node`][node]), an [HTML][] (or MDX) comment node.
142+
Callback called when a section is found (TypeScript type).
136143

137-
###### `nodes`
144+
###### Parameters
138145

139-
Nodes between `start` and `end` ([`Array<Node>`][node]).
146+
* `start` ([`Node`][node])
147+
— start of section
148+
* `nodes` ([`Array<Node>`][node])
149+
— nodes between `start` and `end`
150+
* `end` ([`Node`][node])
151+
— end of section
152+
* `info` ([`ZoneInfo`][api-zoneinfo])
153+
— extra info
140154

141-
###### `end`
155+
###### Returns
142156

143-
End of range ([`Node`][node]), an [HTML][] (or MDX) comment node.
157+
Results (`Array<Node | null | undefined>`, optional).
144158

145-
###### `info`
159+
If nothing is returned, nothing will be changed.
160+
If an array of nodes (can include `null` and `undefined`) is returned, the
161+
original section will be replaced by those nodes.
146162

147-
Extra info (`Object`):
163+
### `ZoneInfo`
148164

149-
* `parent` ([`Node`][node]) — parent of the range
150-
* `start` (`number`) — index of `start` in `parent`
151-
* `end` (`number`) — index of `end` in `parent`
165+
Extra info (TypeScript type).
152166

153-
###### Returns
167+
###### Fields
154168

155-
Optional list of nodes to replace `start`, `nodes`, and `end` with
156-
([`Array<Node>?`][node]).
169+
* `parent` ([`Node`][node])
170+
— parent of the section
171+
* `start` (`number`)
172+
— index of `start` in `parent`
173+
* `end` (`number` or `null`)
174+
— index of `end` in `parent`
157175

158176
## Types
159177

160178
This package is fully typed with [TypeScript][].
161-
This package exports the types `Handler` and `ZoneInfo`.
179+
It exports the additional types [`Handler`][api-handler] and
180+
[`ZoneInfo`][api-zoneinfo].
162181

163182
## Compatibility
164183

165184
Projects maintained by the unified collective are compatible with all maintained
166185
versions of Node.js.
167-
As of now, that is Node.js 12.20+, 14.14+, and 16.0+.
186+
As of now, that is Node.js 14.14+ and 16.0+.
168187
Our projects sometimes work with older versions, but this is not guaranteed.
169188

170189
## Security
@@ -266,12 +285,14 @@ abide by its terms.
266285

267286
[node]: https://github.com/syntax-tree/mdast#nodes
268287

269-
[html]: https://github.com/syntax-tree/mdast#html
270-
271288
[mdast-util-heading-range]: https://github.com/syntax-tree/mdast-util-heading-range
272289

273290
[hast]: https://github.com/syntax-tree/hast
274291

275292
[hast-util-sanitize]: https://github.com/syntax-tree/hast-util-sanitize
276293

277-
[handler]: #function-handlerstart-nodes-end-info
294+
[api-zone]: #zonetree-name-handler
295+
296+
[api-handler]: #handler
297+
298+
[api-zoneinfo]: #zoneinfo

0 commit comments

Comments
 (0)