17
17
* [ Install] ( #install )
18
18
* [ Use] ( #use )
19
19
* [ 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 )
21
26
* [ Types] ( #types )
22
27
* [ Compatibility] ( #compatibility )
23
28
* [ Security] ( #security )
@@ -40,7 +45,7 @@ This is used in [MDX][].
40
45
## Install
41
46
42
47
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] [ ] :
44
49
45
50
``` sh
46
51
npm install hast-util-to-estree
@@ -141,63 +146,139 @@ console.log(toJs(estree, {handlers: jsx}).value)
141
146
142
147
## API
143
148
144
- This package exports the identifiers ` defaultHandlers ` and ` toEstree ` .
149
+ This package exports the identifiers [ ` defaultHandlers ` ] [ defaulthandlers ] and
150
+ [ ` toEstree ` ] [ toestree ] .
145
151
There is no default export.
146
152
147
- ### ` toEstree(tree, options? ) `
153
+ ### ` toEstree(tree[ , options] ) `
148
154
149
- Transform to [ estree] [ ] (JSX).
155
+ Transform a hast tree (with embedded MDX nodes) into an estree (with JSX
156
+ nodes).
150
157
151
- ##### ` options `
158
+ ###### Notes
152
159
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.
154
164
155
- ##### ` options.space `
165
+ ###### Parameters
156
166
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
167
171
168
172
###### Returns
169
173
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 ` .
172
178
173
179
Typically, there is only one node in ` body ` , however, this utility also supports
174
180
embedded MDX nodes in the HTML (when [ ` mdast-util-mdx ` ] [ mdast-util-mdx ] is used
175
181
with mdast to parse markdown before passing its nodes through to hast).
176
182
When MDX ESM import/exports are used, those nodes are added before the fragment
177
183
or element in body.
178
184
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
-
186
185
There aren’t many great estree serializers out there that support JSX.
187
186
To do that, you can use [ ` estree-util-to-js ` ] [ estree-util-to-js ] .
188
187
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
190
270
191
271
## Types
192
272
193
273
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].
195
276
196
277
## Compatibility
197
278
198
279
Projects maintained by the unified collective are compatible with all maintained
199
280
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+.
201
282
Our projects sometimes work with older versions, but this is not guaranteed.
202
283
203
284
## Security
@@ -280,6 +361,8 @@ abide by its terms.
280
361
281
362
[hast]: https://github.com/syntax-tree/hast
282
363
364
+ [hast-node]: https://github.com/syntax-tree/hast#nodes
365
+
283
366
[estree]: https://github.com/estree/estree
284
367
285
368
[program]: https://github.com/estree/estree/blob/master/es5.md#programs
@@ -290,4 +373,18 @@ abide by its terms.
290
373
291
374
[build-jsx]: https://github.com/wooorm/estree-util-build-jsx
292
375
376
+ [schema]: https://github.com/wooorm/property-information#api
377
+
293
378
[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