1 # unist-util-visit-parents
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]
11 [**unist**][unist] utility to visit nodes, with ancestral information.
18 npm install unist-util-visit-parents
24 var remark = require('remark')
25 var visit = require('unist-util-visit-parents')
27 var tree = remark.parse('Some _emphasis_, **importance**, and `code`.')
29 visit(tree, 'strong', visitor)
31 function visitor(node, ancestors) {
32 console.log(ancestors)
39 [ { type: 'root', children: [ [Object] ] },
53 ### `visit(tree[, test], visitor[, reverse])`
55 Visit nodes ([**inclusive descendants**][descendant] of [`tree`][tree]), with
56 ancestral information. Optionally filtering nodes. Optionally in reverse.
60 * `tree` ([`Node`][node]) — [Tree][] to traverse
61 * `test` ([`Test`][is], optional) — [`is`][is]-compatible test (such as a
63 * `visitor` ([Function][visitor]) — Function invoked when a node is found
65 * `reverse` (`boolean`, default: `false`) — The tree is walked in [preorder][]
66 (NLR), visiting the node itself, then its [head][], etc.
67 When `reverse` is passed, the tree is stilled walked in preorder, but now
68 in NRL (the node itself, then its [tail][], etc.)
70 #### `next? = visitor(node, ancestors)`
72 Invoked when a node (matching `test`, if given) is found.
74 Visitors are free to transform `node`.
75 They can also transform the [parent][] of node (the last of `ancestors`).
76 Replacing `node` itself, if `visit.SKIP` is not returned, still causes its
77 [descendant][]s to be visited.
78 If adding or removing previous [sibling][]s (or next siblings, in case of
79 `reverse`) of `node`, `visitor` should return a new [`index`][index] (`number`)
80 to specify the sibling to traverse after `node` is traversed.
81 Adding or removing next siblings of `node` (or previous siblings, in case of
82 reverse) is handled as expected without needing to return a new `index`.
83 Removing the `children` property of parent still results in them being
88 * `node` ([`Node`][node]) — Found node
89 * `ancestors` (`Array.<Node>`) — [Ancestor][]s of `node`
93 The return value can have the following forms:
95 * [`index`][index] (`number`) — Treated as a tuple of `[CONTINUE, index]`
96 * `action` (`*`) — Treated as a tuple of `[action]`
97 * `tuple` (`Array.<*>`) — List with one or two values, the first an `action`,
98 the second and `index`.
99 Note that passing a tuple only makes sense if the `action` is `SKIP`.
100 If the `action` is `EXIT`, that action can be returned.
101 If the `action` is `CONTINUE`, `index` can be returned.
105 An action can have the following values:
107 * `visit.EXIT` (`false`) — Stop traversing immediately
108 * `visit.CONTINUE` (`true`) — Continue traversing as normal (same behaviour
109 as not returning anything)
110 * `visit.SKIP` (`'skip'`) — Do not traverse this node’s children; continue
111 with the specified index
115 [`index`][index] (`number`) — Move to the sibling at `index` next (after `node`
116 itself is completely traversed).
117 Useful if mutating the tree, such as removing the node the visitor is currently
118 on, or any of its previous siblings (or next siblings, in case of `reverse`)
119 Results less than `0` or greater than or equal to `children.length` stop
120 traversing the parent
124 * [`unist-util-visit`](https://github.com/syntax-tree/unist-util-visit)
125 — Like `visit-parents`, but with one parent
126 * [`unist-util-filter`](https://github.com/eush77/unist-util-filter)
127 — Create a new tree with all nodes that pass a test
128 * [`unist-util-map`](https://github.com/syntax-tree/unist-util-map)
129 — Create a new tree with all nodes mapped by a given function
130 * [`unist-util-flatmap`](https://gitlab.com/staltz/unist-util-flatmap)
131 — Create a new tree by mapping (to an array) with the provided function and
133 * [`unist-util-remove`](https://github.com/eush77/unist-util-remove)
134 — Remove nodes from a tree that pass a test
135 * [`unist-util-select`](https://github.com/eush77/unist-util-select)
136 — Select nodes with CSS-like selectors
140 See [`contributing.md` in `syntax-tree/.github`][contributing] for ways to get
142 See [`support.md`][support] for ways to get help.
144 This project has a [Code of Conduct][coc].
145 By interacting with this repository, organisation, or community you agree to
150 [MIT][license] © [Titus Wormer][author]
154 [build-badge]: https://img.shields.io/travis/syntax-tree/unist-util-visit-parents.svg
156 [build]: https://travis-ci.org/syntax-tree/unist-util-visit-parents
158 [coverage-badge]: https://img.shields.io/codecov/c/github/syntax-tree/unist-util-visit-parents.svg
160 [coverage]: https://codecov.io/github/syntax-tree/unist-util-visit-parents
162 [downloads-badge]: https://img.shields.io/npm/dm/unist-util-visit-parents.svg
164 [downloads]: https://www.npmjs.com/package/unist-util-visit-parents
166 [size-badge]: https://img.shields.io/bundlephobia/minzip/unist-util-visit-parents.svg
168 [size]: https://bundlephobia.com/result?p=unist-util-visit-parents
170 [sponsors-badge]: https://opencollective.com/unified/sponsors/badge.svg
172 [backers-badge]: https://opencollective.com/unified/backers/badge.svg
174 [collective]: https://opencollective.com/unified
176 [chat-badge]: https://img.shields.io/badge/join%20the%20community-on%20spectrum-7b16ff.svg
178 [chat]: https://spectrum.chat/unified/syntax-tree
180 [npm]: https://docs.npmjs.com/cli/install
184 [author]: https://wooorm.com
186 [unist]: https://github.com/syntax-tree/unist
188 [node]: https://github.com/syntax-tree/unist#node
190 [visitor]: #next--visitornode-ancestors
192 [contributing]: https://github.com/syntax-tree/.github/blob/master/contributing.md
194 [support]: https://github.com/syntax-tree/.github/blob/master/support.md
196 [coc]: https://github.com/syntax-tree/.github/blob/master/code-of-conduct.md
198 [is]: https://github.com/syntax-tree/unist-util-is
200 [preorder]: https://www.geeksforgeeks.org/tree-traversals-inorder-preorder-and-postorder/
202 [descendant]: https://github.com/syntax-tree/unist#descendant
204 [head]: https://github.com/syntax-tree/unist#head
206 [tail]: https://github.com/syntax-tree/unist#tail
208 [parent]: https://github.com/syntax-tree/unist#parent-1
210 [sibling]: https://github.com/syntax-tree/unist#sibling
212 [index]: https://github.com/syntax-tree/unist#index
214 [ancestor]: https://github.com/syntax-tree/unist#ancestor
216 [tree]: https://github.com/syntax-tree/unist#tree
218 [type]: https://github.com/syntax-tree/unist#type