Skip to content

Commit 0c9dd6a

Browse files
committed
Add improved docs
1 parent aa76781 commit 0c9dd6a

File tree

4 files changed

+154
-58
lines changed

4 files changed

+154
-58
lines changed

index.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
/**
22
* @typedef {import('./lib/state.js').Handle} Handle
3+
* @typedef {import('./lib/state.js').Options} Options
34
* @typedef {import('./lib/state.js').Space} Space
4-
* @typedef {import('./lib/index.js').Options} Options
5+
* @typedef {import('./lib/state.js').State} State
56
*/
67

78
export {handlers as defaultHandlers} from './lib/handlers/index.js'

lib/index.js

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,12 +26,33 @@ import {createState} from './state.js'
2626
/**
2727
* Transform a hast tree (with embedded MDX nodes) into an estree.
2828
*
29+
* ###### Notes
30+
*
31+
* Comments are attached to the tree in their neighbouring nodes (`recast`,
32+
* `babel` style) and also added as a `comments` array on the program node
33+
* (`espree` style).
34+
* You may have to do `program.comments = undefined` for certain compilers.
35+
*
2936
* @param {Node} tree
3037
* hast tree.
3138
* @param {Options | null | undefined} [options]
3239
* Configuration.
3340
* @returns {Program}
3441
* estree program node.
42+
*
43+
* The program’s last child in `body` is most likely an `ExpressionStatement`,
44+
* whose expression is a `JSXFragment` or a `JSXElement`.
45+
*
46+
* Typically, there is only one node in `body`, however, this utility also
47+
* supports embedded MDX nodes in the HTML (when `mdast-util-mdx` is used
48+
* with mdast to parse markdown before passing its nodes through to hast).
49+
* When MDX ESM import/exports are used, those nodes are added before the
50+
* fragment or element in body.
51+
*
52+
* There aren’t many great estree serializers out there that support JSX.
53+
* To do that, you can use `estree-util-to-js`.
54+
* Or, use `estree-util-build-jsx` to turn JSX into function calls, and then
55+
* serialize with whatever (`astring`, `escodegen`).
3556
*/
3657
export function toEstree(tree, options) {
3758
const state = createState(options || {})

lib/state.js

Lines changed: 4 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -63,13 +63,13 @@
6363
* @property {Schema} schema
6464
* Current schema.
6565
* @property {Array<Comment>} comments
66-
* List of comments.
66+
* List of estree comments.
6767
* @property {Array<Directive | Statement | ModuleDeclaration>} esm
68-
* List of top-level content.
68+
* List of top-level estree nodes.
6969
* @property {(node: any) => JsxChild | null | undefined | void} handle
70-
* Transform a hast node into estree.
70+
* Transform a hast node to estree.
7171
* @property {(parent: Parent) => Array<JsxChild>} all
72-
* Transform children of a hast parent into estree.
72+
* Transform children of a hast parent to estree.
7373
* @property {(from: Node, to: EstreeNode | Comment) => void} patch
7474
* Take positional info from `from` (use `inherit` if you also want data).
7575
* @property {(from: Node, to: EstreeNode | Comment) => void} inherit
@@ -78,29 +78,6 @@
7878
* Create a JSX attribute name.
7979
* @property {(name: string) => JsxElementName} createJsxElementName
8080
* Create a JSX element name.
81-
*
82-
* @typedef Member
83-
* `<a.b.c />`
84-
* @property {'member'} type
85-
* Kind.
86-
* @property {Array<string>} names
87-
* All names.
88-
*
89-
* @typedef Namespace
90-
* `<a:b />`
91-
* @property {'namespace'} type
92-
* Kind.
93-
* @property {string} namespace
94-
* Namespace.
95-
* @property {string} name
96-
* Name.
97-
*
98-
* @typedef Name
99-
* `<a />`
100-
* @property {'name'} type
101-
* Kind.
102-
* @property {string} name
103-
* Name.
10481
*/
10582

10683
import {html, svg} from 'property-information'

readme.md

Lines changed: 127 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,12 @@
1717
* [Install](#install)
1818
* [Use](#use)
1919
* [API](#api)
20-
* [`toEstree(tree, options?)`](#toestreetree-options)
20+
* [`toEstree(tree[, options])`](#toestreetree-options)
21+
* [`defaultHandlers`](#defaulthandlers)
22+
* [`Handle`](#handle)
23+
* [`Options`](#options)
24+
* [`Space`](#space-1)
25+
* [`State`](#state)
2126
* [Types](#types)
2227
* [Compatibility](#compatibility)
2328
* [Security](#security)
@@ -40,7 +45,7 @@ This is used in [MDX][].
4045
## Install
4146

4247
This package is [ESM only][esm].
43-
In Node.js (version 12.20+, 14.14+, 16.0+, 18.0+), install with [npm][]:
48+
In Node.js (version 14.14+ and 16.0+), install with [npm][]:
4449

4550
```sh
4651
npm install hast-util-to-estree
@@ -141,63 +146,139 @@ console.log(toJs(estree, {handlers: jsx}).value)
141146

142147
## API
143148

144-
This package exports the identifiers `defaultHandlers` and `toEstree`.
149+
This package exports the identifiers [`defaultHandlers`][defaulthandlers] and
150+
[`toEstree`][toestree].
145151
There is no default export.
146152

147-
### `toEstree(tree, options?)`
153+
### `toEstree(tree[, options])`
148154

149-
Transform to [estree][] (JSX).
155+
Transform a hast tree (with embedded MDX nodes) into an estree (with JSX
156+
nodes).
150157

151-
##### `options`
158+
###### Notes
152159

153-
Configuration (optional).
160+
Comments are attached to the tree in their neighbouring nodes (`recast`,
161+
`babel` style) and also added as a `comments` array on the program node
162+
(`espree` style).
163+
You may have to do `program.comments = undefined` for certain compilers.
154164

155-
##### `options.space`
165+
###### Parameters
156166

157-
Whether `tree` is in the HTML or SVG space (enum, `'svg'` or `'html'`, default:
158-
`'html'`).
159-
If an `svg` element is found when inside the HTML space, `toEstree`
160-
automatically switches to the SVG space when entering the element, and
161-
switches back when exiting
162-
163-
###### `options.handlers`
164-
165-
Object mapping node types to functions handling the corresponding nodes.
166-
See the code for examples.
167+
* `tree` ([`HastNode`][hast-node])
168+
— hast tree
169+
* `options` ([`Options`][options], optional)
170+
— configuration
167171

168172
###### Returns
169173

170-
Node ([`Program`][program]) whose last child in `body` is most likely an
171-
`ExpressionStatement`, whose expression is a `JSXFragment` or a `JSXElement`.
174+
estree program node ([`Program`][program]).
175+
176+
The program’s last child in `body` is most likely an `ExpressionStatement`,
177+
whose expression is a `JSXFragment` or a `JSXElement`.
172178

173179
Typically, there is only one node in `body`, however, this utility also supports
174180
embedded MDX nodes in the HTML (when [`mdast-util-mdx`][mdast-util-mdx] is used
175181
with mdast to parse markdown before passing its nodes through to hast).
176182
When MDX ESM import/exports are used, those nodes are added before the fragment
177183
or element in body.
178184

179-
###### Note
180-
181-
Comments are both attached to the tree in their neighbouring nodes (recast and
182-
babel style), and added as a `comments` array on the program node (espree
183-
style).
184-
You may have to do `program.comments = null` for certain compilers.
185-
186185
There aren’t many great estree serializers out there that support JSX.
187186
To do that, you can use [`estree-util-to-js`][estree-util-to-js].
188187
Or, use [`estree-util-build-jsx`][build-jsx] to turn JSX into function
189-
calls, and then serialize with whatever (astring, escodegen).
188+
calls, and then serialize with whatever (`astring`, `escodegen`).
189+
190+
### `defaultHandlers`
191+
192+
Default handlers for elements (`Record<string, Handle>`).
193+
194+
Each key is an element name, each value is a [`Handle`][handle].
195+
196+
### `Handle`
197+
198+
Turn a hast node into an estree node (TypeScript type).
199+
200+
###### Parameters
201+
202+
* `node` ([`HastNode`][hast-node])
203+
— expected hast node
204+
* `state` ([`State`][state])
205+
— info passed around about the current state
206+
207+
###### Returns
208+
209+
JSX child (`JsxChild`, optional).
210+
211+
You can also add more results to `state.esm` and `state.comments`.
212+
213+
### `Options`
214+
215+
Configuration (TypeScript type).
216+
217+
##### Fields
218+
219+
###### `space`
220+
221+
Which space the document is in ([`Space`][space], default: `'html'`).
222+
223+
When an `<svg>` element is found in the HTML space, this package already
224+
automatically switches to and from the SVG space when entering and exiting
225+
it.
226+
227+
###### `handlers`
228+
229+
Object mapping node types to functions handling the corresponding nodes
230+
(`Record<string, Handle>`).
231+
232+
Merged into the defaults.
233+
See [`Handle`][handle].
234+
235+
### `Space`
236+
237+
Namespace (TypeScript type).
238+
239+
###### Type
240+
241+
```ts
242+
type Space = 'html' | 'svg'
243+
```
244+
245+
### `State`
246+
247+
Info passed around about the current state (TypeScript type).
248+
249+
###### Fields
250+
251+
* `schema` ([`Schema`][schema])
252+
— current schema
253+
* `comments` (`Array<Comment>`)
254+
— list of estree comments
255+
* `esm` (`Array<Node>`)
256+
— list of top-level estree nodes
257+
* `handle` (`(node: Node) => JsxChild | void`)
258+
— transform a hast node to estree
259+
* `handle` (`(node: Parent) => JsxChild | void`)
260+
— transform children of a hast parent to estree
261+
* `patch` (`(from: HastNode, to: EstreeNode) => void`)
262+
— take positional info from `from` (use `inherit` if you also want data)
263+
* `inherit` (`(from: HastNode, to: EstreeNode) => void`)
264+
— take positional info and data from `from` (use `patch` if you don’t want
265+
data)
266+
* `createJsxAttributeName` (`(name: string) => JsxAttributeName`)
267+
— create a JSX attribute name
268+
* `createJsxElementName` (`(name: string) => JsxElementName`)
269+
— create a JSX attribute name
190270
191271
## Types
192272
193273
This package is fully typed with [TypeScript][].
194-
It exports the additional types `Options`, `Space`, and `Handle`.
274+
It exports the additional types [`Handle`][handle], [`Options`][options],
275+
[`Space`][space], and [`State`][state].
195276
196277
## Compatibility
197278
198279
Projects maintained by the unified collective are compatible with all maintained
199280
versions of Node.js.
200-
As of now, that is Node.js 12.20+, 14.14+, 16.0+, and 18.0+.
281+
As of now, that is Node.js 14.14+ and 16.0+.
201282
Our projects sometimes work with older versions, but this is not guaranteed.
202283
203284
## Security
@@ -280,6 +361,8 @@ abide by its terms.
280361
281362
[hast]: https://github.com/syntax-tree/hast
282363
364+
[hast-node]: https://github.com/syntax-tree/hast#nodes
365+
283366
[estree]: https://github.com/estree/estree
284367
285368
[program]: https://github.com/estree/estree/blob/master/es5.md#programs
@@ -290,4 +373,18 @@ abide by its terms.
290373
291374
[build-jsx]: https://github.com/wooorm/estree-util-build-jsx
292375
376+
[schema]: https://github.com/wooorm/property-information#api
377+
293378
[mdx]: https://mdxjs.com
379+
380+
[defaulthandlers]: #defaulthandlers
381+
382+
[toestree]: #toestreetree-options
383+
384+
[space]: #space-1
385+
386+
[options]: #options
387+
388+
[handle]: #handle
389+
390+
[state]: #state

0 commit comments

Comments
 (0)