unist utility to recursively walk over nodes, with ancestral information
npm install unist-util-visit-parents[![Build][build-badge]][build]
[![Coverage][coverage-badge]][coverage]
[![Downloads][downloads-badge]][downloads]
[![Size][size-badge]][size]
[![Sponsors][sponsors-badge]][collective]
[![Backers][backers-badge]][collective]
[![Chat][chat-badge]][chat]
[unist][] utility to walk the tree with a stack of parents.
* What is this?
* When should I use this?
* Install
* Use
* API
* [visitParents(tree[, test], visitor[, reverse])](#visitparentstree-test-visitor-reverse)
* CONTINUE
* EXIT
* SKIP
* Action
* ActionTuple
* BuildVisitor
* Index
* Test
* Visitor
* VisitorResult
* Types
* Compatibility
* Related
* Contribute
* License
This is a very important utility for working with unist as it lets you walk the
tree.
You can use this utility when you want to walk the tree and want to know about
every parent of each node.
You can use [unist-util-visit][unist-util-visit] if you don’t care about the
entire stack of parents.
This package is [ESM only][esm].
In Node.js (version 16+), install with [npm][]:
``sh`
npm install unist-util-visit-parents
In Deno with [esm.sh][esmsh]:
`js`
import {visitParents} from 'https://esm.sh/unist-util-visit-parents@6'
In browsers with [esm.sh][esmsh]:
`html`
`js
import {visitParents} from 'unist-util-visit-parents'
import {fromMarkdown} from 'mdast-util-from-markdown'
const tree = fromMarkdown('Some emphasis, strong, and code.')
visitParents(tree, 'strong', function (node, ancestors) {
console.log(node.type, ancestors.map(ancestor => ancestor.type))
})
`
Yields:
`js`
strong ['root', 'paragraph']
This package exports the identifiers [CONTINUE][api-continue],EXIT
[][api-exit], [SKIP][api-skip], and [visitParents][api-visitparents].
There is no default export.
Visit nodes, with ancestral information.
This algorithm performs [depth-first][] [tree traversal][tree-traversal]
in [preorder][] (NLR) or if reverse is given, in reverse preorder
(NRL).
You can choose for which nodes visitor is called by passing a test.visitor
For complex tests, you should test yourself in , as it will be
faster and will have improved type information.
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, walk it once, use
[unist-util-is][unist-util-is] to check if a node matches, and then perform
different operations.
You can change the tree.
See [Visitor][api-visitor] for more info.
###### Parameters
* tree ([Node][node])test
— tree to traverse
* ([Test][api-test], optional)unist-util-is
— [][unist-util-is]-compatible testvisitor
* ([Visitor][api-visitor])reverse
— handle each node
* (boolean, default: false)
— traverse in reverse preorder (NRL) instead of the default preorder (NLR)
###### Returns
Nothing (undefined).
Continue traversing as normal (true).
Stop traversing immediately (false).
Do not traverse this node’s children ('skip').
Union of the action types (TypeScript type).
###### Type
`ts`
type Action = typeof CONTINUE | typeof EXIT | typeof SKIP
List with one or two values, the first an action, the second an index
(TypeScript type).
###### Type
`ts`
type ActionTuple = [
(Action | null | undefined | void)?,
(Index | null | undefined)?
]
Build a typed Visitor function from a tree and a test (TypeScript type).
It will infer which values are passed as node and which as parents.
###### Type parameters
* Tree ([Node][node], default: Node)Check
— tree type
* ([Test][api-test], default: Test)
— test type
###### Returns
[Visitor][api-visitor].
Move to the sibling at index next (after node itself is completely
traversed) (TypeScript type).
Useful if mutating the tree, such as removing the node the visitor is currently
on, or any of its previous siblings.
Results less than 0 or greater than or equal to children.length stop
traversing the parent.
###### Type
`ts`
type Index = number
[unist-util-is][unist-util-is] compatible test (TypeScript type).
Handle a node (matching test, if given) (TypeScript type).
Visitors are free to transform node.ancestors
They can also transform the parent of node (the last of ).
Replacing node itself, if SKIP is not returned, still causes its
descendants to be walked (which is a bug).
When adding or removing previous siblings of node (or next siblings, inVisitor
case of reverse), the should return a new Index to specify thenode
sibling to traverse after is traversed.node
Adding or removing next siblings of (or previous siblings, in caseIndex
of reverse) is handled as expected without needing to return a new .
Removing the children property of an ancestor still results in them being
traversed.
###### Parameters
* node ([Node][node])parents
— found node
* ([Array][node])node
— ancestors of
###### Returns
What to do next.
An Index is treated as a tuple of [CONTINUE, Index].Action
An is treated as a tuple of [Action].
Passing a tuple back only makes sense if the Action is SKIP.Action
When the is EXIT, that action can be returned.Action
When the is CONTINUE, Index can be returned.
Any value that can be returned from a visitor (TypeScript type).
###### Type
`ts`
type VisitorResult =
| Action
| ActionTuple
| Index
| null
| undefined
| void
This package is fully typed with [TypeScript][].
It exports the additional types [Action][api-action],ActionTuple
[][api-actiontuple], [BuildVisitor][api-buildvisitor],Index
[][api-index], [Test][api-test], [Visitor][api-visitor], andVisitorResult
[][api-visitorresult].
Projects maintained by the unified collective are compatible with maintained
versions of Node.js.
When we cut a new major release, we drop support for unmaintained versions of
Node.
This means we try to keep the current release line,
unist-util-visit-parents@^6, compatible with Node.js 16.
* unist-util-visit
— walk the tree with one parent
* unist-util-filter
— create a new tree with all nodes that pass a test
* unist-util-map
— create a new tree with all nodes mapped by a given function
* unist-util-flatmap
— create a new tree by mapping (to an array) with the given function
* unist-util-remove
— remove nodes from a tree that pass a test
* unist-util-select
— select nodes with CSS-like selectors
See [contributing.md][contributing] in [syntax-tree/.github][health] forsupport.md`][support] for ways to get help.
ways to get started.
See [
This project has a [code of conduct][coc].
By interacting with this repository, organization, or community you agree to
abide by its terms.
[MIT][license] © [Titus Wormer][author]
[build-badge]: https://github.com/syntax-tree/unist-util-visit-parents/workflows/main/badge.svg
[build]: https://github.com/syntax-tree/unist-util-visit-parents/actions
[coverage-badge]: https://img.shields.io/codecov/c/github/syntax-tree/unist-util-visit-parents.svg
[coverage]: https://codecov.io/github/syntax-tree/unist-util-visit-parents
[downloads-badge]: https://img.shields.io/npm/dm/unist-util-visit-parents.svg
[downloads]: https://www.npmjs.com/package/unist-util-visit-parents
[size-badge]: https://img.shields.io/badge/dynamic/json?label=minzipped%20size&query=$.size.compressedSize&url=https://deno.bundlejs.com/?q=unist-util-visit-parents
[size]: https://bundlejs.com/?q=unist-util-visit-parents
[sponsors-badge]: https://opencollective.com/unified/sponsors/badge.svg
[backers-badge]: https://opencollective.com/unified/backers/badge.svg
[collective]: https://opencollective.com/unified
[chat-badge]: https://img.shields.io/badge/chat-discussions-success.svg
[chat]: https://github.com/syntax-tree/unist/discussions
[npm]: https://docs.npmjs.com/cli/install
[esm]: https://gist.github.com/sindresorhus/a39789f98801d908bbc7ff3ecc99d99c
[esmsh]: https://esm.sh
[typescript]: https://www.typescriptlang.org
[license]: license
[author]: https://wooorm.com
[health]: https://github.com/syntax-tree/.github
[contributing]: https://github.com/syntax-tree/.github/blob/HEAD/contributing.md
[support]: https://github.com/syntax-tree/.github/blob/HEAD/support.md
[coc]: https://github.com/syntax-tree/.github/blob/HEAD/code-of-conduct.md
[unist]: https://github.com/syntax-tree/unist
[node]: https://github.com/syntax-tree/unist#node
[depth-first]: https://github.com/syntax-tree/unist#depth-first-traversal
[tree-traversal]: https://github.com/syntax-tree/unist#tree-traversal
[preorder]: https://github.com/syntax-tree/unist#preorder
[unist-util-visit]: https://github.com/syntax-tree/unist-util-visit
[unist-util-is]: https://github.com/syntax-tree/unist-util-is
[api-visitparents]: #visitparentstree-test-visitor-reverse
[api-continue]: #continue
[api-exit]: #exit
[api-skip]: #skip
[api-action]: #action
[api-actiontuple]: #actiontuple
[api-buildvisitor]: #buildvisitor
[api-index]: #index
[api-test]: #test
[api-visitor]: #visitor
[api-visitorresult]: #visitorresult