@@ -23,19 +23,42 @@ npm install hast-util-truncate
2323
2424## Use
2525
26- Say we have the following HTML file , ` example.html ` :
26+ Say we have the following module , ` example.js ` :
2727
28- ``` html
29- ```
28+ ``` js
29+ import {h } from ' hastscript'
30+ import {truncate } from ' hast-util-truncate'
3031
31- And our module, ` example.js ` , contains:
32+ const tree = h (' p' , [
33+ ' Lorem ipsum dolor sit amet, ' ,
34+ h (' em' , ' consectetur' ),
35+ ' adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud'
36+ ])
3237
33- ``` js
38+ console . log ( truncate (tree, {ellipsis : ' … ' }));
3439```
3540
3641Now, running ` node example.js ` yields:
3742
38- ``` html
43+ ``` js
44+ {
45+ type: ' element' ,
46+ tagName: ' p' ,
47+ properties: {},
48+ children: [
49+ {type: ' text' , value: ' Lorem ipsum dolor sit amet, ' },
50+ {
51+ type: ' element' ,
52+ tagName: ' em' ,
53+ properties: {},
54+ children: [{type: ' text' , value: ' consectetur' }]
55+ },
56+ {
57+ type: ' text' ,
58+ value: ' adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim…'
59+ }
60+ ]
61+ }
3962```
4063
4164## API
@@ -47,10 +70,39 @@ There is no default export.
4770
4871Truncate the tree to a certain number of characters.
4972
50- ##### ` options `
73+ ###### ` options.size `
74+
75+ Number of characters to truncate to (` number ` , default: ` 140 ` ).
76+
77+ ###### ` options.ellipsis `
78+
79+ Value to use at truncation point (` string ` , optional).
80+
81+ ###### ` options.maxCharacterStrip `
82+
83+ How far to walk back (` number ` , default: ` 30 ` ).
84+ The algorithm attempts to break right after a word rather than the exact ` size ` .
85+ Take for example the ` | ` , which is the actual break defined by ` size ` , and the
86+ ` … ` is the location where the ellipsis is placed: ` This… an|d that ` .
87+ Breaking at ` | ` would at best look bad but could likely result in things such as
88+ ` ass… ` for ` assignment ` — which is not ideal.
89+ ` maxCharacterStrip ` defines how far back the algorithm will walk to find a
90+ pretty word break.
91+ This prevents a potential slow operation on larger ` size ` s without any
92+ whitespace.
93+ If ` maxCharacterStrip ` characters are walked back and no nice break point is
94+ found, the bad break point is used.
95+ Set ` maxCharacterStrip: 0 ` to not find a nice break.
96+
97+ ###### ` options.ignore `
98+
99+ Nodes to exclude from the resulting tree (` Array.<Node> ` ).
100+ These are not counted towards ` size ` .
51101
52102###### Returns
53103
104+ ` Node ` — Truncated copy of ` tree `
105+
54106## Security
55107
56108Use of ` hast-util-truncate ` should be safe if the tree is already safe and
0 commit comments