Skip to content

Commit 5c5ec84

Browse files
author
Saiya
committed
chore: update doc about keep in ICustomNodeStrategy
1 parent c634601 commit 5c5ec84

File tree

3 files changed

+21
-8
lines changed

3 files changed

+21
-8
lines changed

readme.md

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ Click **<https://npm.runkit.com/truncate-html>** to try.
4242
/**
4343
* custom node strategy, default to Cheerio<AnyNode>
4444
* * 'remove' to remove the node
45-
* * 'keep' to keep the node(and anything inside it) anyway
45+
* * 'keep' to keep the node(and anything inside it) anyway, and won't be counted as there is no text content in it
4646
* * Cheerio<AnyNode> truncate the returned node
4747
* * undefined or any falsy value to truncate original node
4848
*/
@@ -71,7 +71,7 @@ interface IFullOptions {
7171
/**
7272
* custom node strategy, default to Cheerio<AnyNode>
7373
* * 'remove' to remove the node
74-
* * 'keep' to keep the node(and anything inside it) anyway
74+
* * 'keep' to keep the node(and anything inside it) anyway, and won't be counted as there is no text content in it
7575
* * Cheerio<AnyNode> truncate the returned node
7676
* * undefined or any falsy value to truncate original node
7777
*/
@@ -180,7 +180,7 @@ truncate(html, options)
180180
### custom node truncate strategy
181181
In complex html string, you may want to keep some special elements and truncate the others. You can use `customNodeStrategy` to achieve this:
182182
* return `'remove'` to remove the node
183-
* `'keep'` to keep the node(and anything inside it) anyway
183+
* `keep` to keep the node(and anything inside it) anyway, and won't be counted as there is no text content in it
184184
* `Cheerio<AnyNode>` to truncate the returned node, or any falsy value to truncate the original node.
185185

186186
```ts
@@ -215,11 +215,9 @@ truncate(html, options)
215215
216216
```
217217

218-
219-
220218
### About final string length
221219

222-
If the html string content's length is shorter than `options.length`, then no ellipsis will be appended to the final html string. If longer, then the final string length will be `options.length` + `options.ellipsis`. And if you set `reserveLastWord` to true or none zero number, the final string will be various.
220+
If the html string content's length is shorter than `options.length`, then no ellipsis will be appended to the final html string. If longer, then the final string length will be `options.length` + `options.ellipsis`. And if you set `reserveLastWord` to true or none zero number or using `customNodeStrategy`, the final string will be various.
223221

224222
### About html comments
225223

src/truncate.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import cheerio, { CheerioAPI, Cheerio, AnyNode } from 'cheerio'
33
/**
44
* custom node strategy, default to Cheerio<AnyNode>
55
* * 'remove' to remove the node
6-
* * 'keep' to keep the node(and anything inside it) anyway
6+
* * 'keep' to keep the node(and anything inside it) anyway, and won't be counted as there is no text content in it
77
* * Cheerio<AnyNode> truncate the returned node
88
* * undefined or any falsy value to truncate original node
99
*/
@@ -32,7 +32,7 @@ export interface IFullOptions {
3232
/**
3333
* custom node strategy, default to Cheerio<AnyNode>
3434
* * 'remove' to remove the node
35-
* * 'keep' to keep the node(and anything inside it) anyway
35+
* * 'keep' to keep the node(and anything inside it) anyway, and won't be counted as there is no text content in it
3636
* * Cheerio<AnyNode> truncate the returned node
3737
* * undefined or any falsy value to truncate original node
3838
*/

test/demo.ts

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,3 +88,18 @@ console.log(truncate(html, options))
8888

8989
// const expected = '123456...'
9090
// console.log(truncate(testString, 6))
91+
92+
// argument node is a cheerio instance
93+
const customNodeStrategy2: ICustomNodeStrategy = node => {
94+
// truncate summary tag that inside details tag instead of details tag
95+
if (node.is('details')) {
96+
return 'keep'
97+
}
98+
}
99+
100+
html = '<div><details><summary>Click me</summary><p>Some details</p></details>other things</div>'
101+
102+
console.log(truncate(html, {
103+
length: 3,
104+
customNodeStrategy: customNodeStrategy2
105+
}))

0 commit comments

Comments
 (0)