Unist node visitor. Useful when working with remark, retext, or rehype.
npm:
npm install unist-util-visit
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: '.' }
Visit nodes. Optionally filtering nodes. Optionally in reverse.
node
(Node
) — Node to searchtest
(Test
, optional) — Node type or otheris
-compatible testvisitor
(Function) — Visitor invoked when a node is foundreverse
(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
Invoked when a node (matching test
, if given) is found.
node
(Node
) — Found nodeindex
(number?
) — Position ofnode
inparent
parent
(Node?
) — Parent ofnode
boolean?
- When false
, visiting is immediately stopped.
unist-util-visit-parents
— Likevisit
, but with a stack of parentsunist-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-remove
— Remove nodes from a tree that pass a testunist-util-select
— Select nodes with CSS-like selectors