HAST to HTML Serializer

9.0.5 · active · verified Sun Apr 19

hast-util-to-html is a core utility within the unifiedjs ecosystem, designed to serialize a HAST (Hypertext Abstract Syntax Tree) into an HTML string. It is currently at version 9.0.5 and maintains an active release cadence, providing frequent patch updates and introducing major versions for significant breaking changes or feature additions. This utility is distinct from `rehype-stringify`, which acts as a higher-level wrapper for integrating with rehype plugins, while `hast-util-to-html` offers direct, granular control over the serialization process. Its primary differentiators include extensive options for configuring the output, enabling use cases from pretty-printing to minification, and its direct compatibility with HAST trees. It serves as the inverse operation to `hast-util-from-html`, providing a complete round-trip for HTML processing within the unifiedjs framework. The package adheres to modern JavaScript standards, being ESM-only since version 9.

Common errors

Warnings

Install

Imports

Quickstart

This quickstart demonstrates creating a HAST tree and then serializing it into an HTML string using `toHtml` with default and custom options.

import {h} from 'hastscript';
import {toHtml} from 'hast-util-to-html';

// Create a HAST tree using hastscript for convenience
const tree = h('.alpha', [
  'bravo ',
  h('b', 'charlie'),
  ' delta ',
  h('a.echo', {download: true, href: '#'}, 'foxtrot')
]);

// Serialize the HAST tree to an HTML string with default options
console.log(toHtml(tree));

// Example with options: omit quotes for attributes where possible
console.log(toHtml(tree, { quote: false }));

view raw JSON →