Skip to content

Commit a210768

Browse files
committed
Add improved docs
1 parent c704ae4 commit a210768

File tree

1 file changed

+98
-15
lines changed

1 file changed

+98
-15
lines changed

readme.md

Lines changed: 98 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -8,22 +8,68 @@
88
[![Backers][backers-badge]][collective]
99
[![Chat][chat-badge]][chat]
1010

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.
1212

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.
1433

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?
1735

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][]:
1951

2052
```sh
2153
npm install hast-util-truncate
2254
```
2355

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+
2470
## Use
2571

26-
Say we have the following module, `example.js`:
72+
Say our module `example.js` looks as follows:
2773

2874
```js
2975
import {h} from 'hastscript'
@@ -38,7 +84,7 @@ const tree = h('p', [
3884
console.log(truncate(tree, {ellipsis: ''}));
3985
```
4086

41-
Now, running `node example.js` yields:
87+
…now running `node example.js` yields:
4288

4389
```js
4490
{
@@ -63,13 +109,17 @@ Now, running `node example.js` yields:
63109

64110
## API
65111

66-
This package exports the following identifiers: `truncate`.
112+
This package exports the identifier `truncate`.
67113
There is no default export.
68114

69115
### `truncate(tree, options?)`
70116

71117
Truncate the tree to a certain number of characters.
72118

119+
##### `options`
120+
121+
Configuration (optional).
122+
73123
###### `options.size`
74124

75125
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`.
85135
Take for example the `|`, which is the actual break defined by `size`, and the
86136
`` is the location where the ellipsis is placed: `This… an|d that`.
87137
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.
89139
`maxCharacterStrip` defines how far back the algorithm will walk to find a
90140
pretty word break.
91141
This prevents a potential slow operation on larger `size`s without any
@@ -101,7 +151,19 @@ These are not counted towards `size`.
101151

102152
###### Returns
103153

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.
105167

106168
## Security
107169

@@ -111,10 +173,17 @@ When in doubt, use [`hast-util-sanitize`][sanitize].
111173

112174
## Related
113175

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+
114183
## Contribute
115184

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.
118187
See [`support.md`][support] for ways to get help.
119188

120189
This project has a [code of conduct][coc].
@@ -155,16 +224,30 @@ abide by its terms.
155224

156225
[npm]: https://docs.npmjs.com/cli/install
157226

227+
[esm]: https://gist.github.com/sindresorhus/a39789f98801d908bbc7ff3ecc99d99c
228+
229+
[esmsh]: https://esm.sh
230+
231+
[typescript]: https://www.typescriptlang.org
232+
158233
[license]: license
159234

160235
[author]: https://wooorm.com
161236

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
163240

164-
[support]: https://github.com/syntax-tree/.github/blob/HEAD/support.md
241+
[support]: https://github.com/syntax-tree/.github/blob/main/support.md
165242

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
167244

168245
[sanitize]: https://github.com/syntax-tree/hast-util-sanitize
169246

170247
[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

Comments
 (0)