7.4 KiB
unist-util-visit-parents
unist utility to visit nodes, with ancestral information.
Install
npm:
npm install unist-util-visit-parents
Use
var remark = require('remark')
var visit = require('unist-util-visit-parents')
var tree = remark.parse('Some _emphasis_, **importance**, and `code`.')
visit(tree, 'strong', visitor)
function visitor(node, ancestors) {
console.log(ancestors)
}
Yields:
type: 'root', children: [ [Object] ] },
[ { type: 'paragraph',
{ children:
Object],
[ [Object],
[Object],
[Object],
[Object],
[Object],
[Object] ] } ] [
API
visit(tree[, test], visitor[, reverse])
Visit nodes (inclusive
descendants of tree
),
with ancestral information. Optionally filtering nodes. Optionally in
reverse.
This algorithm performs depth-first
tree
traversal in preorder
(NLR), or if reverse
is given, in
reverse preorder (NRL).
Walking the tree is an intensive task. Make use of the return values
of the visitor when possible. Instead of walking a tree multiple times
with different test
s, walk it once without a test, and use
unist-util-is
to check if a node matches a test, and then perform different
operations.
Parameters
tree
(Node
) — Tree to traversetest
(Test
, optional) —is
-compatible test (such as a type)visitor
(Function) — Function invoked when a node is found that passestest
reverse
(boolean
, default:false
) — The tree is traversed in preorder (NLR), visiting the node itself, then its head, etc. Whenreverse
is passed, the tree is traversed in reverse preorder (NRL): the node itself is visited, then its tail, etc.
next? = visitor(node, ancestors)
Invoked when a node (matching test
, if given) is
found.
Visitors are free to transform node
. They can also
transform the parent of node
(the last of ancestors
). Replacing node
itself, if visit.SKIP
is not returned, still causes its descendants
to be visited. If adding or removing previous siblings (or
next siblings, in case of reverse
) of node
,
visitor
should return a new index
(number
) to specify the sibling to traverse after
node
is traversed. Adding or removing next siblings of
node
(or previous siblings, in case of reverse) is handled
as expected without needing to return a new index
. Removing
the children
property of an ancestor still results in them
being traversed.
Parameters
Returns
The return value can have the following forms:
index
(number
) — Treated as a tuple of[CONTINUE, index]
action
(*
) — Treated as a tuple of[action]
tuple
(Array.<*>
) — List with one or two values, the first anaction
, the second andindex
. Note that passing a tuple only makes sense if theaction
isSKIP
. If theaction
isEXIT
, that action can be returned. If theaction
isCONTINUE
,index
can be returned.
action
An action can have the following values:
visit.EXIT
(false
) — Stop traversing immediatelyvisit.CONTINUE
(true
) — Continue traversing as normal (same behaviour as not returning anything)visit.SKIP
('skip'
) — Do not traverse this node’s children; continue with the specified index
index
index
(number
) — Move to the sibling at index
next
(after node
itself is completely traversed). Useful if
mutating the tree, such as removing the node the visitor is currently
on, or any of its previous siblings (or next siblings, in case of
reverse
) Results less than 0
or greater than
or equal to children.length
stop traversing the parent
Related
unist-util-visit
— Likevisit-parents
, but with one parentunist-util-filter
— Create a new tree with all nodes that pass a testunist-util-map
— Create a new tree with all nodes mapped by a given functionunist-util-flatmap
— Create a new tree by mapping (to an array) with the given functionunist-util-remove
— Remove nodes from a tree that pass a testunist-util-select
— Select nodes with CSS-like selectors
Contribute
See contributing.md
in syntax-tree/.github
for ways to get started. See support.md
for ways to get help.
This project has a code of conduct. By interacting with this repository, organization, or community you agree to abide by its terms.