Registry / serialization / hast-util-to-text

hast-util-to-text

JSON →
library4.0.2jsnpmunverified

hast-util-to-text is a utility for the unified ecosystem that extracts the plain-text value from a HAST (HTML Abstract Syntax Tree) node. It approximates the DOM's `Node#innerText` algorithm, which is more user-friendly than `Node#textContent` (like `hast-util-to-string`) by converting `<br>` elements into line breaks and using tabs (`\t`) between table cells. The package is currently at version 4.0.2, actively maintained, and primarily releases patch versions for fixes and minor updates for new features, with major versions reserved for breaking changes. Its key differentiator is its adherence to the `innerText`-like behavior, providing a textual representation that reflects how content would be visually rendered, although it cannot account for dynamic CSS properties like `display: none` or `text-transform`.

npm install hast-util-to-text
INSTALL
IMPORT
SIG · HAST-UTIL-TO-TEXT
H
hast-util-to-text
serializationjavascriptv4.0.2
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.

toText
import { toText } from 'hast-util-to-text'
const toText = require('hast-util-to-text')
This package is ESM-only since v3.0.0. CommonJS `require()` is not supported. It also requires Node.js 16+ since v4.0.0.
Options
import type { Options } from 'hast-util-to-text'
Type import for configuring the `toText` function, for use in TypeScript projects. It's a type, so it uses `import type`.
Whitespace
import type { Whitespace } from 'hast-util-to-text'
Type import for the `whitespace` option, specifying how whitespace should be handled. For use in TypeScript projects.

This quickstart demonstrates how to use `toText` to convert a HAST tree into a plain-text string, showing how it handles line breaks from `<br>` and tabs in table cells.

import {h} from 'hastscript' import {toText} from 'hast-util-to-text' const tree = h('div', [ h('h1', {hidden: true}, 'Alpha.'), h('article', [ h('p', ['Bravo', h('br'), 'charlie.']), // <br> will become a newline h('p', 'Delta echo \t foxtrot.') // Tab will be preserved ]), h('table', [ h('tr', [ h('td', 'Cell 1'), h('td', 'Cell 2') ]) ]) ]) console.log(toText(tree)); // Expected output: // Bravo // charlie. // // Delta echo foxtrot. // Cell 1 Cell 2
Debug
Known issues
breakingVersion 4.0.0 changed to require Node.js 16 or higher. Older Node.js versions are no longer supported.
fix
Upgrade your Node.js environment to version 16 or newer. Use `nvm install 16` or update your CI/CD configuration.
affects: >=4.0.0
breakingVersion 4.0.0 changed the package to use the `exports` field in `package.json`, which affects how it can be imported, particularly in CommonJS environments or older bundlers. It's designed for modern ESM import patterns.
fix
Ensure your build tooling and Node.js version support the `exports` field. Always use `import` statements for ESM modules. Avoid using private APIs if previously done.
affects: >=4.0.0
breakingVersion 3.0.0 converted the package to be ESM-only. CommonJS `require()` statements will no longer work.
fix
Migrate your codebase to use ES Modules (`import ... from '...'`) for this package. If you must use CommonJS, consider transpiling your code or sticking to an older major version (e.g., `<3.0.0`).
affects: >=3.0.0
breakingVersion 2.0.0 updated `unist-util-find-after`, which could be a breaking change, particularly for TypeScript users or dependents relying on specific type definitions.
fix
Review the changelog for `unist-util-find-after` and `hast-util-to-text` v2.0.0. Update `@types/unist` and related packages to ensure type compatibility.
affects: >=2.0.0
gotchaThis utility's `innerText` algorithm is an approximation and deviates from the DOM specification in some cases. It cannot account for CSS properties like `display: none` or `text-transform` that dynamically alter text visibility or appearance, nor does it process replaced elements (e.g., `<audio>`) as the DOM would.
fix
Be aware of these limitations. If exact DOM `innerText` behavior is required, consider using a headless browser environment (e.g., Puppeteer, JSDOM) to render and extract text, as `hast-util-to-text` cannot interpret CSS.
affects: >=1.0.0
Errors
Common errors & fixes
ERR_REQUIRE_ESM
Attempting to `require()` an ESM-only package.
fix
Change `const { toText } = require('hast-util-to-text')` to `import { toText } from 'hast-util-to-text'`.
TypeError: toText is not a function
Incorrect import syntax (e.g., default import when only named exports exist, or attempting to destructure from an incorrectly transpiled CJS module).
fix
Ensure you are using named imports: `import { toText } from 'hast-util-to-text'`.
Error [ERR_PACKAGE_PATH_NOT_EXPORTED]: Package 'hast-util-to-text' was not found at package.json#exports.
Using an older Node.js version (<16) that doesn't fully support the `exports` field in `package.json`, or an outdated bundler/tooling.
fix
Update Node.js to version 16 or newer. Ensure your build tooling (e.g., webpack, Rollup, Parcel) is up-to-date and configured to handle ESM and `exports` maps correctly.
Upgrade
Version history
4.0.2latest on npm
Audit
Dependencies

No dependency data recorded yet.

Agent activity
4 hits · last 30 days
node
4
Resources
hast-util-to-text — npm install hast-util-to-text · libregistry