mirror of https://github.com/lukechilds/node.git
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
Rich Trott
aaee43473b
|
8 years ago | |
---|---|---|
.. | ||
LICENSE | 8 years ago | |
index.js | 8 years ago | |
package.json | 8 years ago | |
readme.md | 8 years ago |
readme.md
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(node[, type], visitor[, reverse])
Visit nodes. Optionally by node type. Optionally in reverse.
Parameters
node
(Node
) — Node to searchtype
(string
, optional) — Node typevisitor
(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
stop? = visitor(node, index, parent)
Invoked when a node (when type
is given, matching type
) is found.
Parameters
node
(Node
) — Found nodeindex
(number?
) — Position ofnode
inparent
parent
(Node?
) — Parent ofnode
Returns
boolean?
- When false
, visiting is immediately stopped.
Related
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