All Projects → syntax-tree → unist-util-visit-parents

syntax-tree / unist-util-visit-parents

Licence: MIT License
utility to recursively walk over unist nodes, with ancestral information

Programming Languages

javascript
184084 projects - #8 most used programming language
typescript
32286 projects

Projects that are alternatives of or similar to unist-util-visit-parents

unist-builder
utility to create a new trees with a nice syntax
Stars: ✭ 52 (+108%)
Mutual labels:  syntax-tree, util, unist, unist-util
unist-util-map
utility to create a new tree by mapping all nodes
Stars: ✭ 30 (+20%)
Mutual labels:  syntax-tree, util, unist, unist-util
unist-util-inspect
utility to inspect nodes
Stars: ✭ 16 (-36%)
Mutual labels:  syntax-tree, util, unist, unist-util
hast-util-sanitize
utility to sanitize hast nodes
Stars: ✭ 34 (+36%)
Mutual labels:  syntax-tree, util, unist
hast-util-from-dom
utility to transform a DOM tree to hast
Stars: ✭ 20 (-20%)
Mutual labels:  syntax-tree, util, unist
unist-util-select
utility to select unist nodes with CSS-like selectors
Stars: ✭ 41 (+64%)
Mutual labels:  util, unist, unist-util
hast-util-to-html
utility to serialize hast to HTML
Stars: ✭ 47 (+88%)
Mutual labels:  syntax-tree, util, unist
nlcst-to-string
utility to transform an nlcst tree to a string
Stars: ✭ 16 (-36%)
Mutual labels:  syntax-tree, util, unist
mdast-util-to-string
utility to get the plain text content of an mdast node
Stars: ✭ 27 (+8%)
Mutual labels:  syntax-tree, util, unist
mdast-util-to-hast
utility to transform mdast to hast
Stars: ✭ 53 (+112%)
Mutual labels:  syntax-tree, unist
Unist Util Visit
utility to visit nodes
Stars: ✭ 101 (+304%)
Mutual labels:  syntax-tree, util
Unified
☔️ interface for parsing, inspecting, transforming, and serializing content through syntax trees
Stars: ✭ 3,036 (+12044%)
Mutual labels:  syntax-tree, unist
sast
Parse CSS, Sass, SCSS, and Less into a unist syntax tree
Stars: ✭ 51 (+104%)
Mutual labels:  syntax-tree, unist
hast-util-to-mdast
utility to transform hast (HTML) to mdast (markdown)
Stars: ✭ 26 (+4%)
Mutual labels:  unist, unist-util
MarkdownSyntax
☄️ A Type-safe Markdown parser in Swift.
Stars: ✭ 65 (+160%)
Mutual labels:  syntax-tree, unist
ntast
Notion Abstract Syntax Tree specification.
Stars: ✭ 101 (+304%)
Mutual labels:  syntax-tree, unist
xast
Extensible Abstract Syntax Tree
Stars: ✭ 32 (+28%)
Mutual labels:  syntax-tree, unist
astutils
Bare essentials for building abstract syntax trees, and skeleton classes for PLY lexers and parsers.
Stars: ✭ 13 (-48%)
Mutual labels:  syntax-tree
Concrete-Syntax-Tree
Concrete Syntax Trees represent s-expressions with source information
Stars: ✭ 48 (+92%)
Mutual labels:  syntax-tree
hast-util-select
utility to add `querySelector`, `querySelectorAll`, and `matches` support for hast
Stars: ✭ 20 (-20%)
Mutual labels:  util

unist-util-visit-parents

Build Coverage Downloads Size Sponsors Backers Chat

unist utility to visit nodes, with ancestral information.

Install

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

npm:

npm install unist-util-visit-parents

Use

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)
})

Yields:

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

API

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.

Parameters
  • tree (Node) — Tree to traverse
  • test (Test, optional) — is-compatible test (such as a type)
  • visitor (Function) — Function invoked when a node is found that passes test
  • reverse (boolean, default: false) — The tree is traversed in preorder (NLR), visiting the node itself, then its head, etc. When reverse is passed, the tree is traversed in reverse preorder (NRL): the node itself is visited, then its tail, etc.

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.

Parameters
  • node (Node) — Found node
  • ancestors (Array.<Node>) — Ancestors of node
Returns

The return value can have the following forms:

  • index (number) — Treated as a tuple of [CONTINUE, index]
  • action (*) — Treated as a tuple of [action]
  • tuple (Array.<*>) — List with one or two values, the first an action, the second and index. Note that passing a tuple only makes sense if the action is SKIP. If the action is EXIT, that action can be returned. If the action is CONTINUE, index can be returned.
action

An action can have the following values:

  • EXIT (false) — Stop traversing immediately
  • CONTINUE (true) — Continue traversing as normal (same behaviour as not returning anything)
  • SKIP ('skip') — Do not traverse this node’s children; continue with the specified index
index

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

Related

Contribute

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.

License

MIT © Titus Wormer

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].