Product
Introducing License Enforcement in Socket
Ensure open-source compliance with Socket’s License Enforcement Beta. Set up your License Policy and secure your software!
hast-util-to-estree
Advanced tools
The `hast-util-to-estree` package is a utility that converts HAST (Hypertext Abstract Syntax Tree) to ESTree (ECMAScript Tree). This is particularly useful for projects that need to manipulate HTML or Markdown content and then transform it into JavaScript code structures.
Convert HAST to ESTree
This feature allows you to convert a HAST node into an ESTree node. The code sample demonstrates converting a simple HAST node representing a <div> element into its ESTree equivalent.
const toEstree = require('hast-util-to-estree');
const hast = { type: 'element', tagName: 'div', properties: {}, children: [] };
const estree = toEstree(hast);
console.log(estree);
Handling different HAST node types
This feature shows how the utility handles different types of HAST nodes, including text nodes. The code sample converts a HAST node representing a <p> element with text content into its ESTree equivalent.
const toEstree = require('hast-util-to-estree');
const hast = { type: 'element', tagName: 'p', properties: {}, children: [{ type: 'text', value: 'Hello, world!' }] };
const estree = toEstree(hast);
console.log(estree);
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.
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 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 us used in MDX.
This package is ESM only. In Node.js (version 12.20+, 14.14+, 16.0+, 18.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>
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>
</>
This package exports the identifier toEstree
.
There is no default export.
toEstree(tree, options?)
Transform to estree (JSX).
options
Configuration (optional).
options.space
Whether tree
is in the HTML or SVG space (enum, 'svg'
or 'html'
, default:
'html'
).
If an svg
element is found when inside the HTML space, toEstree
automatically switches to the SVG space when entering the element, and
switches back when exiting
options.handlers
Object mapping node types to functions handling the corresponding nodes. See the code for examples.
Node (Program
) whose 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.
Comments are both attached to the tree in their neighbouring nodes (recast and
babel style), and added as a comments
array on the program node (espree
style).
You may have to do program.comments = null
for certain compilers.
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).
This package is fully typed with TypeScript.
It exports the additional types Options
, Space
, and Handle
.
Projects maintained by the unified collective are compatible with all maintained versions of Node.js. As of now, that is Node.js 12.20+, 14.14+, 16.0+, and 18.0+. Our projects sometimes work with older versions, but this is not guaranteed.
You’re working with JavaScript. It’s not safe.
hastscript
— hyperscript compatible interface for creating nodeshast-util-from-dom
— transform a DOM tree to hastestree-util-build-jsx
— transform JSX to function callsSee 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.
FAQs
hast utility to transform to estree (JavaScript AST) JSX
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
Ensure open-source compliance with Socket’s License Enforcement Beta. Set up your License Policy and secure your software!
Product
We're launching a new set of license analysis and compliance features for analyzing, managing, and complying with licenses across a range of supported languages and ecosystems.
Product
We're excited to introduce Socket Optimize, a powerful CLI command to secure open source dependencies with tested, optimized package overrides.