8
8
[ ![ Backers] [ backers-badge ]] [ collective ]
9
9
[ ![ Chat] [ chat-badge ]] [ chat ]
10
10
11
- ** [ hast] [ ] ** utility to truncate the tree to a certain number of characters.
11
+ [ hast] [ ] utility to truncate the tree to a certain number of characters.
12
12
13
- ## Install
13
+ ## Contents
14
+
15
+ * [ What is this?] ( #what-is-this )
16
+ * [ When should I use this?] ( #when-should-i-use-this )
17
+ * [ Install] ( #install )
18
+ * [ Use] ( #use )
19
+ * [ API] ( #api )
20
+ * [ ` truncate(tree, options?) ` ] ( #truncatetree-options )
21
+ * [ Types] ( #types )
22
+ * [ Compatibility] ( #compatibility )
23
+ * [ Security] ( #security )
24
+ * [ Related] ( #related )
25
+ * [ Contribute] ( #contribute )
26
+ * [ License] ( #license )
27
+
28
+ ## What is this?
29
+
30
+ This package is a utility that takes a [ hast] [ ] (HTML) syntax tree and truncates
31
+ it to a certain number of characters, while otherwise preserving the tree
32
+ structure.
14
33
15
- This package is [ ESM only] ( https://gist.github.com/sindresorhus/a39789f98801d908bbc7ff3ecc99d99c ) :
16
- Node 12+ is needed to use it and it must be ` import ` ed instead of ` require ` d.
34
+ ## When should I use this?
17
35
18
- [ npm] [ ] :
36
+ This is a small utility useful when you need to create a shorter version of a
37
+ potentially long document.
38
+
39
+ This utility is similar to [ ` hast-util-excerpt ` ] [ hast-util-excerpt ] , which
40
+ truncates a tree to a certain comment.
41
+
42
+ The rehype plugin
43
+ [ ` rehype-infer-description-meta ` ] [ rehype-infer-description-meta ]
44
+ wraps both this utility and ` hast-util-excerpt ` to figure out a description of a
45
+ document, for use with [ ` rehype-meta ` ] [ rehype-meta ] .
46
+
47
+ ## Install
48
+
49
+ This package is [ ESM only] [ esm ] .
50
+ In Node.js (version 12.20+, 14.14+, or 16.0+), install with [ npm] [ ] :
19
51
20
52
``` sh
21
53
npm install hast-util-truncate
22
54
```
23
55
56
+ In Deno with [ ` esm.sh ` ] [ esmsh ] :
57
+
58
+ ``` js
59
+ import {truncate } from " https://esm.sh/hast-util-truncate@1"
60
+ ```
61
+
62
+ In browsers with [ ` esm.sh ` ] [ esmsh ] :
63
+
64
+ ``` html
65
+ <script type =" module" >
66
+ import {truncate } from " https://esm.sh/hast-util-truncate@1?bundle"
67
+ </script >
68
+ ```
69
+
24
70
## Use
25
71
26
- Say we have the following module, ` example.js ` :
72
+ Say our module ` example.js ` looks as follows :
27
73
28
74
``` js
29
75
import {h } from ' hastscript'
@@ -38,7 +84,7 @@ const tree = h('p', [
38
84
console .log (truncate (tree, {ellipsis: ' …' }));
39
85
```
40
86
41
- Now, running ` node example.js ` yields:
87
+ …now running ` node example.js ` yields:
42
88
43
89
``` js
44
90
{
@@ -63,13 +109,17 @@ Now, running `node example.js` yields:
63
109
64
110
## API
65
111
66
- This package exports the following identifiers: ` truncate ` .
112
+ This package exports the identifier ` truncate ` .
67
113
There is no default export.
68
114
69
115
### ` truncate(tree, options?) `
70
116
71
117
Truncate the tree to a certain number of characters.
72
118
119
+ ##### ` options `
120
+
121
+ Configuration (optional).
122
+
73
123
###### ` options.size `
74
124
75
125
Number of characters to truncate to (` number ` , default: ` 140 ` ).
@@ -85,7 +135,7 @@ The algorithm attempts to break right after a word rather than the exact `size`.
85
135
Take for example the ` | ` , which is the actual break defined by ` size ` , and the
86
136
` … ` is the location where the ellipsis is placed: ` This… an|d that ` .
87
137
Breaking at ` | ` would at best look bad but could likely result in things such as
88
- ` ass… ` for ` assignment ` — which is not ideal.
138
+ ` ass… ` for ` assignment ` , which is not ideal.
89
139
` maxCharacterStrip ` defines how far back the algorithm will walk to find a
90
140
pretty word break.
91
141
This prevents a potential slow operation on larger ` size ` s without any
@@ -101,7 +151,19 @@ These are not counted towards `size`.
101
151
102
152
###### Returns
103
153
104
- ` Node ` — Truncated copy of ` tree `
154
+ Truncated copy of ` tree ` (` Node ` ).
155
+
156
+ ## Types
157
+
158
+ This package is fully typed with [ TypeScript] [ ] .
159
+ It exports the additional type ` Options ` .
160
+
161
+ ## Compatibility
162
+
163
+ Projects maintained by the unified collective are compatible with all maintained
164
+ versions of Node.js.
165
+ As of now, that is Node.js 12.20+, 14.14+, and 16.0+.
166
+ Our projects sometimes work with older versions, but this is not guaranteed.
105
167
106
168
## Security
107
169
@@ -111,10 +173,17 @@ When in doubt, use [`hast-util-sanitize`][sanitize].
111
173
112
174
## Related
113
175
176
+ * [ ` hast-util-excerpt ` ] [ hast-util-excerpt ]
177
+ — truncate the tree to a comment
178
+ * [ ` rehype-infer-description-meta ` ] [ rehype-infer-description-meta ]
179
+ — infer file metadata from the contents of the document
180
+ * [ ` rehype-meta ` ] [ rehype-meta ]
181
+ — add metadata to the head of a document
182
+
114
183
## Contribute
115
184
116
- See [ ` contributing.md ` in ` syntax-tree/.github ` ] [ contributing ] for ways to get
117
- started.
185
+ See [ ` contributing.md ` ] [ contributing ] in [ ` syntax-tree/.github ` ] [ health ] for
186
+ ways to get started.
118
187
See [ ` support.md ` ] [ support ] for ways to get help.
119
188
120
189
This project has a [ code of conduct] [ coc ] .
@@ -155,16 +224,30 @@ abide by its terms.
155
224
156
225
[ npm ] : https://docs.npmjs.com/cli/install
157
226
227
+ [ esm ] : https://gist.github.com/sindresorhus/a39789f98801d908bbc7ff3ecc99d99c
228
+
229
+ [ esmsh ] : https://esm.sh
230
+
231
+ [ typescript ] : https://www.typescriptlang.org
232
+
158
233
[ license ] : license
159
234
160
235
[ author ] : https://wooorm.com
161
236
162
- [ contributing ] : https://github.com/syntax-tree/.github/blob/HEAD/contributing.md
237
+ [ health ] : https://github.com/syntax-tree/.github
238
+
239
+ [ contributing ] : https://github.com/syntax-tree/.github/blob/main/contributing.md
163
240
164
- [ support ] : https://github.com/syntax-tree/.github/blob/HEAD /support.md
241
+ [ support ] : https://github.com/syntax-tree/.github/blob/main /support.md
165
242
166
- [ coc ] : https://github.com/syntax-tree/.github/blob/HEAD /code-of-conduct.md
243
+ [ coc ] : https://github.com/syntax-tree/.github/blob/main /code-of-conduct.md
167
244
168
245
[ sanitize ] : https://github.com/syntax-tree/hast-util-sanitize
169
246
170
247
[ hast ] : https://github.com/syntax-tree/hast
248
+
249
+ [ hast-util-excerpt ] : https://github.com/syntax-tree/hast-util-excerpt
250
+
251
+ [ rehype-infer-description-meta ] : https://github.com/rehypejs/rehype-infer-description-meta
252
+
253
+ [ rehype-meta ] : https://github.com/rehypejs/rehype-meta
0 commit comments