Registry / serialization / hast-util-to-html

hast-util-to-html

JSON →
library9.0.5jsnpmunverified

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.

npm install hast-util-to-html
INSTALL
IMPORT
SIG · HAST-UTIL-TO-HTML
H
hast-util-to-html
serializationjavascriptv9.0.5
Install
Import
Disk
Pass rate
0/ 6
Env Coverage0 / 6
glibc
1822
musl
1822
Install & Compatibility
Where this runs
tested against v? · npm install
Install × environment matrix
Each cell = how many times install + import succeeded across repeated harness runs. Partial = flaky.
glibc = Debian/Ubuntu slim · musl = Alpine Linux
musl
node 18226 runs
build_error
glibc
node 18226 runs
build_error
Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

toHtml
import { toHtml } from 'hast-util-to-html';
const { toHtml } = require('hast-util-to-html');
The package is ESM-only since v9, so CommonJS `require` is not supported.
Options
import type { Options } from 'hast-util-to-html';
Type import for configuring serialization behavior; not a runtime value.
toHtml (Deno/Browser)
import {toHtml} from 'https://esm.sh/hast-util-to-html@9'
For Deno environments or browser consumption via `esm.sh`. Add `?bundle` for browser build.

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 }));
Debug
Known issues
breakingVersion 9.0.0 changed to be ESM-only and requires Node.js 16 or higher. Projects still using CommonJS or older Node.js versions must stick to v8 or migrate.
fix
Upgrade Node.js to 16+ and migrate imports to ESM syntax (e.g., `import` instead of `require`). If migration is not possible, use `hast-util-to-html@8`.
affects: >=9.0.0
breakingThe `entities` option was removed in v9.0.0 and replaced with `characterReferences`. Trying to use `options.entities` will result in a TypeScript error or runtime property access issues.
fix
Replace `options.entities` with `options.characterReferences` and adjust its properties accordingly (e.g., `useNamedReferences`, `omitOptionalSemicolons`).
affects: >=9.0.0
breakingVersion 9.0.0 transitioned to Node.js `exports` field, which means internal or private APIs are no longer directly accessible. Relying on undocumented paths will break.
fix
Only use publicly documented exports. Avoid deep imports into `node_modules/hast-util-to-html`.
affects: >=9.0.0
gotchaSetting `allowDangerousCharacters` or `allowDangerousHtml` to `true` can introduce XSS vulnerabilities if the input HAST tree is not fully trusted. These options should only be used in controlled environments or with guaranteed safe input.
fix
Ensure that `allowDangerousCharacters` and `allowDangerousHtml` are `false` (the default) unless you have thoroughly vetted the input source and understand the security implications. Sanitize or validate user-generated HAST trees before serialization.
affects: >=1.0.0
gotchaThe `characterReferences.omitOptionalSemicolons` option, when enabled, can produce HTML that causes 'parse errors' according to HTML specifications. While still technically valid, it is primarily intended for minification and might cause issues with some parsers or validators.
fix
Only enable `characterReferences.omitOptionalSemicolons` if you are specifically building a minifier and have tested its compatibility with your target environment. For general use, keep this option disabled.
affects: >=9.0.0
Errors
Common errors & fixes
ERR_REQUIRE_ESM
Attempting to `require()` an ESM-only package in a CommonJS context.
fix
Migrate your project to use ES modules (`import`/`export`) or switch to an older version of `hast-util-to-html` (e.g., v8) that supports CommonJS. Ensure your `package.json` specifies `"type": "module"` for ESM.
TypeError: (0 , hast_util_to_html__WEBPACK_IMPORTED_MODULE_0__.toHtml) is not a function
Incorrect import syntax (e.g., trying to default import a named export) or a version mismatch where `toHtml` might not be directly available.
fix
Ensure you are using named imports: `import { toHtml } from 'hast-util-to-html';`. Verify that `hast-util-to-html` is correctly installed and that the version is compatible with your code.
Property 'entities' does not exist on type 'Options'. Did you mean 'characterReferences'?
Using the deprecated `entities` option from v8 with `hast-util-to-html` v9 or newer.
fix
Update your code to use the `characterReferences` option instead of `entities`. For example, `options: { characterReferences: { useNamedReferences: true } }`.
Upgrade
Version history
9.0.5latest on npm
Audit
Dependencies
property-informationrequiredUsed internally for handling HTML property serialization, ensuring correct attribute output.
Agent activity
4 hits · last 30 days
node
4
Resources
hast-util-to-html — npm install hast-util-to-html · libregistry