Skip to content

Commit e9e2b1d

Browse files
committed
Add improved docs
1 parent e7a4327 commit e9e2b1d

File tree

1 file changed

+97
-17
lines changed

1 file changed

+97
-17
lines changed

readme.md

Lines changed: 97 additions & 17 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 comment.
11+
[hast][] utility to truncate the tree to a comment.
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+
* [`excerpt(tree, options?)`](#excerpttree-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 comment, while otherwise preserving the tree structure.
1432

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.
33+
## When should I use this?
1734

18-
[npm][]:
35+
This is a small utility useful when you need to create a shorter version of a
36+
potentially long document, and want authors to be able to mark where that
37+
version ends.
38+
39+
This utility is similar to [`hast-util-truncate`][hast-util-truncate], which
40+
truncates a tree to a certain number of characters.
41+
42+
The rehype plugin
43+
[`rehype-infer-description-meta`][rehype-infer-description-meta]
44+
wraps both this utility and `hast-util-truncate` to figure out a description of
45+
a 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-excerpt
2254
```
2355

56+
In Deno with [`esm.sh`][esmsh]:
57+
58+
```js
59+
import {excerpt} from "https://esm.sh/hast-util-excerpt@1"
60+
```
61+
62+
In browsers with [`esm.sh`][esmsh]:
63+
64+
```html
65+
<script type="module">
66+
import {excerpt} from "https://esm.sh/hast-util-excerpt@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 {u} from 'unist-builder'
@@ -41,7 +87,7 @@ const tree = h('p', [
4187
console.log(excerpt(tree));
4288
```
4389

44-
Now, running `node example.js` yields:
90+
…now running `node example.js` yields:
4591

4692
```js
4793
{
@@ -67,13 +113,17 @@ Now, running `node example.js` yields:
67113

68114
## API
69115

70-
This package exports the following identifiers: `excerpt`.
116+
This package exports the identifier `excerpt`.
71117
There is no default export.
72118

73119
### `excerpt(tree, options?)`
74120

75121
Truncate the tree to a comment.
76122

123+
##### `options`
124+
125+
Configuration (optional).
126+
77127
###### `options.comment`
78128

79129
Comment value to search for (`string`, default: `'more'`).
@@ -93,23 +143,39 @@ These are not counted towards `size`.
93143

94144
###### Returns
95145

96-
`Node?` — Truncated copy of `tree` if there’s a comment, `undefined` otherwise.
146+
Truncated copy of `tree` if there’s a comment, `undefined` otherwise (`Node?`).
147+
148+
## Types
149+
150+
This package is fully typed with [TypeScript][].
151+
It exports the additional type `Options`.
152+
153+
## Compatibility
154+
155+
Projects maintained by the unified collective are compatible with all maintained
156+
versions of Node.js.
157+
As of now, that is Node.js 12.20+, 14.14+, and 16.0+.
158+
Our projects sometimes work with older versions, but this is not guaranteed.
97159

98160
## Security
99161

100162
Use of `hast-util-excerpt` should be safe if the tree is already safe and
101163
you’re not using user content in options.
102-
When in doubt, use [`hast-util-sanitize`][sanitize].
164+
When in doubt, use [`hast-util-sanitize`][hast-util-sanitize].
103165

104166
## Related
105167

106168
* [`hast-util-truncate`](https://github.com/syntax-tree/hast-util-truncate)
107-
— Truncate the tree to a certain number of characters
169+
— truncate the tree to a number of characters
170+
* [`rehype-infer-description-meta`][rehype-infer-description-meta]
171+
— infer file metadata from the contents of the document
172+
* [`rehype-meta`][rehype-meta]
173+
— add metadata to the head of a document
108174

109175
## Contribute
110176

111-
See [`contributing.md` in `syntax-tree/.github`][contributing] for ways to get
112-
started.
177+
See [`contributing.md`][contributing] in [`syntax-tree/.github`][health] for
178+
ways to get started.
113179
See [`support.md`][support] for ways to get help.
114180

115181
This project has a [code of conduct][coc].
@@ -150,16 +216,30 @@ abide by its terms.
150216

151217
[npm]: https://docs.npmjs.com/cli/install
152218

219+
[esm]: https://gist.github.com/sindresorhus/a39789f98801d908bbc7ff3ecc99d99c
220+
221+
[esmsh]: https://esm.sh
222+
223+
[typescript]: https://www.typescriptlang.org
224+
153225
[license]: license
154226

155227
[author]: https://wooorm.com
156228

157-
[contributing]: https://github.com/syntax-tree/.github/blob/HEAD/contributing.md
229+
[health]: https://github.com/syntax-tree/.github
158230

159-
[support]: https://github.com/syntax-tree/.github/blob/HEAD/support.md
231+
[contributing]: https://github.com/syntax-tree/.github/blob/main/contributing.md
160232

161-
[coc]: https://github.com/syntax-tree/.github/blob/HEAD/code-of-conduct.md
233+
[support]: https://github.com/syntax-tree/.github/blob/main/support.md
162234

163-
[sanitize]: https://github.com/syntax-tree/hast-util-sanitize
235+
[coc]: https://github.com/syntax-tree/.github/blob/main/code-of-conduct.md
164236

165237
[hast]: https://github.com/syntax-tree/hast
238+
239+
[hast-util-sanitize]: https://github.com/syntax-tree/hast-util-sanitize
240+
241+
[hast-util-truncate]: https://github.com/syntax-tree/hast-util-truncate
242+
243+
[rehype-infer-description-meta]: https://github.com/rehypejs/rehype-infer-description-meta
244+
245+
[rehype-meta]: https://github.com/rehypejs/rehype-meta

0 commit comments

Comments
 (0)