hast-util-to-estree
hast utility to transform to estree (JSX).
Contents
- What is this?
- When should I use this?
- Install
- Use
- API
- Types
- Compatibility
- Security
- Related
- Contribute
- License
What is this?
This package is a utility that takes a hast (HTML) syntax tree as input and turns it into an estree (JavaScript) syntax tree (with a JSX extension). This package also supports embedded MDX nodes.
When should I use this?
This project is useful when you want to embed HTML as JSX inside JS while working with syntax trees. This is used in MDX.
Install
This package is ESM only. In Node.js (version 14.14+ and 16.0+), install with npm:
npm install hast-util-to-estree
In Deno with esm.sh
:
import {toEstree} from 'https://esm.sh/hast-util-to-estree@2'
In browsers with esm.sh
:
<script type="module">
import {toEstree} from 'https://esm.sh/hast-util-to-estree@2?bundle'
</script>
Use
Say our module example.html
contains:
<!doctype html>
<html lang=en>
<title>Hi!</title>
<link rel=stylesheet href=index.css>
<h1>Hello, world!</h1>
<a download style="width:1;height:10px"></a>
<!--commentz-->
<svg xmlns="http://www.w3.org/2000/svg">
<title>SVG `<ellipse>` element</title>
<ellipse
cx="120"
cy="70"
rx="100"
ry="50"
/>
</svg>
<script src="index.js"></script>
…and our module example.js
looks as follows:
import fs from 'node:fs/promises'
import {fromHtml} from 'hast-util-from-html'
import {toEstree} from 'hast-util-to-estree'
import {toJs, jsx} from 'estree-util-to-js'
const hast = fromHtml(await fs.readFile('example.html'))
const estree = toEstree(hast)
console.log(toJs(estree, {handlers: jsx}).value)
…now running node example.js
(and prettier) yields:
/* Commentz */
;<>
<html lang="en">
<head>
<title>{'Hi!'}</title>
{'\n'}
<link rel="stylesheet" href="index.css" />
{'\n'}
</head>
<body>
<h1>{'Hello, world!'}</h1>
{'\n'}
<a
download
style={{
width: '1',
height: '10px'
}}
/>
{'\n'}
{}
{'\n'}
<svg xmlns="http://www.w3.org/2000/svg">
{'\n '}
<title>{'SVG `<ellipse>` element'}</title>
{'\n '}
<ellipse cx="120" cy="70" rx="100" ry="50" />
{'\n'}
</svg>
{'\n'}
<script src="index.js" />
{'\n'}
</body>
</html>
</>
API
This package exports the identifiers defaultHandlers
and
toEstree
.
There is no default export.
toEstree(tree[, options])
Transform a hast tree (with embedded MDX nodes) into an estree (with JSX nodes).
Notes
Comments are attached to the tree in their neighbouring nodes (recast
,
babel
style) and also added as a comments
array on the program node
(espree
style).
You may have to do program.comments = undefined
for certain compilers.
Parameters
Returns
estree program node (Program
).
The program’s last child in body
is most likely an ExpressionStatement
,
whose expression is a JSXFragment
or a JSXElement
.
Typically, there is only one node in body
, however, this utility also supports
embedded MDX nodes in the HTML (when mdast-util-mdx
is used
with mdast to parse markdown before passing its nodes through to hast).
When MDX ESM import/exports are used, those nodes are added before the fragment
or element in body.
There aren’t many great estree serializers out there that support JSX.
To do that, you can use estree-util-to-js
.
Or, use estree-util-build-jsx
to turn JSX into function
calls, and then serialize with whatever (astring
, escodegen
).
defaultHandlers
Default handlers for elements (Record<string, Handle>
).
Each key is a node type, each value is a Handle
.
Handle
Turn a hast node into an estree node (TypeScript type).
Parameters
Returns
JSX child (JsxChild
, optional).
You can also add more results to state.esm
and state.comments
.
Options
Configuration (TypeScript type).
Fields
space
Which space the document is in (Space
, default: 'html'
).
When an <svg>
element is found in the HTML space, this package already
automatically switches to and from the SVG space when entering and exiting
it.
handlers
Object mapping node types to functions handling the corresponding nodes
(Record<string, Handle>
, optional).
Merged into the defaults.
See Handle
.
Space
Namespace (TypeScript type).
Type
type Space = 'html' | 'svg'
State
Info passed around about the current state (TypeScript type).
Fields
-
schema
(Schema
) — current schema -
comments
(Array<EstreeComment>
) — list of estree comments -
esm
(Array<EstreeNode>
) — list of top-level estree nodes -
handle
((node: HastNode) => EstreeJsxChild | void
) — transform a hast node to estree -
handle
((node: HastParent) => EstreeJsxChild | void
) — transform children of a hast parent to estree -
patch
((from: HastNode, to: EstreeNode) => void
) — take positional info fromfrom
(useinherit
if you also want data) -
inherit
((from: HastNode, to: EstreeNode) => void
) — take positional info and data fromfrom
(usepatch
if you don’t want data) -
createJsxAttributeName
((name: string) => EstreeJsxAttributeName
) — create a JSX attribute name -
createJsxElementName
((name: string) => EstreeJsxElementName
) — create a JSX attribute name
Types
This package is fully typed with TypeScript.
It exports the additional types Handle
, Options
,
Space
, and State
.
Compatibility
Projects maintained by the unified collective are compatible with all maintained versions of Node.js. As of now, that is Node.js 14.14+ and 16.0+. Our projects sometimes work with older versions, but this is not guaranteed.
Security
You’re working with JavaScript. It’s not safe.
Related
-
hastscript
— hyperscript compatible interface for creating nodes -
hast-util-from-dom
— transform a DOM tree to hast -
estree-util-build-jsx
— transform JSX to function calls
Contribute
See contributing.md
in syntax-tree/.github
for
ways to get started.
See support.md
for ways to get help.
This project has a code of conduct. By interacting with this repository, organization, or community you agree to abide by its terms.