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-truncateVerified import paths — ran on the pinned version, not inferred.
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.
Upgrade your Node.js environment to version 16 or a later LTS release.
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.
Always import symbols directly from the main package entry point: `import { symbol } from 'hast-util-truncate'`.Update `@types/hast` to its latest compatible version (e.g., `npm install @types/hast@latest`).
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`).Ensure you are using `import { truncate } from 'hast-util-truncate'` as there is no default export. Also, verify Node.js version and ESM context.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.
No dependency data recorded yet.