Registry / web-framework / hast-util-to-string

hast-util-to-string

JSON →
library3.0.1jsnpmunverified

hast-util-to-string is a utility within the `unified` ecosystem designed to extract the plain-text value of a `hast` (HTML Abstract Syntax Tree) node. It strictly mimics the DOM's `Node#textContent` getter, returning all textual content regardless of styling or layout, and importantly, it does not interpret HTML elements like `<br>` as introducing newlines. This behavior differentiates it from `hast-util-to-text`, which emulates `Node#innerText` by considering rendered output. The current stable version is 3.0.1. As part of the actively maintained `unified` collective, it follows a release cadence tied to the broader ecosystem, with major versions often introducing updated Node.js requirements (e.g., Node.js 16+ for v3) and migrating to modern JavaScript module practices, including being ESM-only and utilizing package `exports` fields. The library provides comprehensive TypeScript type definitions, ensuring robust development.

npm install hast-util-to-string
INSTALL
IMPORT
SIG · HAST-UTIL-TO-STRIN
H
hast-util-to-string
web-frameworkjavascriptv3.0.1
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.

toString
import { toString } from 'hast-util-to-string'
import toString from 'hast-util-to-string'
The `toString` function is a named export, not a default export. This package is ESM-only since v3.
toString
import { toString } from 'hast-util-to-string'
const toString = require('hast-util-to-string')
This package is ESM-only and requires Node.js 16+ for v3 and newer. CommonJS `require()` is not supported directly.
toString
import {toString} from 'https://esm.sh/hast-util-to-string@3'
For browser or Deno environments, it can be imported directly from a CDN like esm.sh, specifying the version.

This quickstart demonstrates how to convert both simple and complex HAST nodes into their plain-text representations using `hast-util-to-string`, highlighting its `Node#textContent`-like behavior.

import {h, type Element} from 'hastscript'; import {toString} from 'hast-util-to-string'; // Create a simple HAST paragraph node const paragraphNode: Element = h('p', 'This is a simple paragraph.'); console.log('Input HAST node (paragraph):', JSON.stringify(paragraphNode, null, 2)); console.log('Plain text output:', toString(paragraphNode)); // Expected output: 'This is a simple paragraph.' // Create a more complex HAST div node with nested elements and a break tag const complexNode: Element = h('div', [ h('b', 'Bold text'), ' and ', h('i', 'italic text'), h('br'), // A break tag ' on the same line according to textContent (no newline).' // textContent doesn't add newline for <br> ]); console.log('\nInput HAST node (complex div):', JSON.stringify(complexNode, null, 2)); console.log('Plain text output:', toString(complexNode)); // Expected output: 'Bold text and italic text. on the same line according to textContent (no newline).' // This demonstrates that <br> tags are ignored when mimicking Node#textContent behavior.
Debug
Known issues
breaking`hast-util-to-string` v3 and newer packages in the `unified` ecosystem are ESM-only, requiring projects to adopt ES module syntax (`import`/`export`) and tooling. CommonJS `require()` is no longer supported.
fix
Migrate your project to use ES module syntax. For Node.js, ensure your `package.json` specifies `"type": "module"` or use `.mjs` file extensions. Update bundler configurations if applicable.
affects: >=3.0.0
breakingVersion 3.0.0 and subsequent releases require Node.js 16 or higher. Older Node.js versions are not supported.
fix
Upgrade your Node.js environment to version 16 or newer. Use `nvm` or other version managers for easier switching.
affects: >=3.0.0
gotcha`hast-util-to-string` implements the `Node#textContent` algorithm, which extracts text content without regard for HTML display properties (e.g., `<br>` does not introduce a newline). If you need `Node#innerText`-like behavior (which considers rendered layout), use `hast-util-to-text` instead.
fix
If line breaks or other layout-aware text extraction is needed, switch to `hast-util-to-text`. Otherwise, ensure your code accounts for `textContent` behavior.
affects: >=1.0.0
gotchaThe `unified` ecosystem, including `hast-util-to-string`, processes HTML. Improper use or failure to sanitize input/output can lead to Cross-Site Scripting (XSS) vulnerabilities. This package itself does not sanitize content.
fix
Always use a sanitizer like `rehype-sanitize` on `hast` trees before serializing them back to HTML, especially when dealing with untrusted user input.
affects: >=1.0.0
Errors
Common errors & fixes
ERR_REQUIRE_ESM: require() of ES Module .../node_modules/hast-util-to-string/index.js from ... not supported.
Attempting to use CommonJS `require()` to import `hast-util-to-string`, which is an ESM-only package.
fix
Change `const { toString } = require('hast-util-to-string')` to `import { toString } from 'hast-util-to-string'`. Ensure your project is configured for ES modules (e.g., `"type": "module"` in `package.json`).
TypeError: (0, hast_util_to_string_1.toString) is not a function
This error often occurs when trying to use a default import for `toString`, but `hast-util-to-string` provides `toString` as a named export.
fix
Ensure you are using a named import: `import { toString } from 'hast-util-to-string'` instead of `import toString from 'hast-util-to-string'`.
Upgrade
Version history
3.0.1latest on npm
Audit
Dependencies

No dependency data recorded yet.

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