Skip to content

Commit 00413a1

Browse files
committed
Add improved docs
1 parent ed996fe commit 00413a1

File tree

1 file changed

+57
-33
lines changed

1 file changed

+57
-33
lines changed

readme.md

+57-33
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
[![Backers][backers-badge]][collective]
99
[![Chat][chat-badge]][chat]
1010

11-
[hast][] utility to transform from [`parse5`][parse5]s [AST][].
11+
[hast][] utility to transform from [`parse5`][parse5]s AST.
1212

1313
## Contents
1414

@@ -17,7 +17,9 @@
1717
* [Install](#install)
1818
* [Use](#use)
1919
* [API](#api)
20-
* [`fromParse5(ast[, file|options])`](#fromparse5ast-fileoptions)
20+
* [`fromParse5(tree[, file|options])`](#fromparse5tree-fileoptions)
21+
* [`Options`](#options)
22+
* [`Space`](#space-1)
2123
* [Types](#types)
2224
* [Compatibility](#compatibility)
2325
* [Security](#security)
@@ -44,7 +46,7 @@ The utility [`hast-util-from-html`][hast-util-from-html] wraps this utility and
4446
## Install
4547

4648
This package is [ESM only][esm].
47-
In Node.js (version 12.20+, 14.14+, 16.0+, 18.0+), install with [npm][]:
49+
In Node.js (version 14.14+ and 16.0+), install with [npm][]:
4850

4951
```sh
5052
npm install hast-util-from-parse5
@@ -113,38 +115,52 @@ root[2] (1:1-2:1, 0-70)
113115

114116
## API
115117

116-
This package exports the identifier `fromParse5`.
118+
This package exports the identifier [`fromParse5`][fromparse5].
117119
There is no default export.
118120

119-
### `fromParse5(ast[, file|options])`
121+
### `fromParse5(tree[, file|options])`
120122

121-
Transform from [`parse5`][parse5]s [AST][].
123+
Transform a `parse5` AST to hast.
122124

123-
##### `options`
125+
###### Parameters
124126

125-
If `options` is a [`VFile`][vfile], it’s treated as `{file: options}`.
127+
* `tree` ([`Parse5Node`][parse5-node])
128+
`parse5` tree to transform
129+
* `file` ([`VFile`][vfile], optional)
130+
— corresponding file (treated as `{file: file}`)
131+
* `options` ([`Options`][options], optional)
132+
— configuration
126133

127-
###### `options.space`
134+
###### Returns
128135

129-
Whether the [*root*][root] of the [*tree*][tree] is in the HTML or SVG space
130-
(enum, `'svg'` or `'html'`, default: `'html'`).
136+
hast tree ([`HastNode`][hast-node]).
131137

132-
If an element in with the SVG namespace is found in `ast`, `fromParse5`
133-
automatically switches to the SVG space when entering the element, and switches
134-
back when leaving.
138+
### `Options`
135139

136-
###### `options.file`
140+
Configuration (TypeScript type).
137141

138-
[`VFile`][vfile], used to add [positional information][positional-information]
139-
to [*nodes*][node].
140-
If given, the [*file*][file] should have the original HTML source as its
141-
contents.
142+
##### Fields
142143

143-
###### `options.verbose`
144+
###### `space`
144145

145-
Whether to add extra positional information about starting tags, closing tags,
146+
Which space the document is in ([`Space`][space], default: `'html'`).
147+
148+
When an `<svg>` element is found in the HTML space, this package already
149+
automatically switches to and from the SVG space when entering and exiting
150+
it.
151+
152+
###### `file`
153+
154+
File used to add positional info to nodes ([`VFile`][vfile], optional).
155+
156+
If given, the file should represent the original HTML source.
157+
158+
###### `verbose`
159+
160+
Whether to add extra positional info about starting tags, closing tags,
146161
and attributes to elements (`boolean`, default: `false`).
147-
Note: not used without `file`.
162+
163+
> 👉 **Note**: only used when `file` is given.
148164
149165
For the following HTML:
150166

@@ -190,10 +206,20 @@ The verbose info would looks as follows:
190206
}
191207
```
192208

209+
### `Space`
210+
211+
Namespace (TypeScript type).
212+
213+
###### Type
214+
215+
```ts
216+
type Space = 'html' | 'svg'
217+
```
218+
193219
## Types
194220
195221
This package is fully typed with [TypeScript][].
196-
It exports the additional type `Options`.
222+
It exports the additional types [`Options`][options] and [`Space`][space].
197223
198224
## Compatibility
199225
@@ -288,22 +314,20 @@ abide by its terms.
288314
289315
[parse5]: https://github.com/inikulin/parse5
290316
291-
[ast]: https://github.com/inikulin/parse5/blob/HEAD/packages/parse5/docs/tree-adapter/default/interface-list.md
317+
[parse5-node]: https://github.com/inikulin/parse5/blob/master/packages/parse5/lib/tree-adapters/default.ts
292318
293319
[vfile]: https://github.com/vfile/vfile
294320
295-
[tree]: https://github.com/syntax-tree/unist#tree
296-
297-
[root]: https://github.com/syntax-tree/unist#root
298-
299-
[positional-information]: https://github.com/syntax-tree/unist#positional-information
300-
301321
[hast-util-to-parse5]: https://github.com/syntax-tree/hast-util-to-parse5
302322
303-
[file]: https://github.com/syntax-tree/unist#file
304-
305323
[hast]: https://github.com/syntax-tree/hast
306324
307325
[hast-util-from-html]: https://github.com/syntax-tree/hast-util-from-html
308326
309-
[node]: https://github.com/syntax-tree/hast#nodes
327+
[hast-node]: https://github.com/syntax-tree/hast#nodes
328+
329+
[fromparse5]: #fromparse5tree-fileoptions
330+
331+
[options]: #options
332+
333+
[space]: #space-1

0 commit comments

Comments
 (0)