Product
Introducing License Enforcement in Socket
Ensure open-source compliance with Socket’s License Enforcement Beta. Set up your License Policy and secure your software!
unist-util-visit-parents
Advanced tools
The unist-util-visit-parents package is a utility for traversing Unist syntax trees. It allows for visiting nodes, optionally filtered by type, and provides access to parent nodes during traversal. This can be particularly useful for manipulating or inspecting the structure of documents parsed into Unist syntax trees, such as Markdown or HTML parsed by remark or rehype processors.
Visiting nodes with access to their parents
This feature allows you to visit all nodes of a specified type ('paragraph' in this example) in a Unist syntax tree, and for each node, you have access to an array of its ancestor nodes. This is useful for context-aware processing or transformations.
visit(tree, 'paragraph', (node, ancestors) => {
console.log(node);
console.log(ancestors);
});
Visiting all nodes without filtering
This feature enables visiting all nodes in the syntax tree without filtering by type. Each visited node is accompanied by its ancestors, allowing for comprehensive traversal and manipulation of the tree.
visit(tree, (node, ancestors) => {
console.log(node);
console.log(ancestors);
});
Similar to unist-util-visit-parents, unist-util-visit allows for traversing Unist trees and applying a function to each visited node. The key difference is that unist-util-visit does not provide access to the ancestors of each node, focusing instead on simpler visitation needs.
This package provides a way to select nodes in a Unist tree using CSS-like selectors. While it serves a different primary purpose from unist-util-visit-parents, it offers an alternative approach to node selection and manipulation in Unist trees.
Unist node visitor, with ancestral information.
npm:
npm install unist-util-visit-parents
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] ] } ]
visit(tree[, test], visitor[, reverse])
Visit nodes (inclusive descendants of tree
), with
ancestral information. Optionally filtering nodes. Optionally in reverse.
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
) — The tree is walked in preorder
(NLR), visiting the node itself, then its head, etc.
When reverse
is passed, the tree is stilled walked in preorder, but now
in NRL (the node itself, 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 parent still results in them being
traversed.
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 an action
,
the second and index
.
Note that passing a tuple only makes sense if the action
is SKIP
.
If the action
is EXIT
, that action can be returned.
If the action
is CONTINUE
, 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 indexindex
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
unist-util-visit
— Like visit-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 provided function and
then flatteningunist-util-remove
— Remove nodes from a tree that pass a testunist-util-select
— Select nodes with CSS-like selectorsSee contributing.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.
FAQs
unist utility to recursively walk over nodes, with ancestral information
The npm package unist-util-visit-parents receives a total of 12,255,859 weekly downloads. As such, unist-util-visit-parents popularity was classified as popular.
We found that unist-util-visit-parents demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 2 open source maintainers collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Product
Ensure open-source compliance with Socket’s License Enforcement Beta. Set up your License Policy and secure your software!
Product
We're launching a new set of license analysis and compliance features for analyzing, managing, and complying with licenses across a range of supported languages and ecosystems.
Product
We're excited to introduce Socket Optimize, a powerful CLI command to secure open source dependencies with tested, optimized package overrides.