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');
remark().use(plugin).process('Some _emphasis_, **importance**, and `code`.');
function plugin() {
return transformer;
function transformer(tree) {
visit(tree, 'strong', visitor);
}
function visitor(node, parents) {
console.log(parents);
}
}
Yields:
[ { type: 'root',
children: [ [Object] ] },
{ type: 'paragraph',
children:
[ [Object],
[Object],
[Object],
[Object],
[Object],
[Object],
[Object] ] } ]
visit(node[, type], visitor)
Visit nodes, with ancestral information. Optionally by node type.
node
(Node
) — Node to search;type
(string
, optional) — Node type;visitor
(Function) — Visitor invoked when a node is found.stop? = visitor(node, parents)
Invoked when a node (when type
is given, matching type
) is found.
node
(Node
) — Found node;parents
(Array.<Node>
) — List of parents.boolean?
- When false
, visiting is immediately stopped.
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.