Registry / serialization / hast-util-truncate

hast-util-truncate

JSON →
library2.0.0jsnpmunverified

hast-util-truncate is a focused utility within the unified (unist/hast/mdast) ecosystem, designed to truncate a HAST (HTML Abstract Syntax Tree) to a specified number of characters while meticulously preserving its underlying structure. The package, currently stable at version 2.0.0, follows the syntax-tree's measured release cadence, with major versions often introducing changes related to Node.js environment requirements or shifts in JavaScript module standards. Its key differentiator lies in operating directly on the AST, enabling intelligent truncation that respects HTML semantics and avoids breaking tags, unlike naive string-based approaches. It offers configurable options for the truncation size, the ellipsis character, and a `maxCharacterStrip` mechanism to ensure word breaks occur gracefully, preventing awkward partial words at the truncation point. This makes it ideal for generating accurate excerpts, summaries, or SEO descriptions from rich HTML content for applications like blogs, content management systems, or search result snippets.

npm install hast-util-truncate
INSTALL
IMPORT
SIG · HAST-UTIL-TRUNCATE
H
hast-util-truncate
serializationjavascriptv2.0.0
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.

truncate
import { truncate } from 'hast-util-truncate'
const { truncate } = require('hast-util-truncate')
Package is ESM-only since v2.0.0, requiring Node.js 16+.
Options
import type { Options } from 'hast-util-truncate'
Type import for configuration options.
hast-util-truncate as a whole
import * as hastUtilTruncate from 'hast-util-truncate'
import hastUtilTruncate from 'hast-util-truncate'
There is no default export; all exports are named.

This example demonstrates how to import and use the `truncate` function to process a HAST paragraph node, limiting its text content to the default character count while appending a custom ellipsis.

import { h } from 'hastscript'; import { truncate } from 'hast-util-truncate'; const tree = h('p', [ 'Lorem ipsum dolor sit amet, ', h('em', 'consectetur'), 'adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud' ]); // Truncate to a default size (140 chars) with a custom ellipsis const truncatedTree = truncate(tree, { ellipsis: '…' }); console.log(JSON.stringify(truncatedTree, null, 2));
Debug
Known issues
breakinghast-util-truncate now requires Node.js 16 or higher due to updated build targets and features.
fix
Upgrade your Node.js environment to version 16 or a later LTS release.
affects: >=2.0.0
breakingThe package is now ESM-only (ECMAScript Modules). CommonJS `require()` is no longer supported for importing `hast-util-truncate`.
fix
Migrate your project to use ESM `import` statements. If running in Node.js, ensure `type: 'module'` is set in your `package.json` or use `.mjs` file extensions.
affects: >=2.0.0
breakingThe package now uses the `exports` field in `package.json`. Direct imports to internal paths (e.g., `hast-util-truncate/lib/*`) are no longer supported and will lead to import errors.
fix
Always import symbols directly from the main package entry point: `import { symbol } from 'hast-util-truncate'`.
affects: >=2.0.0
gotchaThe `@types/hast` dependency needs to be updated when migrating to `hast-util-truncate` v2.0.0 to ensure type compatibility with internal utility updates.
fix
Update `@types/hast` to its latest compatible version (e.g., `npm install @types/hast@latest`).
affects: >=2.0.0
Errors
Common errors & fixes
Error [ERR_REQUIRE_ESM]: require() of ES Module ... hast-util-truncate.js not supported.
Attempting to import hast-util-truncate (an ESM-only package) using CommonJS `require()`.
fix
Change `const { truncate } = require('hast-util-truncate')` to `import { truncate } from 'hast-util-truncate'`. Ensure your project runs in an ESM context (e.g., Node.js 16+ with `"type": "module"` in `package.json`).
TypeError: truncate is not a function
Incorrect import statement or attempting to access a default export when only named exports exist.
fix
Ensure you are using `import { truncate } from 'hast-util-truncate'` as there is no default export. Also, verify Node.js version and ESM context.
SyntaxError: Cannot use import statement outside a module
Using ESM `import` syntax in a file that is treated as a CommonJS module (e.g., `.js` file without `"type": "module"` in `package.json` or in an older Node.js environment).
fix
For Node.js projects, add `"type": "module"` to your `package.json` or rename your file to `.mjs`. Ensure your Node.js version is 16 or newer.
Upgrade
Version history
2.0.0latest on npm
Audit
Dependencies

No dependency data recorded yet.

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