HAST Node Plain-Text Converter
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.
Common errors
-
ERR_REQUIRE_ESM: require() of ES Module .../node_modules/hast-util-to-string/index.js from ... not supported.
cause Attempting to use CommonJS `require()` to import `hast-util-to-string`, which is an ESM-only package.fixChange `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
cause This error often occurs when trying to use a default import for `toString`, but `hast-util-to-string` provides `toString` as a named export.fixEnsure you are using a named import: `import { toString } from 'hast-util-to-string'` instead of `import toString from 'hast-util-to-string'`.
Warnings
- 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.
- breaking Version 3.0.0 and subsequent releases require Node.js 16 or higher. Older Node.js versions are not supported.
- 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.
- gotcha The `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.
Install
-
npm install hast-util-to-string -
yarn add hast-util-to-string -
pnpm add hast-util-to-string
Imports
- toString
import toString from 'hast-util-to-string'
import { toString } from 'hast-util-to-string' - toString
const toString = require('hast-util-to-string')import { toString } from 'hast-util-to-string' - toString
import {toString} from 'https://esm.sh/hast-util-to-string@3'
Quickstart
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.