• Home
  • History
  • Annotate
Name Date Size #Lines LOC

..22-Nov-2021-

licenseH A D22-Nov-20211.1 KiB2318

package.jsonH A D22-Nov-20212.5 KiB104103

readme.mdH A D22-Nov-20217.4 KiB234158

readme.md

1# unist-util-visit-parents
2
3[![Build][build-badge]][build]
4[![Coverage][coverage-badge]][coverage]
5[![Downloads][downloads-badge]][downloads]
6[![Size][size-badge]][size]
7[![Sponsors][sponsors-badge]][collective]
8[![Backers][backers-badge]][collective]
9[![Chat][chat-badge]][chat]
10
11[**unist**][unist] utility to visit nodes, with ancestral information.
12
13## Install
14
15[npm][]:
16
17```sh
18npm install unist-util-visit-parents
19```
20
21## Use
22
23```js
24var remark = require('remark')
25var visit = require('unist-util-visit-parents')
26
27var tree = remark.parse('Some _emphasis_, **importance**, and `code`.')
28
29visit(tree, 'strong', visitor)
30
31function visitor(node, ancestors) {
32  console.log(ancestors)
33}
34```
35
36Yields:
37
38```js
39[ { type: 'root', children: [ [Object] ] },
40  { type: 'paragraph',
41    children:
42     [ [Object],
43       [Object],
44       [Object],
45       [Object],
46       [Object],
47       [Object],
48       [Object] ] } ]
49```
50
51## API
52
53### `visit(tree[, test], visitor[, reverse])`
54
55Visit nodes ([*inclusive descendants*][descendant] of [`tree`][tree]), with
56ancestral information.
57Optionally filtering nodes.
58Optionally in reverse.
59
60This algorithm performs [*depth-first*][depth-first]
61[*tree traversal*][tree-traversal] in [*preorder*][preorder] (**NLR**), or
62if `reverse` is given, in *reverse preorder* (**NRL**).
63
64Walking the tree is an intensive task.
65Make use of the return values of the visitor when possible.
66Instead of walking a tree multiple times with different `test`s, walk it once
67without a test, and use [`unist-util-is`][is] to check if a node matches a test,
68and then perform different operations.
69
70###### Parameters
71
72*   `tree` ([`Node`][node]) — [Tree][] to traverse
73*   `test` ([`Test`][is], optional) — [`is`][is]-compatible test (such as a
74    [type][])
75*   `visitor` ([Function][visitor]) — Function invoked when a node is found
76    that passes `test`
77*   `reverse` (`boolean`, default: `false`) — The tree is traversed in
78    [preorder][] (NLR), visiting the node itself, then its [head][], etc.
79    When `reverse` is passed, the tree is traversed in reverse preorder (NRL):
80    the node itself is visited, then its [tail][], etc.
81
82#### `next? = visitor(node, ancestors)`
83
84Invoked when a node (matching `test`, if given) is found.
85
86Visitors are free to transform `node`.
87They can also transform the [parent][] of node (the last of `ancestors`).
88Replacing `node` itself, if `visit.SKIP` is not returned, still causes its
89[descendant][]s to be visited.
90If adding or removing previous [sibling][]s (or next siblings, in case of
91`reverse`) of `node`, `visitor` should return a new [`index`][index] (`number`)
92to specify the sibling to traverse after `node` is traversed.
93Adding or removing next siblings of `node` (or previous siblings, in case of
94reverse) is handled as expected without needing to return a new `index`.
95Removing the `children` property of an ancestor still results in them being
96traversed.
97
98###### Parameters
99
100*   `node` ([`Node`][node]) — Found node
101*   `ancestors` (`Array.<Node>`) — [Ancestor][]s of `node`
102
103##### Returns
104
105The return value can have the following forms:
106
107*   [`index`][index] (`number`) — Treated as a tuple of `[CONTINUE, index]`
108*   `action` (`*`) — Treated as a tuple of `[action]`
109*   `tuple` (`Array.<*>`) — List with one or two values, the first an `action`,
110    the second and `index`.
111    Note that passing a tuple only makes sense if the `action` is `SKIP`.
112    If the `action` is `EXIT`, that action can be returned.
113    If the `action` is `CONTINUE`, `index` can be returned.
114
115###### `action`
116
117An action can have the following values:
118
119*   `visit.EXIT` (`false`) — Stop traversing immediately
120*   `visit.CONTINUE` (`true`) — Continue traversing as normal (same behaviour
121    as not returning anything)
122*   `visit.SKIP` (`'skip'`) — Do not traverse this node’s children; continue
123    with the specified index
124
125###### `index`
126
127[`index`][index] (`number`) — Move to the sibling at `index` next (after `node`
128itself is completely traversed).
129Useful if mutating the tree, such as removing the node the visitor is currently
130on, or any of its previous siblings (or next siblings, in case of `reverse`)
131Results less than `0` or greater than or equal to `children.length` stop
132traversing the parent
133
134## Related
135
136*   [`unist-util-visit`](https://github.com/syntax-tree/unist-util-visit)
137    — Like `visit-parents`, but with one parent
138*   [`unist-util-filter`](https://github.com/syntax-tree/unist-util-filter)
139    — Create a new tree with all nodes that pass a test
140*   [`unist-util-map`](https://github.com/syntax-tree/unist-util-map)
141    — Create a new tree with all nodes mapped by a given function
142*   [`unist-util-flatmap`](https://gitlab.com/staltz/unist-util-flatmap)
143    — Create a new tree by mapping (to an array) with the given function
144*   [`unist-util-remove`](https://github.com/syntax-tree/unist-util-remove)
145    — Remove nodes from a tree that pass a test
146*   [`unist-util-select`](https://github.com/syntax-tree/unist-util-select)
147    — Select nodes with CSS-like selectors
148
149## Contribute
150
151See [`contributing.md` in `syntax-tree/.github`][contributing] for ways to get
152started.
153See [`support.md`][support] for ways to get help.
154
155This project has a [code of conduct][coc].
156By interacting with this repository, organization, or community you agree to
157abide by its terms.
158
159## License
160
161[MIT][license] © [Titus Wormer][author]
162
163<!-- Definition -->
164
165[build-badge]: https://img.shields.io/travis/syntax-tree/unist-util-visit-parents.svg
166
167[build]: https://travis-ci.org/syntax-tree/unist-util-visit-parents
168
169[coverage-badge]: https://img.shields.io/codecov/c/github/syntax-tree/unist-util-visit-parents.svg
170
171[coverage]: https://codecov.io/github/syntax-tree/unist-util-visit-parents
172
173[downloads-badge]: https://img.shields.io/npm/dm/unist-util-visit-parents.svg
174
175[downloads]: https://www.npmjs.com/package/unist-util-visit-parents
176
177[size-badge]: https://img.shields.io/bundlephobia/minzip/unist-util-visit-parents.svg
178
179[size]: https://bundlephobia.com/result?p=unist-util-visit-parents
180
181[sponsors-badge]: https://opencollective.com/unified/sponsors/badge.svg
182
183[backers-badge]: https://opencollective.com/unified/backers/badge.svg
184
185[collective]: https://opencollective.com/unified
186
187[chat-badge]: https://img.shields.io/badge/chat-spectrum-7b16ff.svg
188
189[chat]: https://spectrum.chat/unified/syntax-tree
190
191[npm]: https://docs.npmjs.com/cli/install
192
193[license]: license
194
195[author]: https://wooorm.com
196
197[unist]: https://github.com/syntax-tree/unist
198
199[node]: https://github.com/syntax-tree/unist#node
200
201[visitor]: #next--visitornode-ancestors
202
203[contributing]: https://github.com/syntax-tree/.github/blob/HEAD/contributing.md
204
205[support]: https://github.com/syntax-tree/.github/blob/HEAD/support.md
206
207[coc]: https://github.com/syntax-tree/.github/blob/HEAD/code-of-conduct.md
208
209[is]: https://github.com/syntax-tree/unist-util-is
210
211[depth-first]: https://github.com/syntax-tree/unist#depth-first-traversal
212
213[tree-traversal]: https://github.com/syntax-tree/unist#tree-traversal
214
215[preorder]: https://github.com/syntax-tree/unist#preorder
216
217[descendant]: https://github.com/syntax-tree/unist#descendant
218
219[head]: https://github.com/syntax-tree/unist#head
220
221[tail]: https://github.com/syntax-tree/unist#tail
222
223[parent]: https://github.com/syntax-tree/unist#parent-1
224
225[sibling]: https://github.com/syntax-tree/unist#sibling
226
227[index]: https://github.com/syntax-tree/unist#index
228
229[ancestor]: https://github.com/syntax-tree/unist#ancestor
230
231[tree]: https://github.com/syntax-tree/unist#tree
232
233[type]: https://github.com/syntax-tree/unist#type
234