Skip to content

Commit 25f6b73

Browse files
committed
Add docs
1 parent 70e0fce commit 25f6b73

File tree

2 files changed

+61
-8
lines changed

2 files changed

+61
-8
lines changed

index.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
* @property {string} [ellipsis]
1212
* Value to use at truncation point.
1313
* @property {number} [maxCharacterStrip=30]
14+
* How far to walk back.
1415
* The algorithm attempts to break right after a word rather than the exact
1516
* `size`.
1617
* Take for example the `|`, which is the actual break defined by `size`, and
@@ -22,7 +23,7 @@
2223
* This prevents a potential slow operation on larger `size`s without any
2324
* whitespace.
2425
* If `maxCharacterStrip` characters are walked back and no nice break point
25-
* is found, the bad break point.
26+
* is found, the bad break point is used.
2627
* Set `maxCharacterStrip: 0` to not find a nice break.
2728
* @property {Content[]} [ignore=[]]
2829
* Nodes to exclude from the resulting tree.

readme.md

Lines changed: 59 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -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

3641
Now, 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

4871
Truncate 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

56108
Use of `hast-util-truncate` should be safe if the tree is already safe and

0 commit comments

Comments
 (0)