Registry / serialization / hast-util-to-xast

hast-util-to-xast

JSON →
library3.0.1jsnpmunverified

Utility to transform a hast (HTML) syntax tree into a xast (XML) syntax tree. v3.0.1, ESM-only, fully typed with TypeScript. Supports automatic SVG namespace switching and embedded MDX nodes. Commonly used for EPUB generation. Requires Node.js 16+. Replaced old direct `space` argument with `options.space` in v3.

npm install hast-util-to-xast
INSTALL
IMPORT
SIG · HAST-UTIL-TO-XAST
H
hast-util-to-xast
serializationjavascriptv3.0.1
harness data pending
Install & Compatibility
Where this runs

No compatibility data collected yet for this library.

Code
Verified usage

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

toXast
import { toXast } from 'hast-util-to-xast'
import toXast from 'hast-util-to-xast'
This package has no default export; use named import only. Also works with `const { toXast } = await import('hast-util-to-xast')`.
Options
import type { Options } from 'hast-util-to-xast'
import { Options } from 'hast-util-to-xast'
Options is a TypeScript type, not a runtime value. Use `import type` to avoid runtime inclusion.
Space
import type { Space } from 'hast-util-to-xast'
import { Space } from 'hast-util-to-xast'
Space is a TypeScript type, not a runtime value. Use `import type`.

Parses HTML with hast-util-from-html, converts to xast, then serializes to XML string.

import { fromHtml } from 'hast-util-from-html' import { toXast } from 'hast-util-to-xast' import { toXml } from 'xast-util-to-xml' const html = `<!DOCTYPE html><title>Hello</title><h1>World</h1>` const hast = fromHtml(html) const xast = toXast(hast, { space: 'html' }) const xml = toXml(xast) console.log(xml) // <?xml version="1.0" encoding="utf-8"?><!DOCTYPE html><html xmlns="http://www.w3.org/1999/xhtml"><head><title>Hello</title></head><body><h1>World</h1></body></html>
Debug
Known issues
breakingRemoved direct `space` string argument; use `options.space` instead.
fix
Pass `{ space: 'svg' }` instead of `'svg'` as second argument.
affects: >=3.0.0
breakingChanged to ESM-only. No CommonJS support starting from v3.
fix
Use `import { toXast } from 'hast-util-to-xast'` or dynamic `import()`.
affects: >=3.0.0
breakingDropped support for Node.js < 16.
fix
Upgrade Node.js to version 16 or later.
affects: >=3.0.0
gotchaWhen space is 'html', SVG elements are automatically detected and switched to SVG namespace, but custom XML namespaces not supported.
fix
Use explicit `xmlns` attributes for non-SVG custom namespaces (may not be preserved).
affects: >=1.0.0
Errors
Common errors & fixes
TypeError: toXast is not a function
Using default import instead of named import (e.g., `import toXast from 'hast-util-to-xast'`). Package only exports named export.
fix
Change import to `import { toXast } from 'hast-util-to-xast'`.
Error [ERR_REQUIRE_ESM]: require() of ES Module /path/to/hast-util-to-xast/index.js from /path/to/project/index.js not supported.
Using require() on an ESM-only package.
fix
Use `import()` or switch to ESM in your project (package.json type: module).
TypeError: The "original" argument must be of type function. Received an instance of Object
Passing a string as second argument instead of an options object in v3+.
fix
Change `toXast(hast, 'svg')` to `toXast(hast, { space: 'svg' })`.
Upgrade
Version history
3.0.1latest on npm
Audit
Dependencies
hast-util-from-htmloptionalCommonly used together to parse HTML before conversion
xast-util-to-xmloptionalOften used to serialize the resulting xast tree to XML string
Agent activity
9 hits · last 30 days
node
8
Amazon
1
Resources
hast-util-to-xast — npm install hast-util-to-xast · libregistry