hast to DOM Transformer
This package, `hast-util-to-dom`, serves as a utility for converting a `hast` (HTML Abstract Syntax Tree) into a browser-native DOM tree or a simulated DOM environment like `jsdom`. The current stable version is 4.0.1. The project generally follows Node.js maintenance cycles for compatibility, dropping support for unmaintained Node.js versions with major releases. It has a steady release cadence, with version 4.0.0 released recently to update dependencies and enforce modern JavaScript environments. Key differentiators include its tight integration within the unified ecosystem, providing a direct transformation path for HTML ASTs to DOM nodes. It supports client-side rendering directly into the browser's DOM, and also works in server-side contexts with DOM implementations like `jsdom`. It complements `hast-util-from-dom`, which performs the inverse operation, turning DOM trees into hast. Furthermore, `hast-util-to-dom` is utilized internally by `rehype-dom-stringify` for DOM-based HTML serialization. It is important to note that the library transitioned to being ESM-only since version 3.0.0 and requires Node.js 16 or newer as of version 4.0.0, which are significant compatibility considerations for integrators.
Common errors
-
Error [ERR_REQUIRE_ESM]: require() of ES Module ... hast-util-to-dom/index.js from ... not supported.
cause Attempting to import an ESM-only package using CommonJS `require()` syntax.fixChange your import statement from `const toDom = require('hast-util-to-dom')` to `import { toDom } from 'hast-util-to-dom'`. Ensure your project is configured for ES Modules. -
TypeError: (0, hast_util_to_dom_1.toDom) is not a function
cause Incorrectly trying to access `toDom` as a default export or from an unexpected path when it is a named export.fixVerify that you are using the correct named import: `import { toDom } from 'hast-util-to-dom'`.
Warnings
- breaking Version 4.0.0 dropped support for Node.js versions prior to 16. Ensure your environment meets this minimum requirement.
- breaking The package transitioned to ESM-only starting with version 3.0.0. CommonJS `require()` statements will no longer work.
- breaking Version 4.0.0 introduced the `exports` field in `package.json`. Avoid using private or undocumented import paths, as they may no longer be accessible or stable.
- breaking Version 3.0.0 removed support for transforming non-HTML doctypes. The utility is now strictly for HTML-based hast trees.
- gotcha As with any utility that transforms arbitrary input into DOM, care must be taken to prevent Cross-Site Scripting (XSS) vulnerabilities if the `hast` input is derived from untrusted sources. Sanitize input appropriately.
Install
-
npm install hast-util-to-dom -
yarn add hast-util-to-dom -
pnpm add hast-util-to-dom
Imports
- toDom
const toDom = require('hast-util-to-dom')import { toDom } from 'hast-util-to-dom' - AfterTransform
import { AfterTransform } from 'hast-util-to-dom'import type { AfterTransform } from 'hast-util-to-dom' - Options
import { Options } from 'hast-util-to-dom'import type { Options } from 'hast-util-to-dom'
Quickstart
import { JSDOM } from 'jsdom';
import { h } from 'hastscript';
import { toDom } from 'hast-util-to-dom';
// Create a JSDOM environment to simulate a browser DOM
const dom = new JSDOM();
const document = dom.window.document;
// Construct a hast tree using hastscript (a helper for creating HAST nodes)
const hastTree = h('main', [
h('h1', 'Welcome to the DOM!'),
h('p', [
h('strong', 'Hello'),
', world! This paragraph was created from a HAST node.'
]),
h('ul', [
h('li', 'Item 1'),
h('li', 'Item 2')
])
]);
// Convert the hast tree into a DOM node. Pass the JSDOM document as an option.
const domNode = toDom(hastTree, { document });
// Append the generated DOM node to the JSDOM document body
document.body.append(domNode);
// You can now inspect or serialize the resulting DOM structure
console.log('Serialized DOM:', dom.serialize());
// Example of using the `afterTransform` hook option
const treeWithHook = h('div', [h('span', 'This span will be marked')]);
const domNodeWithHook = toDom(treeWithHook, {
document,
afterTransform(hastNode, domNode) {
if (hastNode.type === 'element' && hastNode.tagName === 'span') {
// Add a data attribute to the DOM span element
(domNode as HTMLElement).dataset.transformed = 'true';
}
}
});
document.body.append(domNodeWithHook);
console.log('Serialized DOM with hook:', dom.serialize());