Releases: oe/truncate-html
custom truncate strategy
A new option customNodeStrategy has been added. Check readme for details.
For your requirements, the following code may achieve your needs:
import truncate, { type IOptions, type ICustomNodeStrategy } from 'truncate-html'
// argument node is a cheerio instance
const customNodeStrategy: ICustomNodeStrategy = node => {
// keep details and treat it as nothing inside
if (node.is('details')) {
return 'keep'
}
}
const html = '<div><details><summary>Click me</summary><p>Some details</p></details>other things</div>'
const options: IOptions = {
length: 3,
customNodeStrategy
}
truncate(html, options)
// => <div><details><summary>Click me</summary><p>Some details</p></details>oth...</div>ref: #41
Fix Cheerio's break changes
Cheerio released stable v1.0 a few days ago, but its APIs are very different from the latest beta version 1.0.0-rc.12, which breaks this lib.
The v1.1.2 release has pinned the cheerio version to 1.0.0-rc.12 to fix the cheerio problem.
Reference:
Cheerio spent years on beta version of 1.0, and then bring so many break changes in stable version? Unbelievable!
update cheerio to v1.0.0-rc.12
There are several changes:
- no vulnerabilities

- cheerio can now handle CJK characters correctly
- if you are using custom cheerio instance with this library, you should set the third parameter of
cheerio.loadtofalseto get rid of extra wrappers, see load for details. - better support for typescript
v1.0.0 has been released🎉
After a hard work, thanks to @calebeno v1.0.0 has been released
- added test cases, covered all statements/lines/branches, fixed some bugs, you can use truncate-html with faith
- added new option reserveLastWord to deal with when truncate in the middle of a word
- supports ES6 Modules, optimized for webpack/rollup
- upgraded
cheeriotov0.22.0
Added test cases and `options.reserveLastWord`
- added test cases, covered all statements/lines/branches, fixed some bugs.
- added new option
reserveLastWordto deal with when truncate in the middle of a word - supports ES6 Modules, optimized for webpack/rollup
Thanks to @cenoicc 's help
truncating by words just supported
By setting option byWords to true, then option length means word count.
extra whitespaces can be reserved now
By setting option keepWhitespaces to true, you can keep the extra whitespaces when dealing with tags like pre.
By default continuous whitespace will be replaced to one space, setting keepWhitespaces to true can disable it.