Skip to content

Commit f976ab4

Browse files
committed
Add improved docs
1 parent 192f4f9 commit f976ab4

File tree

2 files changed

+71
-39
lines changed

2 files changed

+71
-39
lines changed

lib/index.js

+13-14
Original file line numberDiff line numberDiff line change
@@ -11,25 +11,24 @@
1111
* @typedef {HastChild | HastRoot} HastNode
1212
*
1313
* @callback AfterTransform
14-
* Function called when a hast node is transformed into a DOM node
14+
* Callback called when each node is transformed.
1515
* @param {HastNode} hastNode
16-
* The hast node that was handled
16+
* hast node that was handled.
1717
* @param {Node} domNode
18-
* The corresponding DOM node
18+
* Corresponding DOM node.
1919
* @returns {void}
20+
* Nothing.
2021
*
2122
* @typedef Options
22-
* Configuration (optional).
23-
* @property {boolean | null | undefined} [fragment=false]
24-
* Whether to return a DOM fragment.
25-
*
26-
* A whole document is built otherwise.
23+
* Configuration.
24+
* @property {AfterTransform | null | undefined} [afterTransform]
25+
* Callback called when each node is transformed.
2726
* @property {Document | null | undefined} [document]
2827
* Document interface to use (default: `globalThis.document`).
28+
* @property {boolean | null | undefined} [fragment=false]
29+
* Whether to return a DOM fragment (`true`) or a whole document (`false`).
2930
* @property {string | null | undefined} [namespace]
30-
* `namespace` to use to create elements.
31-
* @property {AfterTransform | null | undefined} [afterTransform]
32-
* Callback called after each hast node is transformed.
31+
* Namespace to use to create elements.
3332
*
3433
* @typedef State
3534
* Info passed around about the current state.
@@ -55,16 +54,16 @@ const own = {}.hasOwnProperty
5554
/**
5655
* Transform a hast tree to a DOM tree
5756
*
58-
* @param {HastNode} node
57+
* @param {HastNode} tree
5958
* Tree to transform.
6059
* @param {Options | null | undefined} [options]
6160
* Configuration (optional).
6261
* @returns {XMLDocument | DocumentFragment | Text | DocumentType | Comment | Element}
6362
* Equivalent DOM node.
6463
*/
65-
export function toDom(node, options) {
64+
export function toDom(tree, options) {
6665
const config = options || {}
67-
return transform(node, {
66+
return transform(tree, {
6867
doc: config.document || document,
6968
fragment: config.fragment || false,
7069
namespace: config.namespace || undefined,

readme.md

+58-25
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,10 @@
1717
* [Install](#install)
1818
* [Use](#use)
1919
* [API](#api)
20-
* [`toDom(node[, options])`](#todomnode-options)
20+
* [`toDom(tree[, options])`](#todomtree-options)
21+
* [`AfterTransform`](#aftertransform)
22+
* [`Options`](#options)
23+
* [Syntax tree](#syntax-tree)
2124
* [Types](#types)
2225
* [Compatibility](#compatibility)
2326
* [Security](#security)
@@ -47,7 +50,7 @@ utility to serialize as HTML with DOM APIs.
4750
## Install
4851

4952
This package is [ESM only][esm].
50-
In Node.js (version 12.20+, 14.14+, or 16.0+), install with [npm][]:
53+
In Node.js (version 14.14+ or 16.0+), install with [npm][]:
5154

5255
```sh
5356
npm install hast-util-to-dom
@@ -88,42 +91,62 @@ Say our page `example.html` looks as follows:
8891
</script>
8992
```
9093

91-
Now running `open example.html` shows the equivalent HTML on the page.
94+
Now running `open example.html` shows the `main`, `h1`, and `p` elements on the
95+
page.
9296

9397
## API
9498

95-
This package exports the identifier `toDom`.
99+
This package exports the identifier [`toDom`][to-dom].
96100
There is no default export.
97101

98-
### `toDom(node[, options])`
102+
### `toDom(tree[, options])`
99103

100104
Turn a hast tree into a DOM tree.
101105

102-
##### `options`
106+
###### Parameters
103107

104-
Configuration (optional).
108+
* `tree` ([`HastNode`][hast-node])
109+
— tree to transform
110+
* `options` ([`Options`][options], optional)
111+
— configuration
105112

106-
###### `options.fragment`
113+
###### Returns
107114

108-
Return a DOM fragment (`boolean`, default: `false`).
109-
Creates whole documents otherwise.
115+
DOM node ([`DomNode`][dom-node]).
110116

111-
###### `options.document`
117+
### `AfterTransform`
112118

113-
Document interface to use (`Document`, default: `globalThis.document`).
119+
Callback called when each node is transformed (TypeScript type).
114120

115-
###### `options.namespace`
121+
###### Parameters
116122

117-
`namespace` to use to create elements (`string?`, optional).
123+
* `hastNode` ([`HastNode`][hast-node])
124+
— hast node that was handled
125+
* `domNode` ([`DomNode`][dom-node])
126+
— corresponding DOM node
118127

119-
###### `options.afterTransform`
128+
###### Returns
120129

121-
Called when a hast node was transformed into a DOM node
122-
(`(HastNode, Node) => void?`, optional).
130+
Nothing.
123131

124-
##### Returns
132+
### `Options`
125133

126-
[`Node`][dom].
134+
Configuration (TypeScript type).
135+
136+
###### Fields
137+
138+
* `afterTransform` ([`AfterTransform`][aftertransform], optional)
139+
— callback called when each node is transformed
140+
* `document` (`Document`, default: `globalThis.document`)
141+
— document interface to use.
142+
* `fragment` (`boolean`, default: `false`)
143+
— whether to return a DOM fragment (`true`) or a whole document (`false`)
144+
* `namespace` (`string`, default: depends)
145+
— namespace to use to create elements
146+
147+
## Syntax tree
148+
149+
The syntax tree is [hast][].
127150

128151
## Types
129152

@@ -134,7 +157,7 @@ It exports the additional types `AfterTransform` and `Options`.
134157

135158
Projects maintained by the unified collective are compatible with all maintained
136159
versions of Node.js.
137-
As of now, that is Node.js 12.20+, 14.14+, and 16.0+.
160+
As of now, that is Node.js 14.14+ and 16.0+.
138161
Our projects sometimes work with older versions, but this is not guaranteed.
139162

140163
## Security
@@ -159,7 +182,7 @@ ways to get started.
159182
See [`support.md`][support] for ways to get help.
160183

161184
This project has a [code of conduct][coc].
162-
By interacting with this repository, organisation, or community you agree to
185+
By interacting with this repository, organization, or community you agree to
163186
abide by its terms.
164187

165188
## License
@@ -214,10 +237,6 @@ abide by its terms.
214237

215238
[coc]: https://github.com/syntax-tree/.github/blob/main/code-of-conduct.md
216239

217-
[hast]: https://github.com/syntax-tree/hast
218-
219-
[dom]: https://developer.mozilla.org/docs/Web/API/Document_Object_Model
220-
221240
[xss]: https://en.wikipedia.org/wiki/Cross-site_scripting
222241

223242
[hast-util-sanitize]: https://github.com/syntax-tree/hast-util-sanitize
@@ -227,3 +246,17 @@ abide by its terms.
227246
[jsdom]: https://github.com/jsdom/jsdom
228247

229248
[rehype-dom-stringify]: https://github.com/rehypejs/rehype-dom/tree/main/packages/rehype-dom-stringify
249+
250+
[hast]: https://github.com/syntax-tree/hast
251+
252+
[hast-node]: https://github.com/syntax-tree/hast#nodes
253+
254+
[dom]: https://developer.mozilla.org/docs/Web/API/Document_Object_Model
255+
256+
[dom-node]: https://developer.mozilla.org/docs/Web/API/Node
257+
258+
[to-dom]: #todomtree-options
259+
260+
[aftertransform]: #aftertransform
261+
262+
[options]: #options

0 commit comments

Comments
 (0)