You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
feat: 1. Add an option called customNodeStrategy to the custom node truncate strategy #41
2. Rewrite the library to a purely functional library to prevent configurations from getting polluted.
3. Update the build toolchain to Vite and Vitetest.
* @param{Boolean}[options.decodeEntities] decode html entities(e.g. convert `&` to `&`) before
51
-
* counting length, default false
52
-
* @param{String|Array}[options.excludes] elements' selector you want ignore
53
-
* @param{Number}[options.length] how many letters(words if `byWords` is true)
54
-
* you want reserve
55
-
* @param{Boolean}[options.byWords] if true, length means how many words to reserve
56
-
* @param{Boolean|Number}[options.reserveLastWord] how to deal with when truncate in the middle of a word
57
-
* 1. by default, just cut at that position.
58
-
* 2. set it to true, with max exceed 10 letters can exceed to reserver the last word
59
-
* 3. set it to a positive number decide how many letters can exceed to reserve the last word
60
-
* 4. set it to negetive number to remove the last word if cut in the middle.
61
-
* @param{Boolean}[options.trimTheOnlyWord] whether to trim the only word when `reserveLastWord` < 0
62
-
* if reserveLastWord set to negetive number, and there is only one word in the html string,
63
-
* when trimTheOnlyWord set to true, the extra letters will be cutted if word's length longer
64
-
* than `length`.
65
-
* see issue #23 for more details
66
-
* @param{Boolean}[options.keepWhitespaces] keep whitespaces, by default continuous
67
-
* spaces will be replaced with one space
68
-
* set it true to reserve them, and continuous spaces will count as one
69
-
* @return{String}
52
+
* truncate-html full options object
70
53
*/
71
-
truncate(html, [length], [options])
54
+
interfaceIFullOptions {
55
+
/**
56
+
* remove all tags, default false
57
+
*/
58
+
stripTags:boolean
59
+
/**
60
+
* ellipsis sign, default '...'
61
+
*/
62
+
ellipsis:string
63
+
/**
64
+
* decode html entities(e.g. convert `&` to `&`) before counting length, default false
65
+
*/
66
+
decodeEntities:boolean
67
+
/**
68
+
* elements' selector you want ignore
69
+
*/
70
+
excludes:string|string[]
71
+
/**
72
+
* custom node strategy, default to Cheerio<AnyNode>
73
+
* * 'remove' to remove the node
74
+
* * 'keep' to keep the node(and anything inside it) anyway
75
+
* * Cheerio<AnyNode> truncate the returned node
76
+
* * undefined or any falsy value to truncate original node
77
+
*/
78
+
customNodeStrategy:ICustomNodeStrategy
79
+
/**
80
+
* how many letters(words if `byWords` is true) you want reserve
81
+
*/
82
+
length:number
83
+
/**
84
+
* if true, length means how many words to reserve
85
+
*/
86
+
byWords:boolean
87
+
/**
88
+
* how to deal with when truncate in the middle of a word
89
+
* 1. by default, just cut at that position.
90
+
* 2. set it to true, with max exceed 10 letters can exceed to reserver the last word
91
+
* 3. set it to a positive number decide how many letters can exceed to reserve the last word
92
+
* 4. set it to negative number to remove the last word if cut in the middle.
93
+
*/
94
+
reserveLastWord:boolean|number
95
+
/**
96
+
* if reserveLastWord set to negative number, and there is only one word in the html string, when trimTheOnlyWord set to true, the extra letters will be sliced if word's length longer than `length`.
97
+
* see issue #23 for more details
98
+
*/
99
+
trimTheOnlyWord:boolean
100
+
/**
101
+
* keep whitespaces, by default continuous paces will
102
+
* be replaced with one space, set it true to keep them
103
+
*/
104
+
keepWhitespaces:boolean
105
+
}
106
+
107
+
/**
108
+
* options interface for function
109
+
*/
110
+
typeIOptions=Partial<IFullOptions>
111
+
112
+
function truncate(html:string|CheerioAPI, length?:number|IOptions, truncateOptions?:IOptions):string
72
113
// and truncate.setup to change default options
73
-
truncate.setup(options)
114
+
truncate.setup(options:IOptions):void
74
115
```
75
116
76
117
### Defaultoptions
@@ -92,15 +133,15 @@ You can change default options by using `truncate.setup`
92
133
93
134
e.g.
94
135
95
-
```js
136
+
```ts
96
137
truncate.setup({ stripTags: true, length: 10 })
97
138
truncate('<p><img src="xxx.jpg">Hello from earth!</p>')
Thislibiswrittenwithtypescriptandhasatypedefinitionfilealongwithit. ~~Youmayneedtoupdateyour`tsconfig.json`byadding`"esModuleInterop": true`tothe`compilerOptions`ifyouencountersometypingerrors, see [#19](https://github.com/oe/truncate-html/issues/19).~~
122
164
165
+
```ts
166
+
import truncate, { type IOptions } from 'truncate-html'
167
+
168
+
169
+
const html = '<p><img src="abc.png"><i>italic<b>bold</b></i>This is a string</p> for test.'
// truncate summary tag that inside details tag instead of details tag
200
+
if (node.is('details')) {
201
+
return node.find('summary')
202
+
}
203
+
}
204
+
205
+
const html = '<div><img src="abc.png"><i>italic<b>bold</b></i><details><summary>Click me</summary><p>Some details</p></details>This is a string</div> for test.'
Ifthehtmlstringcontent'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.
@@ -240,6 +337,34 @@ truncate(html, {
240
337
})
241
338
// returns: <p> test for <p> 中文 str...</p>
242
339
// to fix this, see below for instructions
340
+
341
+
342
+
// custom node strategy to keep some special elements
343
+
varhtml='<p><img src="abc.png"><i>italic<b>bold</b></i>This is a string</p> for test.'
344
+
truncate(html, {
345
+
length: 10,
346
+
customNodeStrategy: node=> {
347
+
if (node.is('img')) {
348
+
return'remove'
349
+
}
350
+
if (node.is('i')) {
351
+
return'keep'
352
+
}
353
+
}
354
+
})
355
+
// returns: <p><i>italic<b>bold</b></i>This is a ...</p>
356
+
357
+
// custom node strategy to truncate summary instead of original node
0 commit comments