hast to DOM Transformer

4.0.1 · active · verified Sun Apr 19

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

Warnings

Install

Imports

Quickstart

Demonstrates converting a `hast` tree to a DOM tree using `toDom` with `jsdom` for a Node.js environment. It also shows how to use the `afterTransform` option.

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());

view raw JSON →