unist utility to visit nodes
npm install unist-util-visit[![Build][badge-build-image]][badge-build-url]
[![Coverage][badge-coverage-image]][badge-coverage-url]
[![Downloads][badge-downloads-image]][badge-downloads-url]
[![Size][badge-size-image]][badge-size-url]
[unist][github-unist] utility to walk the tree.
* What is this?
* When should I use this?
* Install
* Use
* API
* [visit(tree[, test], visitor[, reverse])](#visittree-test-visitor-reverse)
* CONTINUE
* EXIT
* SKIP
* Action
* ActionTuple
* BuildVisitor
* Index
* Test
* Visitor
* VisitorResult
* 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.
You can use [unist-util-visit-parents][github-vp] if you care about the
entire stack of parents.
This package is [ESM only][github-gist-esm].
In Node.js (version 16+),
install with [npm][npmjs-install]:
``sh`
npm install unist-util-visit
In Deno with [esm.sh][esmsh]:
`js`
import {CONTINUE, EXIT, SKIP, visit} from 'https://esm.sh/unist-util-visit@5'
In browsers with [esm.sh][esmsh]:
`html`
`js
import {fromMarkdown} from 'mdast-util-from-markdown'
import {visit} from 'unist-util-visit'
const tree = fromMarkdown('Some emphasis, strong, and code.')
visit(tree, 'text', function (node, index, parent) {
console.log([node.value, parent ? parent.type : index])
})
`
Yields:
`js`
[ 'Some ', 'paragraph' ]
[ 'emphasis', 'emphasis' ]
[ ', ', 'paragraph' ]
[ 'strong', 'strong' ]
[ ', and ', 'paragraph' ]
[ '.', 'paragraph' ]
This package exports the identifiers
[CONTINUE][api-continue],EXIT
[][api-exit],SKIP
[][api-skip], andvisit
[][api-visit].ActionTuple
It exports the [TypeScript][] types
[][api-action-tuple],Action
[][api-action],BuildVisitor
[][api-build-visitor],Index
[][api-index],Test
[][api-test],VisitorResult
[][api-visitor-result], andVisitor
[][api-visitor].
There is no default export.
This function works exactly the same as
[unist-util-visit-parents][github-vp],Visitor
but [][api-visitor] has a different signature.
Continue traversing as normal (true).
Stop traversing immediately (false).
Do not traverse this node’s children ('skip').
Union of the action types (TypeScript type).
See [Action in unist-util-visit-parents][github-vp-action].
List with an action and an index (TypeScript type).
See [ActionTuple in unist-util-visit-parents][github-vp-action-tuple].
Build a typed Visitor function from a tree and a test (TypeScript type).BuildVisitor
See [ in unist-util-visit-parents][github-vp-build-visitor].
Move to the sibling at index next (TypeScript type).Index
See [ in unist-util-visit-parents][github-vp-index].
[unist-util-is][github-unist-util-is] compatible test
(TypeScript type).
Handle a node (matching test, if given) (TypeScript type).
Visitors are free to transform node.parent
They can also transform .
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 parent still results in them being
traversed.
###### Parameters
* node ([Node][github-unist-node])index
— found node
* (number or undefined)node
— index of in parentparent
* ([Node][github-unist-node] or undefined)node
— parent 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).
See [VisitorResult inunist-util-visit-parents][github-vp-visitor-result].
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@^5,
compatible with Node.js 16.
* [unist-util-visit-parents][github-vp]unist-util-filter
— walk the tree with a stack of parents
* unist-util-map
— create a new tree with all nodes that pass a test
* unist-util-flatmap
— create a new tree with all nodes mapped by a given function
* unist-util-remove
— create a new tree by mapping (to an array) with the given function
* unist-util-select
— remove nodes from a tree that pass a test
*
— select nodes with CSS-like selectors
See [contributing.md][health-contributing] in [syntax-tree/.github][health]support.md`][health-support] for ways to get help.
for ways to get started.
See [
This project has a [code of conduct][health-coc].
By interacting with this repository,
organization,
or community you agree to abide by its terms.
[MIT][file-license] © [Titus Wormer][wooorm]
[api-action]: #action
[api-action-tuple]: #actiontuple
[api-build-visitor]: #buildvisitor
[api-continue]: #continue
[api-exit]: #exit
[api-index]: #index
[api-skip]: #skip
[api-test]: #test
[api-visit]: #visittree-test-visitor-reverse
[api-visitor]: #visitor
[api-visitor-result]: #visitorresult
[badge-build-image]: https://github.com/syntax-tree/unist-util-visit/workflows/main/badge.svg
[badge-build-url]: https://github.com/syntax-tree/unist-util-visit/actions
[badge-coverage-image]: https://img.shields.io/codecov/c/github/syntax-tree/unist-util-visit.svg
[badge-coverage-url]: https://codecov.io/github/syntax-tree/unist-util-visit
[badge-downloads-image]: https://img.shields.io/npm/dm/unist-util-visit.svg
[badge-downloads-url]: https://www.npmjs.com/package/unist-util-visit
[badge-size-image]: https://img.shields.io/bundlejs/size/unist-util-visit
[badge-size-url]: https://bundlejs.com/?q=unist-util-visit
[esmsh]: https://esm.sh
[file-license]: license
[github-gist-esm]: https://gist.github.com/sindresorhus/a39789f98801d908bbc7ff3ecc99d99c
[github-unist]: https://github.com/syntax-tree/unist
[github-unist-node]: https://github.com/syntax-tree/unist#nodes
[github-unist-util-is]: https://github.com/syntax-tree/unist-util-is
[github-vp]: https://github.com/syntax-tree/unist-util-visit-parents
[github-vp-action]: https://github.com/syntax-tree/unist-util-visit-parents#action
[github-vp-action-tuple]: https://github.com/syntax-tree/unist-util-visit-parents#actiontuple
[github-vp-build-visitor]: https://github.com/syntax-tree/unist-util-visit-parents#buildvisitor
[github-vp-index]: https://github.com/syntax-tree/unist-util-visit-parents#index
[github-vp-visitor-result]: https://github.com/syntax-tree/unist-util-visit-parents#visitorresult
[health]: https://github.com/syntax-tree/.github
[health-coc]: https://github.com/syntax-tree/.github/blob/main/code-of-conduct.md
[health-contributing]: https://github.com/syntax-tree/.github/blob/main/contributing.md
[health-support]: https://github.com/syntax-tree/.github/blob/main/support.md
[npmjs-install]: https://docs.npmjs.com/cli/install
[typescript]: https://www.typescriptlang.org
[wooorm]: https://wooorm.com