hast-util-to-parse5
hast-util-to-parse5 is a utility within the unified (syntax-tree) ecosystem designed to convert a HAST (Hypertext Abstract Syntax Tree) into a parse5 AST (Abstract Syntax Tree). It is currently at version 8.0.1 and receives updates as part of the broader unified collective, with major versions typically introducing significant changes and minor/patch versions addressing features and fixes. This package differentiates itself by avoiding the use of parse5 adapters, which the developers state reduces code weight and fragility. It is primarily used when there's a specific need to generate a parse5 AST from an existing HAST tree, though its inverse, `hast-util-from-parse5`, is often more commonly employed. The library is ESM-only and requires Node.js 16 or newer since its 8.x release line. Users should be aware of potential cross-site scripting (XSS) risks if the input HAST tree is not properly sanitized, as highlighted in its security considerations.
Common errors
-
ERR_REQUIRE_ESM
cause Attempting to use `require()` to import `hast-util-to-parse5` in a CommonJS environment, but the package is ESM-only.fixChange `const { toParse5 } = require('hast-util-to-parse5')` to `import { toParse5 } from 'hast-util-to-parse5'` and ensure your project is configured for ES modules (e.g., `"type": "module"` in `package.json`). -
Argument of type 'string' is not assignable to parameter of type 'Options | undefined'.
cause Passing the `space` option directly as a string to `toParse5` after version 8.0.0.fixWrap the `space` option in an object: `toParse5(tree, { space: 'html' })`.
Warnings
- breaking Version 8.0.0 removed direct support for passing the `space` option as a string. It must now be passed within an `options` object as `{ space: 'html' }` or `{ space: 'svg' }`.
- breaking Version 8.0.0 changed to require Node.js 16 or later. Older Node.js versions are no longer supported.
- breaking Version 8.0.0 updated its internal `@types/hast` dependency. If your project uses `@types/hast` directly, ensure you also update it to maintain compatibility.
- breaking Version 7.0.0 switched to being an ESM-only package. CommonJS `require()` is no longer supported.
- breaking Version 7.0.0 removed support for transforming non-HTML doctypes.
- gotcha The package uses the `exports` field in its `package.json`. Directly accessing internal paths (e.g., `hast-util-to-parse5/lib/index.js`) is not supported and may break in future versions.
- gotcha Using `hast-util-to-parse5` with an untrusted HAST tree can lead to a cross-site scripting (XSS) attack if the resulting parse5 AST is later serialized and rendered without proper sanitization.
Install
-
npm install hast-util-to-parse5 -
yarn add hast-util-to-parse5 -
pnpm add hast-util-to-parse5
Imports
- toParse5
const toParse5 = require('hast-util-to-parse5')import { toParse5 } from 'hast-util-to-parse5' - Options
import { Options } from 'hast-util-to-parse5'import type { Options } from 'hast-util-to-parse5' - Space
import { Space } from 'hast-util-to-parse5'import type { Space } from 'hast-util-to-parse5'
Quickstart
import { toParse5 } from 'hast-util-to-parse5';
import type { Element, Text, Root } from 'hast';
const hastTree: Root = {
type: 'root',
children: [
{
type: 'element',
tagName: 'html',
properties: { lang: 'en' },
children: [
{
type: 'element',
tagName: 'head',
properties: {},
children: [
{ type: 'element', tagName: 'title', properties: {}, children: [{ type: 'text', value: 'Hello' }] as Text[] }
]
} as Element,
{
type: 'element',
tagName: 'body',
properties: {},
children: [
{ type: 'element', tagName: 'h1', properties: {}, children: [{ type: 'text', value: 'World!' }] as Text[] },
{ type: 'element', tagName: 'p', properties: {}, children: [{ type: 'text', value: 'This is a paragraph.' }] as Text[] }
]
} as Element
]
} as Element
]
};
// Convert the hast tree to a parse5 AST
const parse5Tree = toParse5(hastTree, { space: 'html' });
console.log(JSON.stringify(parse5Tree, null, 2));
/* Example of what the output parse5Tree might look like (truncated):
{
"nodeName": "html",
"tagName": "html",
"attrs": [
{
"name": "lang",
"value": "en"
}
],
"namespaceURI": "http://www.w3.org/1999/xhtml",
"childNodes": [
{
"nodeName": "head",
"tagName": "head",
// ... more child nodes ...
},
{
"nodeName": "body",
"tagName": "body",
// ... more child nodes ...
}
]
}
*/