Skip to content

Commit 6789ad5

Browse files
committed
Add improved docs
1 parent 2f0494b commit 6789ad5

File tree

1 file changed

+83
-30
lines changed

1 file changed

+83
-30
lines changed

readme.md

+83-30
Original file line numberDiff line numberDiff line change
@@ -8,19 +8,56 @@
88
[![Backers][backers-badge]][collective]
99
[![Chat][chat-badge]][chat]
1010

11-
[**unist**][unist] utility to visit nodes.
11+
[unist][] utility to walk the tree.
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+
* [`visit(tree[, test], visitor[, reverse])`](#visittree-test-visitor-reverse)
21+
* [Types](#types)
22+
* [Compatibility](#compatibility)
23+
* [Related](#related)
24+
* [Contribute](#contribute)
25+
* [License](#license)
26+
27+
## What is this?
28+
29+
This is a very important utility for working with unist as it lets you walk the
30+
tree.
1431

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.
32+
## When should I use this?
33+
34+
You can use this utility when you want to walk the tree.
35+
You can use [`unist-util-visit-parents`][vp] if you care about the entire stack
36+
of parents.
37+
38+
## Install
1739

18-
[npm][]:
40+
This package is [ESM only][esm].
41+
In Node.js (version 12.20+, 14.14+, 16.0+, 18.0+), install with [npm][]:
1942

2043
```sh
2144
npm install unist-util-visit
2245
```
2346

47+
In Deno with [`esm.sh`][esmsh]:
48+
49+
```js
50+
import {visit} from "https://esm.sh/unist-util-visit@4"
51+
```
52+
53+
In browsers with [`esm.sh`][esmsh]:
54+
55+
```html
56+
<script type="module">
57+
import {visit} from "https://esm.sh/unist-util-visit@4?bundle"
58+
</script>
59+
```
60+
2461
## Use
2562

2663
```js
@@ -42,21 +79,14 @@ visit(tree, 'leaf', (node) => {
4279
Yields:
4380

4481
```js
45-
{ type: 'leaf', value: '1' }
46-
{ type: 'leaf', value: '2' }
47-
{ type: 'leaf', value: '3' }
48-
```
49-
50-
Note: this example also uses `unist-builder`, to run the example ensure both `unist-builder` and `unist-util-visit` are installed:
51-
52-
```sh
53-
npm install unist-builder unist-util-visit
82+
{type: 'leaf', value: '1'}
83+
{type: 'leaf', value: '2'}
84+
{type: 'leaf', value: '3'}
5485
```
5586

5687
## API
5788

58-
This package exports the following identifiers: `visit`, `CONTINUE`, `SKIP`, and
59-
`EXIT`.
89+
This package exports the identifiers `visit`, `CONTINUE`, `SKIP`, and `EXIT`.
6090
There is no default export.
6191

6292
### `visit(tree[, test], visitor[, reverse])`
@@ -67,30 +97,45 @@ but `visitor` has a different signature.
6797
#### `next? = visitor(node, index, parent)`
6898

6999
Instead of being passed an array of ancestors, `visitor` is called with the
70-
`node`’s [`index`][index] and its [`parent`][parent]. The optional return value
71-
`next` is documented in [`unist-util-visit-parents`][vp]’s readme.
100+
`node`’s [`index`][index] and its [`parent`][parent].
101+
102+
Please see [`unist-util-visit-parents`][vp].
103+
104+
## Types
105+
106+
This package is fully typed with [TypeScript][].
107+
It exports the additional types `Test`, `VisitorResult`, and `Visitor`.
72108

73-
Otherwise the same as [`unist-util-visit-parents`][vp].
109+
It also exports the types `BuildVisitor<Tree extends Node = Node, Check extends
110+
Test = string>` to properly type visitors from a tree and a test, from
111+
`unist-util-visit-parents/complex-types.d.ts`.
112+
113+
## Compatibility
114+
115+
Projects maintained by the unified collective are compatible with all maintained
116+
versions of Node.js.
117+
As of now, that is Node.js 12.20+, 14.14+, 16.0+, and 18.0+.
118+
Our projects sometimes work with older versions, but this is not guaranteed.
74119

75120
## Related
76121

77122
* [`unist-util-visit-parents`][vp]
78-
Like `visit`, but with a stack of parents
123+
walk the tree with a stack of parents
79124
* [`unist-util-filter`](https://github.com/syntax-tree/unist-util-filter)
80-
Create a new tree with all nodes that pass a test
125+
create a new tree with all nodes that pass a test
81126
* [`unist-util-map`](https://github.com/syntax-tree/unist-util-map)
82-
Create a new tree with all nodes mapped by a given function
127+
create a new tree with all nodes mapped by a given function
83128
* [`unist-util-flatmap`](https://gitlab.com/staltz/unist-util-flatmap)
84-
Create a new tree by mapping (to an array) with the given function
129+
create a new tree by mapping (to an array) with the given function
85130
* [`unist-util-remove`](https://github.com/syntax-tree/unist-util-remove)
86-
Remove nodes from a tree that pass a test
131+
remove nodes from a tree that pass a test
87132
* [`unist-util-select`](https://github.com/syntax-tree/unist-util-select)
88-
Select nodes with CSS-like selectors
133+
select nodes with CSS-like selectors
89134

90135
## Contribute
91136

92-
See [`contributing.md` in `syntax-tree/.github`][contributing] for ways to get
93-
started.
137+
See [`contributing.md`][contributing] in [`syntax-tree/.github`][health] for
138+
ways to get started.
94139
See [`support.md`][support] for ways to get help.
95140

96141
This project has a [code of conduct][coc].
@@ -131,15 +176,23 @@ abide by its terms.
131176

132177
[npm]: https://docs.npmjs.com/cli/install
133178

179+
[esm]: https://gist.github.com/sindresorhus/a39789f98801d908bbc7ff3ecc99d99c
180+
181+
[esmsh]: https://esm.sh
182+
183+
[typescript]: https://www.typescriptlang.org
184+
134185
[license]: license
135186

136187
[author]: https://wooorm.com
137188

138-
[contributing]: https://github.com/syntax-tree/.github/blob/HEAD/contributing.md
189+
[health]: https://github.com/syntax-tree/.github
190+
191+
[contributing]: https://github.com/syntax-tree/.github/blob/main/contributing.md
139192

140-
[support]: https://github.com/syntax-tree/.github/blob/HEAD/support.md
193+
[support]: https://github.com/syntax-tree/.github/blob/main/support.md
141194

142-
[coc]: https://github.com/syntax-tree/.github/blob/HEAD/code-of-conduct.md
195+
[coc]: https://github.com/syntax-tree/.github/blob/main/code-of-conduct.md
143196

144197
[unist]: https://github.com/syntax-tree/unist
145198

0 commit comments

Comments
 (0)