
Product
Introducing Reports: An Extensible Reporting Framework for Socket Data
Explore exportable charts for vulnerabilities, dependencies, and usage with Reports, Socket’s new extensible reporting framework.
hast-util-to-estree
Advanced tools
hast utility to transform to estree (JSX).
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.
This project is useful when you want to embed HTML as JSX inside JS while working with syntax trees. This is used in MDX.
This package is ESM only. In Node.js (version 16+), install with npm:
npm install hast-util-to-estree
In Deno with esm.sh:
import {toEstree} from 'https://esm.sh/hast-util-to-estree@3'
In browsers with esm.sh:
<script type="module">
import {toEstree} from 'https://esm.sh/hast-util-to-estree@3?bundle'
</script>
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 {jsx, toJs} from 'estree-util-to-js'
import {fromHtml} from 'hast-util-from-html'
import {toEstree} from 'hast-util-to-estree'
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>
</>
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).
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.
There are differences between what JSX frameworks accept, such as whether they
accept class or className, or background-color or backgroundColor.
For JSX components written in MDX, the author has to be aware of this difference and write code accordingly. For hast elements transformed by this project, this will be handled through options.
| Framework | elementAttributeNameCase | stylePropertyNameCase |
|---|---|---|
| Preact | 'html' | 'dom' |
| React | 'react' | 'dom' |
| Solid | 'html' | 'css' |
| Vue | 'html' | 'dom' |
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).
defaultHandlersDefault handlers for elements (Record<string, Handle>).
Each key is a node type, each value is a Handle.
ElementAttributeNameCaseSpecify casing to use for attribute names (TypeScript type).
HTML casing is for example class, stroke-linecap, xml:lang.
React casing is for example className, strokeLinecap, xmlLang.
type ElementAttributeNameCase = 'html' | 'react'
HandleTurn a hast node into an estree node (TypeScript type).
JSX child (JsxChild, optional).
You can also add more results to state.esm and state.comments.
OptionsConfiguration (TypeScript type).
elementAttributeNameCase
(ElementAttributeNameCase, default:
'react')
— specify casing to use for attribute names; this casing is used for hast
elements, not for embedded MDX JSX nodes (components that someone authored
manually)handlers (Record<string, Handle>, optional)
— custom handlersspace (Space, default: 'html')
— which space the document is in; 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 itstylePropertyNameCase
(StylePropertyNameCase,
default: 'dom')
— specify casing to use for property names in style objects; this casing
is used for hast elements, not for embedded MDX JSX nodes (components that
someone authored manually)tableCellAlignToStyle (boolean, default: true)
— turn obsolete align props on td and th into CSS style propsSpaceNamespace (TypeScript type).
type Space = 'html' | 'svg'
StateInfo passed around about the current state (TypeScript type).
all ((node: HastParent) => EstreeJsxChild | undefined)
— transform children of a hast parent to estreecomments (Array<EstreeComment>)
— list of estree commentscreateJsxAttributeName ((name: string) => EstreeJsxAttributeName)
— create a JSX attribute namecreateJsxElementName ((name: string) => EstreeJsxElementName)
— create a JSX attribute nameelementAttributeNameCase
(ElementAttributeNameCase)
— casing to use for attribute namesesm (Array<EstreeNode>)
— list of top-level estree nodeshandle ((node: HastNode) => EstreeJsxChild | undefined)
— transform a hast node to estreeinherit ((from: HastNode, to: EstreeNode) => undefined)
— take positional info and data from from (use patch if you don’t want
data)patch ((from: HastNode, to: EstreeNode) => undefined)
— take positional info from from (use inherit if you also want data)schema (Schema)
— current schemastylePropertyNameCase
(StylePropertyNameCase)
— casing for property names in style objectstableCellAlignToStyle (boolean)
— turn obsolete align props on td and th into CSS style propsStylePropertyNameCaseCasing to use for property names in style objects (TypeScript type).
CSS casing is for example background-color and -webkit-line-clamp.
DOM casing is for example backgroundColor and WebkitLineClamp.
type StylePropertyNameCase = 'css' | 'dom'
This package is fully typed with TypeScript.
It exports the additional types
ElementAttributeNameCase,
Handle, Options,
Space, State, and
StylePropertyNameCase.
Projects maintained by the unified collective are compatible with maintained versions of Node.js.
When we cut a new major release, we drop support for unmaintained versions of
Node.
This means we try to keep the current release line, hast-util-to-estree@^3,
compatible with Node.js 16.
You’re working with JavaScript. It’s not safe.
estree-util-build-jsx
— transform JSX to function callshastscript
— hyperscript compatible interface for creating nodeshast-util-from-dom
— transform a DOM tree to hastSee 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.
The `rehype-parse` package is used to parse HTML into HAST. While it doesn't convert HAST to ESTree, it is often used in conjunction with `hast-util-to-estree` to first parse HTML and then convert it to JavaScript structures.
The `unist-util-visit` package is a utility for traversing Unist syntax trees, which include HAST. It doesn't convert HAST to ESTree but can be used to manipulate HAST nodes before conversion.
FAQs
hast utility to transform to estree (JavaScript AST) JSX
The npm package hast-util-to-estree receives a total of 2,981,400 weekly downloads. As such, hast-util-to-estree popularity was classified as popular.
We found that hast-util-to-estree demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 2 open source maintainers collaborating on the project.
Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Product
Explore exportable charts for vulnerabilities, dependencies, and usage with Reports, Socket’s new extensible reporting framework.

Product
Socket for Jira lets teams turn alerts into Jira tickets with manual creation, automated ticketing rules, and two-way sync.

Company News
Socket won two 2026 Reppy Awards from RepVue, ranking in the top 5% of all sales orgs. AE Alexandra Lister shares what it's like to grow a sales career here.