Rehype truncation
npm install rehype-truncate
![
``bash`
npm i rehype-truncate
Rehype truncate is a plugin for rehype that lets you truncate a hast.
`js
import unified from "unified";
import html from "rehype-parse";
import rehypeTruncate from "rehype-truncate";
import stringify from "rehype-stringify";
const document =
This is an example document to test html truncation.
;const processedDocument = unified()
.use(html)
.use(rehypeTruncate, { maxChars: 50 })
.use(stringify)
.processSync(document).contents;
console.log(processedDocument);
`This results in the following:
`html
Hello, World.
This is an example…
`Ignoring tags
You can configure
rehype-truncate to ignore content inside certain tag names by using the ignoreTags options.`js
import unified from "unified";
import html from "rehype-parse";
import rehypeTruncate from "rehype-truncate";
import stringify from "rehype-stringify";const document =
This is an example document to test html truncation.
This example has a list that should get ignored by the truncation character count.
;const processedDocument = unified()
.use(html)
.use(rehypeTruncate, { maxChars: 120, ignoreTags: ["ul"] })
.use(stringify)
.processSync(document).contents;
console.log(processedDocument);
`This results in the following:
`html
Hello, World.
This is an example document to test html truncation.
- List item 1
- List item 2
- List item 3
- List item 4
This example has a lis…
`Options
| Name | Type | Description |
| ------------ | ----------- | ------------------------------------------------- |
|
disable | boolean? | Disable truncation and return the document as is. |
| ellipses | string? | Specify a custom ellipses string (default "…"). |
| ignoreTags | string[]? | Ignore contents of certain tag names. |
| maxChars | number` | The number of characters to truncate. |