NPM unist-util-visit Package


unist-util-visit

Visit nodes in a unist tree.

Usage

npm install unist-util-visit
const visit = require('unist-util-visit');
const unified = require('unified');
const markdown = require('remark-parse');
const visitAt = require('unist-util-visit-parents');

const processor = unified().use(markdown).use(visit);

const tree = processor.parse('Hello, world!');

visit(tree, 'text', visitor);

function visitor(node, index, parent) {
  console.log(`=> ${node.value}`);
}

API

visit(tree, type, visitor)

Visit nodes of a given type in a unist tree with visitor.

The visitor function takes three arguments:

  • node: The unist node.
  • index: The index of the node.
  • parent: The parent node, if it exists.

The visitor function must return nothing.

visitAt(tree, type, visitor)

Like visit, but calls the visitor at each given type with its parent.

visitParents(tree, type, visitor)

Visit parents of nodes of a given type in a unist tree with visitor.

The visitor function takes three arguments:

  • node: The unist node.
  • index: The index of the node.
  • parent: The parent node, if it exists.

The visitor function must return nothing.

visitChildren(tree, type, visitor)

Visit children of nodes of a given type in a unist tree with visitor.

The visitor function takes three arguments:

  • node: The unist node.
  • index: The index of the node.
  • parent: The parent node, if it exists.

The visitor function must return nothing.

visitKeys(tree, type, visitor)

Visit keys of nodes of a given type in a unist tree with visitor.

The visitor function takes three arguments:

  • node: The unist node.
  • index: The index of the node.
  • parent: The parent node, if it exists.

The visitor function must return nothing.

visitValues(tree, type, visitor)

Visit values of nodes of a given type in a unist tree with visitor.

The visitor function takes three arguments:

  • node: The unist node.
  • index: The index of the node.
  • parent: The parent node, if it exists.

The visitor function must return nothing.

visitLeaves(tree, type, visitor)

Visit leaf nodes of a given type in a unist tree with visitor.

The visitor function takes three arguments:

  • node: The unist node.
  • index: The index of the node.
  • parent: The parent node, if it exists.

The visitor function must return nothing.

Configuration

None.

Best practices

Use visitParents, visitChildren, visitKeys, visitValues, and visitLeaves instead of visit when possible.

Authors