@@ -23,19 +23,42 @@ npm install hast-util-truncate
23
23
24
24
## Use
25
25
26
- Say we have the following HTML file , ` example.html ` :
26
+ Say we have the following module , ` example.js ` :
27
27
28
- ``` html
29
- ```
28
+ ``` js
29
+ import {h } from ' hastscript'
30
+ import {truncate } from ' hast-util-truncate'
30
31
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
+ ])
32
37
33
- ``` js
38
+ console . log ( truncate (tree, {ellipsis : ' … ' }));
34
39
```
35
40
36
41
Now, running ` node example.js ` yields:
37
42
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
+ }
39
62
```
40
63
41
64
## API
@@ -47,10 +70,39 @@ There is no default export.
47
70
48
71
Truncate the tree to a certain number of characters.
49
72
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 ` .
51
101
52
102
###### Returns
53
103
104
+ ` Node ` — Truncated copy of ` tree `
105
+
54
106
## Security
55
107
56
108
Use of ` hast-util-truncate ` should be safe if the tree is already safe and
0 commit comments