hast to ESTree (JSX) Transformer
hast-util-to-estree is a utility package within the unified/syntax-tree ecosystem designed to transform a HTML Abstract Syntax Tree (hast) into an ECMAScript Abstract Syntax Tree (estree) with JSX extensions. This is particularly useful for embedding HTML content as JSX within JavaScript, such as in MDX. The current stable version is 3.1.3, with releases occurring regularly, often involving minor updates to dependencies or new options, indicating active maintenance. Key differentiators include its specific focus on the hast-to-estree transformation, robust handling of JSX, support for embedded MDX nodes, and its adherence to the unist specification, making it a reliable component in pipelines involving content transformation and compilation.
Common errors
-
ERR_REQUIRE_ESM
cause Attempting to use `require('hast-util-to-estree')` in a CommonJS context after version 3.0.0, which is ESM-only.fixChange your import statement to `import { toEstree } from 'hast-util-to-estree'` and ensure your Node.js project or file is configured for ES Modules. -
ReferenceError: toEstree is not defined
cause Incorrectly importing `toEstree` using a default import or a misspelled named import.fixEnsure you are using a named import: `import { toEstree } from 'hast-util-to-estree'`. -
TypeError: toEstree is not a function
cause Potentially an incorrect default import being treated as a function, or a version mismatch where `toEstree` isn't properly exported.fixVerify that you are using the correct named import: `import { toEstree } from 'hast-util-to-estree'`. Check your package version for any breaking changes related to exports.
Warnings
- breaking Version 3.0.0 changed the minimum required Node.js version to 16. Older Node.js environments will encounter errors.
- breaking Since version 3.0.0, the package is ESM-only and uses the `exports` field in `package.json`. This means `require()` statements will fail, and specific import paths might change.
- breaking Version 3.0.0 updated its dependency on `@types/hast`. If your project explicitly depends on `@types/hast`, you may need to update your version as well to avoid type conflicts.
- gotcha Comments are attached to neighboring nodes and as a `comments` array on the program node. Some compilers (e.g., Babel) might require `program.comments = undefined` for correct processing.
- gotcha JSX frameworks often have different conventions for attributes (e.g., `class` vs `className`, `background-color` vs `backgroundColor`). This utility generates standard JSX, which might need further processing for specific frameworks.
Install
-
npm install hast-util-to-estree -
yarn add hast-util-to-estree -
pnpm add hast-util-to-estree
Imports
- toEstree
const { toEstree } = require('hast-util-to-estree')import { toEstree } from 'hast-util-to-estree' - defaultHandlers
const { defaultHandlers } = require('hast-util-to-estree')import { defaultHandlers } from 'hast-util-to-estree' - ElementAttributeNameCase
import type { ElementAttributeNameCase } from 'hast-util-to-estree'
Quickstart
import { read } from 'to-vfile'
import { fromHtml } from 'hast-util-from-html'
import { toEstree } from 'hast-util-to-estree'
import { toJs, jsx } from 'estree-util-to-js'
async function transformHtmlToJsx() {
const htmlContent = `
<!doctype html>
<html lang=en>
<title>Example Page</title>
<h1>Hello, hast-util-to-estree!</h1>
<p style="color:red">This is a styled paragraph.</p>
</html>
`
// Convert HTML string to a hast tree
const hast = fromHtml(htmlContent)
// Convert hast tree to an estree (with JSX nodes)
const estree = toEstree(hast)
// Convert estree to a JavaScript string (with JSX syntax)
const jsxOutput = toJs(estree, { handlers: jsx }).value
console.log(jsxOutput)
}
transformHtmlToJsx().catch(console.error)