8
8
[ ![ Backers] [ backers-badge ]] [ collective ]
9
9
[ ![ Chat] [ chat-badge ]] [ chat ]
10
10
11
- [ ** unist** ] [ unist ] utility to visit nodes .
11
+ [ unist] [ ] utility to walk the tree .
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
+ * [ ` 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.
14
31
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
17
39
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] [ ] :
19
42
20
43
``` sh
21
44
npm install unist-util-visit
22
45
```
23
46
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
+
24
61
## Use
25
62
26
63
``` js
@@ -42,21 +79,14 @@ visit(tree, 'leaf', (node) => {
42
79
Yields:
43
80
44
81
``` 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' }
54
85
```
55
86
56
87
## API
57
88
58
- This package exports the following identifiers: ` visit ` , ` CONTINUE ` , ` SKIP ` , and
59
- ` EXIT ` .
89
+ This package exports the identifiers ` visit ` , ` CONTINUE ` , ` SKIP ` , and ` EXIT ` .
60
90
There is no default export.
61
91
62
92
### ` visit(tree[, test], visitor[, reverse]) `
@@ -67,30 +97,45 @@ but `visitor` has a different signature.
67
97
#### ` next? = visitor(node, index, parent) `
68
98
69
99
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 ` .
72
108
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.
74
119
75
120
## Related
76
121
77
122
* [ ` unist-util-visit-parents ` ] [ vp ]
78
- — Like ` visit ` , but with a stack of parents
123
+ — walk the tree with a stack of parents
79
124
* [ ` 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
81
126
* [ ` 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
83
128
* [ ` 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
85
130
* [ ` 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
87
132
* [ ` 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
89
134
90
135
## Contribute
91
136
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.
94
139
See [ ` support.md ` ] [ support ] for ways to get help.
95
140
96
141
This project has a [ code of conduct] [ coc ] .
@@ -131,15 +176,23 @@ abide by its terms.
131
176
132
177
[ npm ] : https://docs.npmjs.com/cli/install
133
178
179
+ [ esm ] : https://gist.github.com/sindresorhus/a39789f98801d908bbc7ff3ecc99d99c
180
+
181
+ [ esmsh ] : https://esm.sh
182
+
183
+ [ typescript ] : https://www.typescriptlang.org
184
+
134
185
[ license ] : license
135
186
136
187
[ author ] : https://wooorm.com
137
188
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
139
192
140
- [ support ] : https://github.com/syntax-tree/.github/blob/HEAD /support.md
193
+ [ support ] : https://github.com/syntax-tree/.github/blob/main /support.md
141
194
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
143
196
144
197
[ unist ] : https://github.com/syntax-tree/unist
145
198
0 commit comments