What is unist-util-visit?
The unist-util-visit package is a utility for visiting nodes in a unist syntax tree. It allows for traversal of the tree and application of functions to nodes that match specified conditions. This can be useful for manipulating or inspecting the tree in various ways, such as filtering nodes, applying transformations, or extracting information.
What are unist-util-visit's main functionalities?
Visiting nodes
This feature allows you to visit all nodes in a unist syntax tree that match a specified type. The provided function is called for each matching node. This can be useful for performing operations on specific types of nodes.
visit(tree, 'type', node => { console.log(node) })
Visiting nodes with a test function
This feature allows you to visit all nodes that pass a test implemented by a function. The test function is called with each node, and if it returns true, the node is considered a match. This allows for more complex matching conditions beyond just type.
visit(tree, node => node.value === 'specific value', node => { console.log(node) })
Exiting early
This feature allows you to exit the traversal early by returning `visit.EXIT` from the visitor function. This can be useful for optimizing performance by avoiding unnecessary traversal once a certain condition is met.
visit(tree, 'type', (node, index, parent) => { if (node.value === 'stop') return visit.EXIT })
Other packages similar to unist-util-visit
unist-util-select
This package provides a way to select nodes in a unist syntax tree using CSS-like selectors. It's similar to unist-util-visit in that it allows for node selection, but it uses a different approach based on selectors rather than explicit traversal and testing.
unist-util-filter
unist-util-filter offers functionality to create a new tree by filtering nodes in a unist syntax tree based on a given condition. It's similar to unist-util-visit in its ability to apply conditions to nodes, but instead of visiting nodes, it filters them to produce a new tree.
unist-util-map
This package allows for the creation of a new unist syntax tree by applying a function to every node in an input tree. It's related to unist-util-visit in that it involves applying functions to nodes, but unist-util-map applies the function to all nodes and constructs a new tree based on the results.
unist-util-visit
unist node visitor. Useful when working with remark,
retext, or rehype.
Installation
npm:
npm install unist-util-visit
Usage
var remark = require('remark');
var visit = require('unist-util-visit');
var tree = remark().parse('Some _emphasis_, **importance**, and `code`.');
visit(tree, 'text', visitor);
function visitor(node) {
console.log(node);
}
Yields:
{type: 'text', value: 'Some '}
{type: 'text', value: 'emphasis'}
{type: 'text', value: ', '}
{type: 'text', value: 'importance'}
{type: 'text', value: ', and '}
{type: 'text', value: '.'}
API
visit(tree[, test], visitor[, reverse])
Visit nodes (inclusive descendants of tree
). Optionally
filtering nodes. Optionally in reverse.
Parameters
tree
(Node
)
— Tree to traversetest
(Test
, optional)
— is
-compatible test (such as a node type)visitor
(Function)
— Function invoked when a node is found that passes test
reverse
(boolean
, default: false
)
— When falsey, checking starts at the first child and continues
through to later children. When truthy, this is reversed.
This does not mean checking starts at the deepest node and
continues on to the highest node
next? = visitor(node, index, parent)
Invoked when a node (matching test
if given) is found.
You can transform visited nodes. You can transform parent
, but if adding or
removing children before index
, you should return a new index
(number
) from visitor
to specify the next sibling to visit. Replacing
node
itself still causes its descendants to be visited. Adding or removing
nodes after index
is handled as expected without needing to return a new
index
. Removing the children
property on parent
still results in them
being traversed.
Parameters
node
(Node
) — Found nodeindex
(number?
) — Position of node
in parent
parent
(Node?
) — Parent of node
Returns
visit.EXIT
(false
)
— Stop traversing immediatelyvisit.CONTINUE
(true
)
— Continue traversing as normal (same behaviour as not returning anything)visit.SKIP
('skip'
)
— Do not enter this node (traversing into its children), but do continue
with the next siblingindex
(number
)
— Move to the sibling at position index
next (after node
itself is
traversed). Useful if you’re mutating the tree (such as removing the node
you’re currently on, or any of its preceding siblings). Results less than
0
or greater than or equal to children.length
stop iteration of the
parent
Related
Contribute
See contribute.md
in syntax-tree/unist
for ways to get
started.
This organisation has a Code of Conduct. By interacting with this
repository, organisation, or community you agree to abide by its terms.
License
MIT © Titus Wormer