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
4247This 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
4651npm 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 ] .
145151There 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
173179Typically, there is only one node in ` body ` , however, this utility also supports
174180embedded MDX nodes in the HTML (when [ ` mdast-util-mdx ` ] [ mdast-util-mdx ] is used
175181with mdast to parse markdown before passing its nodes through to hast).
176182When MDX ESM import/exports are used, those nodes are added before the fragment
177183or 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-
186185There aren’t many great estree serializers out there that support JSX.
187186To do that, you can use [ ` estree-util-to-js ` ] [ estree-util-to-js ] .
188187Or, 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
193273This 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
198279Projects maintained by the unified collective are compatible with all maintained
199280versions 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+.
201282Our 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