Project: syntax-tree/unist-util-visit-parents

Package: unist-util-visit-parents@5.1.0

  1. Dependents: 147
  2. unist utility to recursively walk over nodes, with ancestral information
  1. util 143
  2. utility 139
  3. unist 127
  4. tree 40
  5. ast 32
  6. unist-util 25
  7. check 10
  8. walk 8
  9. visit 6
  10. parent 2
  11. parents 2


Build Coverage Downloads Size Sponsors Backers Chat

unist utility to visit nodes, with ancestral information.


This package is ESM only: Node 12+ is needed to use it and it must be imported instead of required.


npm install unist-util-visit-parents


import {visitParents} from 'unist-util-visit-parents'
import {fromMarkdown} from 'mdast-util-from-markdown'

const tree = fromMarkdown('Some _emphasis_, **importance**, and `code`.')

visitParents(tree, 'strong', (node, ancestors) => {
  console.log(node.type, ancestors)


    type: 'root',
    children: [[Object]],
    position: {start: [Object], end: [Object]}
    type: 'paragraph',
    children: [
    position: {start: [Object], end: [Object]}


This package exports the following identifiers: visitParents, SKIP, CONTINUE, and EXIT. There is no default export.

visitParents(tree[, test], visitor[, reverse])

Visit nodes (inclusive descendants of tree), with ancestral information. Optionally filtering nodes. Optionally in reverse.

This algorithm performs depth-first tree traversal in preorder (NLR), or if reverse is given, in reverse preorder (NRL).

Walking the tree is an intensive task. Make use of the return values of the visitor when possible. Instead of walking a tree multiple times with different tests, walk it once without a test, and use unist-util-is to check if a node matches a test, and then perform different operations.


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 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 an ancestor still results in them being traversed.


The return value can have the following forms:


An action can have the following values:


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


See contributing.md in syntax-tree/.github for ways to get started. See support.md for ways to get help.

This project has a code of conduct. By interacting with this repository, organization, or community you agree to abide by its terms.


MIT © Titus Wormer