the editable site (validation-lab)
deploy / build-and-publish (push) Successful in 1m37s

This commit is contained in:
2026-11-21 08:03:55 +01:00
commit 3ac81d5a52
13454 changed files with 2227005 additions and 0 deletions
+16
View File
@@ -0,0 +1,16 @@
export type {Test} from 'unist-util-is'
export type {
Action,
ActionTuple,
Index,
VisitorResult,
VisitorResultExit
} from 'unist-util-visit-parents'
export type {
BuildVisitor,
BuildVisitors,
Visitor,
VisitorExit,
Visitors
} from './lib/index.js'
export {CONTINUE, EXIT, SKIP, visit} from './lib/index.js'
+2
View File
@@ -0,0 +1,2 @@
// Note: types exported from `index.d.ts`
export {CONTINUE, EXIT, SKIP, visit} from './lib/index.js'
+219
View File
@@ -0,0 +1,219 @@
/**
* Visit nodes.
*
* This algorithm performs *depth-first* *tree traversal* in *preorder*
* (**NLR**) or if `reverse` is given,
* in *reverse preorder* (**NRL**).
* An `exit` visitor is called in *postorder* (**LRN**)
* or if `reverse` is given, in *reverse postorder* (**RLN**).
*
* You can choose which nodes are visited by passing a `test`.
* For complex tests, you should test yourself in a visitor, 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`
* to check if a node matches, and then perform different operations.
*
* You can change the tree.
* Changes inside a node made when entering it are walked,
* changes made when exiting it are not.
* See `Visitor` and `VisitorExit` for more info.
*
* @overload
* @param {Tree} tree
* Tree to traverse.
* @param {Check} check
* `unist-util-is`-compatible test.
* @param {BuildVisitor<Tree, Check> | BuildVisitors<Tree, Check>} visitors
* Handle each node when entering it,
* or `{enter, exit}` to also handle it when exiting it.
* @param {boolean | null | undefined} [reverse]
* Walk children from last to first;
* for `enter`, reverse preorder (NRL) instead of preorder (NLR);
* for `exit`, reverse postorder (RLN) instead of postorder (LRN).
* @returns {undefined}
* Nothing.
*
* @overload
* @param {Tree} tree
* Tree to traverse.
* @param {BuildVisitor<Tree> | BuildVisitors<Tree>} visitors
* Handle each node when entering it,
* or `{enter, exit}` to also handle it when exiting it.
* @param {boolean | null | undefined} [reverse]
* Walk children from last to first;
* for `enter`, reverse preorder (NRL) instead of preorder (NLR);
* for `exit`, reverse postorder (RLN) instead of postorder (LRN).
* @returns {undefined}
* Nothing.
*
* @param {UnistNode} tree
* Tree to traverse.
* @param {Visitor | Visitors | Test} testOrEnterOrVisitors
* `unist-util-is`-compatible test, `enter`, or `visitors`.
* @param {Visitor | Visitors | boolean | null | undefined} [enterOrVisitorsOrReverse]
* `enter`, `visitors`, or `reverse` after a test.
* @param {boolean | null | undefined} [maybeReverse]
* Walk children from last to first;
* for `enter`, reverse preorder (NRL) instead of preorder (NLR);
* for `exit`, reverse postorder (RLN) instead of postorder (LRN).
* @returns {undefined}
* Nothing.
*
* @template {UnistNode} Tree
* Node type.
* @template {Test} Check
* `unist-util-is`-compatible test.
*/
export function visit<Tree extends UnistNode, Check extends Test>(tree: Tree, check: Check, visitors: BuildVisitor<Tree, Check> | BuildVisitors<Tree, Check>, reverse?: boolean | null | undefined): undefined;
/**
* Visit nodes.
*
* This algorithm performs *depth-first* *tree traversal* in *preorder*
* (**NLR**) or if `reverse` is given,
* in *reverse preorder* (**NRL**).
* An `exit` visitor is called in *postorder* (**LRN**)
* or if `reverse` is given, in *reverse postorder* (**RLN**).
*
* You can choose which nodes are visited by passing a `test`.
* For complex tests, you should test yourself in a visitor, 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`
* to check if a node matches, and then perform different operations.
*
* You can change the tree.
* Changes inside a node made when entering it are walked,
* changes made when exiting it are not.
* See `Visitor` and `VisitorExit` for more info.
*
* @overload
* @param {Tree} tree
* Tree to traverse.
* @param {Check} check
* `unist-util-is`-compatible test.
* @param {BuildVisitor<Tree, Check> | BuildVisitors<Tree, Check>} visitors
* Handle each node when entering it,
* or `{enter, exit}` to also handle it when exiting it.
* @param {boolean | null | undefined} [reverse]
* Walk children from last to first;
* for `enter`, reverse preorder (NRL) instead of preorder (NLR);
* for `exit`, reverse postorder (RLN) instead of postorder (LRN).
* @returns {undefined}
* Nothing.
*
* @overload
* @param {Tree} tree
* Tree to traverse.
* @param {BuildVisitor<Tree> | BuildVisitors<Tree>} visitors
* Handle each node when entering it,
* or `{enter, exit}` to also handle it when exiting it.
* @param {boolean | null | undefined} [reverse]
* Walk children from last to first;
* for `enter`, reverse preorder (NRL) instead of preorder (NLR);
* for `exit`, reverse postorder (RLN) instead of postorder (LRN).
* @returns {undefined}
* Nothing.
*
* @param {UnistNode} tree
* Tree to traverse.
* @param {Visitor | Visitors | Test} testOrEnterOrVisitors
* `unist-util-is`-compatible test, `enter`, or `visitors`.
* @param {Visitor | Visitors | boolean | null | undefined} [enterOrVisitorsOrReverse]
* `enter`, `visitors`, or `reverse` after a test.
* @param {boolean | null | undefined} [maybeReverse]
* Walk children from last to first;
* for `enter`, reverse preorder (NRL) instead of preorder (NLR);
* for `exit`, reverse postorder (RLN) instead of postorder (LRN).
* @returns {undefined}
* Nothing.
*
* @template {UnistNode} Tree
* Node type.
* @template {Test} Check
* `unist-util-is`-compatible test.
*/
export function visit<Tree extends UnistNode, Check extends Test>(tree: Tree, visitors: BuildVisitor<Tree> | BuildVisitors<Tree>, reverse?: boolean | null | undefined): undefined;
/**
* Test from `unist-util-is`.
*
* Note: we have remove and add `undefined`, because otherwise when generating
* automatic `.d.ts` files, TS tries to flatten paths from a local perspective,
* which doesn’t work when publishing on npm.
*/
export type Test = Exclude<import("unist-util-is").Test, undefined> | undefined;
/**
* Collect nodes that can be parents of `Child`.
*/
export type InternalParent<Node extends UnistNode, Child extends UnistNode> = (Node extends UnistParent ? Node extends {
children: Array<infer Children>;
} ? Child extends Children ? Node : never : never : never);
/**
* Collect nodes in `Tree` that can be parents of `Child`.
*/
export type Parent<Tree extends UnistNode, Child extends UnistNode> = InternalParent<InclusiveDescendant<Tree>, Child>;
/**
* Handle a node (matching `test`, if given) before its children.
*
* Like `Visitor` in `unist-util-visit-parents`,
* but with `index` and `parent` instead of `parents`.
*/
export type Visitor<Visited extends UnistNode = UnistNode, Ancestor extends UnistParent = UnistParent> = (node: Visited, index: Visited extends UnistNode ? number | undefined : never, parent: Ancestor extends UnistParent ? Ancestor | undefined : never) => VisitorResult;
/**
* Handle a node (matching `test`, if given) after its children.
*
* Like `VisitorExit` in `unist-util-visit-parents`,
* but with `index` and `parent` instead of `parents`.
*/
export type VisitorExit<Visited extends UnistNode = UnistNode, Ancestor extends UnistParent = UnistParent> = (node: Visited, index: Visited extends UnistNode ? number | undefined : never, parent: Ancestor extends UnistParent ? Ancestor | undefined : never) => VisitorResultExit;
/**
* Handle nodes when entering and exiting them.
*
* Like `Visitors` in `unist-util-visit-parents`.
*/
export type Visitors<Visited extends UnistNode = UnistNode, Ancestor extends UnistParent = UnistParent> = {
/**
* Handle a node before its children (optional).
*/
enter?: Visitor<Visited, Ancestor> | null | undefined;
/**
* Handle a node after its children.
*/
exit: VisitorExit<Visited, Ancestor>;
};
/**
* Build a typed `Visitor` function from a node and all possible parents.
*
* It will infer which values are passed as `node` and which as `parent`.
*/
export type BuildVisitorFromMatch<Visited extends UnistNode, Ancestor extends UnistParent> = Visitor<Visited, Parent<Ancestor, Visited>>;
/**
* Build a typed `Visitor` function from a list of descendants and a test.
*
* It will infer which values are passed as `node` and which as `parent`.
*/
export type BuildVisitorFromDescendants<Descendant extends UnistNode, Check extends Test> = (BuildVisitorFromMatch<Matches<Descendant, Check>, Extract<Descendant, UnistParent>>);
/**
* Build a typed `Visitor` function from a tree and a test.
*
* It will infer which values are passed as `node` and which as `parent`.
*/
export type BuildVisitor<Tree extends UnistNode = UnistNode, Check extends Test = Test> = (BuildVisitorFromDescendants<InclusiveDescendant<Tree>, Check>);
/**
* Build typed `Visitors` from a tree and a test.
*
* It will infer which values are passed as `node` and which as `parent`.
*/
export type BuildVisitors<Tree extends UnistNode = UnistNode, Check extends Test = Test> = (Visitors<Matches<InclusiveDescendant<Tree>, Check>, Parent<Extract<InclusiveDescendant<Tree>, UnistParent>, Matches<InclusiveDescendant<Tree>, Check>>>);
import type { Node as UnistNode } from 'unist';
import type { Parent as UnistParent } from 'unist';
import type { InclusiveDescendant } from 'unist-util-visit-parents';
import type { VisitorResult } from 'unist-util-visit-parents';
import type { VisitorResultExit } from 'unist-util-visit-parents';
import type { Matches } from 'unist-util-visit-parents';
export { CONTINUE, EXIT, SKIP } from "unist-util-visit-parents";
//# sourceMappingURL=index.d.ts.map
+1
View File
@@ -0,0 +1 @@
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["index.js"],"names":[],"mappings":"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"}
+324
View File
@@ -0,0 +1,324 @@
/**
* @import {Node as UnistNode, Parent as UnistParent} from 'unist'
* @import {
* InclusiveDescendant,
* Matches,
* Visitors as ParentsVisitors,
* VisitorResult,
* VisitorResultExit
* } from 'unist-util-visit-parents'
*/
/**
* @typedef {Exclude<import('unist-util-is').Test, undefined> | undefined} Test
* Test from `unist-util-is`.
*
* Note: we have remove and add `undefined`, because otherwise when generating
* automatic `.d.ts` files, TS tries to flatten paths from a local perspective,
* which doesn’t work when publishing on npm.
*/
/**
* @typedef {(
* Node extends UnistParent
* ? Node extends {children: Array<infer Children>}
* ? Child extends Children ? Node : never
* : never
* : never
* )} InternalParent
* Collect nodes that can be parents of `Child`.
* @template {UnistNode} Node
* All node types in a tree.
* @template {UnistNode} Child
* Node to search for.
*/
/**
* @typedef {InternalParent<InclusiveDescendant<Tree>, Child>} Parent
* Collect nodes in `Tree` that can be parents of `Child`.
* @template {UnistNode} Tree
* All node types in a tree.
* @template {UnistNode} Child
* Node to search for.
*/
/**
* @callback Visitor
* Handle a node (matching `test`, if given) before its children.
*
* Like `Visitor` in `unist-util-visit-parents`,
* but with `index` and `parent` instead of `parents`.
* @param {Visited} node
* Found node.
* @param {Visited extends UnistNode ? number | undefined : never} index
* Index of `node` in `parent`,
* or `undefined` for the tree itself.
* @param {Ancestor extends UnistParent ? Ancestor | undefined : never} parent
* Parent of `node`,
* or `undefined` for the tree itself.
* @returns {VisitorResult}
* What to do next.
* @template {UnistNode} [Visited=UnistNode]
* Visited node type.
* @template {UnistParent} [Ancestor=UnistParent]
* Ancestor type.
*/
/**
* @callback VisitorExit
* Handle a node (matching `test`, if given) after its children.
*
* Like `VisitorExit` in `unist-util-visit-parents`,
* but with `index` and `parent` instead of `parents`.
* @param {Visited} node
* Found node.
* @param {Visited extends UnistNode ? number | undefined : never} index
* Index of `node` in `parent`,
* or `undefined` for the tree itself.
* @param {Ancestor extends UnistParent ? Ancestor | undefined : never} parent
* Parent of `node`,
* or `undefined` for the tree itself.
* @returns {VisitorResultExit}
* What to do next.
* @template {UnistNode} [Visited=UnistNode]
* Visited node type.
* @template {UnistParent} [Ancestor=UnistParent]
* Ancestor type.
*/
/**
* @template {UnistNode} [Visited=UnistNode]
* Visited node type.
* @template {UnistParent} [Ancestor=UnistParent]
* Ancestor type.
* @typedef Visitors
* Handle nodes when entering and exiting them.
*
* Like `Visitors` in `unist-util-visit-parents`.
* @property {Visitor<Visited, Ancestor> | null | undefined} [enter]
* Handle a node before its children (optional).
* @property {VisitorExit<Visited, Ancestor>} exit
* Handle a node after its children.
*/
/**
* @typedef {Visitor<Visited, Parent<Ancestor, Visited>>} BuildVisitorFromMatch
* Build a typed `Visitor` function from a node and all possible parents.
*
* It will infer which values are passed as `node` and which as `parent`.
* @template {UnistNode} Visited
* Node type.
* @template {UnistParent} Ancestor
* Parent type.
*/
/**
* @typedef {(
* BuildVisitorFromMatch<
* Matches<Descendant, Check>,
* Extract<Descendant, UnistParent>
* >
* )} BuildVisitorFromDescendants
* Build a typed `Visitor` function from a list of descendants and a test.
*
* It will infer which values are passed as `node` and which as `parent`.
* @template {UnistNode} Descendant
* Node type.
* @template {Test} Check
* Test type.
*/
/**
* @typedef {(
* BuildVisitorFromDescendants<
* InclusiveDescendant<Tree>,
* Check
* >
* )} BuildVisitor
* Build a typed `Visitor` function from a tree and a test.
*
* It will infer which values are passed as `node` and which as `parent`.
* @template {UnistNode} [Tree=UnistNode]
* Node type.
* @template {Test} [Check=Test]
* Test type.
*/
/**
* @typedef {(
* Visitors<
* Matches<InclusiveDescendant<Tree>, Check>,
* Parent<
* Extract<InclusiveDescendant<Tree>, UnistParent>,
* Matches<InclusiveDescendant<Tree>, Check>
* >
* >
* )} BuildVisitors
* Build typed `Visitors` from a tree and a test.
*
* It will infer which values are passed as `node` and which as `parent`.
* @template {UnistNode} [Tree=UnistNode]
* Node type.
* @template {Test} [Check=Test]
* Test type.
*/
import {visitParents} from 'unist-util-visit-parents'
export {CONTINUE, EXIT, SKIP} from 'unist-util-visit-parents'
/**
* Visit nodes.
*
* This algorithm performs *depth-first* *tree traversal* in *preorder*
* (**NLR**) or if `reverse` is given,
* in *reverse preorder* (**NRL**).
* An `exit` visitor is called in *postorder* (**LRN**)
* or if `reverse` is given, in *reverse postorder* (**RLN**).
*
* You can choose which nodes are visited by passing a `test`.
* For complex tests, you should test yourself in a visitor, 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`
* to check if a node matches, and then perform different operations.
*
* You can change the tree.
* Changes inside a node made when entering it are walked,
* changes made when exiting it are not.
* See `Visitor` and `VisitorExit` for more info.
*
* @overload
* @param {Tree} tree
* Tree to traverse.
* @param {Check} check
* `unist-util-is`-compatible test.
* @param {BuildVisitor<Tree, Check> | BuildVisitors<Tree, Check>} visitors
* Handle each node when entering it,
* or `{enter, exit}` to also handle it when exiting it.
* @param {boolean | null | undefined} [reverse]
* Walk children from last to first;
* for `enter`, reverse preorder (NRL) instead of preorder (NLR);
* for `exit`, reverse postorder (RLN) instead of postorder (LRN).
* @returns {undefined}
* Nothing.
*
* @overload
* @param {Tree} tree
* Tree to traverse.
* @param {BuildVisitor<Tree> | BuildVisitors<Tree>} visitors
* Handle each node when entering it,
* or `{enter, exit}` to also handle it when exiting it.
* @param {boolean | null | undefined} [reverse]
* Walk children from last to first;
* for `enter`, reverse preorder (NRL) instead of preorder (NLR);
* for `exit`, reverse postorder (RLN) instead of postorder (LRN).
* @returns {undefined}
* Nothing.
*
* @param {UnistNode} tree
* Tree to traverse.
* @param {Visitor | Visitors | Test} testOrEnterOrVisitors
* `unist-util-is`-compatible test, `enter`, or `visitors`.
* @param {Visitor | Visitors | boolean | null | undefined} [enterOrVisitorsOrReverse]
* `enter`, `visitors`, or `reverse` after a test.
* @param {boolean | null | undefined} [maybeReverse]
* Walk children from last to first;
* for `enter`, reverse preorder (NRL) instead of preorder (NLR);
* for `exit`, reverse postorder (RLN) instead of postorder (LRN).
* @returns {undefined}
* Nothing.
*
* @template {UnistNode} Tree
* Node type.
* @template {Test} Check
* `unist-util-is`-compatible test.
*/
export function visit(
tree,
testOrEnterOrVisitors,
enterOrVisitorsOrReverse,
maybeReverse
) {
/** @type {Test} */
let check
/** @type {Visitor | Visitors} */
let visitors
/** @type {boolean | null | undefined} */
let reverse
// Third parameter is an enter visitor or a visitors object, so expect a `test` too.
if (
typeof enterOrVisitorsOrReverse === 'function' ||
(enterOrVisitorsOrReverse && typeof enterOrVisitorsOrReverse === 'object')
) {
check = /** @type {Test} */ (testOrEnterOrVisitors)
visitors = enterOrVisitorsOrReverse
reverse = maybeReverse
}
// Assume no test.
else {
visitors = /** @type {Visitor | Visitors} */ (testOrEnterOrVisitors)
reverse = enterOrVisitorsOrReverse
}
if (typeof visitors === 'function') {
visitParents(tree, check, wrap(visitors), reverse)
} else {
visitParents(tree, check, wrapVisitors(visitors), reverse)
}
}
/**
* Turn `visit` visitors into `visitParents` visitors.
*
* @param {Visitors} visitors
* Visitors for `visit`.
* @returns {ParentsVisitors}
* Visitors for `visitParents`.
*/
function wrapVisitors(visitors) {
// Pass anything else straight to `visitParents` to get its errors.
if (!visitors || typeof visitors !== 'object') return visitors
return {
enter:
typeof visitors.enter === 'function'
? wrap(visitors.enter)
: visitors.enter,
exit:
typeof visitors.exit === 'function' ? wrap(visitors.exit) : visitors.exit
}
}
/**
* Turn a `visit` visitor into a `visitParents` one.
*
* @template {VisitorResult} Result
* Result type.
* @param {(node: UnistNode, index: number | undefined, parent: UnistParent | undefined) => Result} visitor
* Visitor for `visit`.
* @returns {(node: UnistNode, parents: Array<UnistParent>, index: number | undefined) => Result}
* Visitor for `visitParents`.
*/
function wrap(visitor) {
return overload
/**
* Call `visitor` with `index` and `parent` instead of `parents`.
*
* @param {UnistNode} node
* Visited node.
* @param {Array<UnistParent>} parents
* Ancestors of `node`.
* @param {number | undefined} index
* Index of `node` in its parent.
* @returns {Result}
* What to do next.
*/
function overload(node, parents, index) {
return visitor(node, index, parents[parents.length - 1])
}
}
+22
View File
@@ -0,0 +1,22 @@
(The MIT License)
Copyright (c) Titus Wormer <tituswormer@gmail.com>
Permission is hereby granted, free of charge, to any person obtaining
a copy of this software and associated documentation files (the
'Software'), to deal in the Software without restriction, including
without limitation the rights to use, copy, modify, merge, publish,
distribute, sublicense, and/or sell copies of the Software, and to
permit persons to whom the Software is furnished to do so, subject to
the following conditions:
The above copyright notice and this permission notice shall be
included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
+98
View File
@@ -0,0 +1,98 @@
{
"author": "Titus Wormer <tituswormer@gmail.com> (https://wooorm.com)",
"bugs": "https://github.com/syntax-tree/unist-util-visit/issues",
"contributors": [
"Titus Wormer <tituswormer@gmail.com> (https://wooorm.com)",
"Eugene Sharygin <eush77@gmail.com>",
"Richard Gibson <richard.gibson@gmail.com>"
],
"dependencies": {
"@types/unist": "^3.0.0",
"unist-util-is": "^6.0.0",
"unist-util-visit-parents": "^6.1.0"
},
"description": "unist utility to visit nodes",
"devDependencies": {
"@types/mdast": "^4.0.0",
"@types/node": "^26.0.0",
"c8": "^12.0.0",
"mdast-util-from-markdown": "^2.0.0",
"mdast-util-gfm": "^3.0.0",
"micromark-extension-gfm": "^3.0.0",
"prettier": "^3.0.0",
"remark-cli": "^12.0.0",
"remark-preset-wooorm": "^11.0.0",
"tsd": "^0.33.0",
"type-coverage": "^2.0.0",
"typescript": "^6.0.0",
"xo": "^5.0.0"
},
"exports": "./index.js",
"files": [
"lib/",
"index.d.ts",
"index.js"
],
"funding": {
"type": "opencollective",
"url": "https://opencollective.com/unified"
},
"keywords": [
"ast",
"hast",
"html",
"language",
"markdown",
"mdast",
"natural",
"nlcst",
"node",
"rehype",
"remark",
"retext",
"tree",
"unist-util",
"unist",
"utility",
"util",
"visit",
"walk",
"xast",
"xml"
],
"license": "MIT",
"name": "unist-util-visit",
"prettier": {
"bracketSpacing": false,
"semi": false,
"singleQuote": true,
"tabWidth": 2,
"trailingComma": "none",
"useTabs": false
},
"remarkConfig": {
"plugins": [
"remark-preset-wooorm"
]
},
"repository": "syntax-tree/unist-util-visit",
"scripts": {
"build": "tsc --build --clean && tsc --build && tsd && type-coverage",
"format": "remark --frail --output --quiet -- . && prettier --log-level warn --write -- . && xo --fix",
"prepack": "npm run build && npm run format",
"test-api": "node --conditions development test.js",
"test-coverage": "c8 --100 --reporter lcov npm run test-api",
"test": "npm run build && npm run format && npm run test-coverage"
},
"sideEffects": false,
"typeCoverage": {
"atLeast": 100,
"#": "needed `any`s",
"ignoreFiles": [
"lib/index.d.ts"
],
"strict": true
},
"type": "module",
"version": "5.2.0"
}
+373
View File
@@ -0,0 +1,373 @@
# 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.
## Contents
* [What is this?](#what-is-this)
* [When should I use this?](#when-should-i-use-this)
* [Install](#install)
* [Use](#use)
* [API](#api)
* [`visit(tree[, test], enter|visitors[, reverse])`](#visittree-test-entervisitors-reverse)
* [`CONTINUE`](#continue)
* [`EXIT`](#exit)
* [`SKIP`](#skip)
* [`Action`](#action)
* [`ActionTuple`](#actiontuple)
* [`BuildVisitor`](#buildvisitor)
* [`BuildVisitors`](#buildvisitors)
* [`Index`](#index)
* [`Test`](#test)
* [`Visitor`](#visitor)
* [`VisitorExit`](#visitorexit)
* [`VisitorResult`](#visitorresult)
* [`VisitorResultExit`](#visitorresultexit)
* [`Visitors`](#visitors)
* [Compatibility](#compatibility)
* [Related](#related)
* [Contribute](#contribute)
* [License](#license)
## What is this?
This is a very important utility for working with unist as it lets you walk the
tree.
## When should I use this?
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.
## Install
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
<script type="module">
import {CONTINUE, EXIT, SKIP, visit} from 'https://esm.sh/unist-util-visit@5?bundle'
</script>
```
## Use
```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' ]
```
## API
This package exports the identifiers
[`CONTINUE`][api-continue],
[`EXIT`][api-exit],
[`SKIP`][api-skip], and
[`visit`][api-visit].
It exports the [TypeScript][] types
[`ActionTuple`][api-action-tuple],
[`Action`][api-action],
[`BuildVisitor`][api-build-visitor],
[`BuildVisitors`][api-build-visitors],
[`Index`][api-index],
[`Test`][api-test],
[`VisitorExit`][api-visitor-exit],
[`VisitorResultExit`][api-visitor-result-exit],
[`VisitorResult`][api-visitor-result],
[`Visitors`][api-visitors], and
[`Visitor`][api-visitor].
There is no default export.
### `visit(tree[, test], enter|visitors[, reverse])`
This function works exactly the same as
[`unist-util-visit-parents`][github-vp],
but [`Visitor`][api-visitor] and [`VisitorExit`][api-visitor-exit] have a
different signature.
###### Signatures
* `visit(tree[, test], enter[, reverse])`
* `visit(tree[, test], visitors[, reverse])`
### `CONTINUE`
Continue traversing as normal (`true`).
### `EXIT`
Stop traversing immediately (`false`).
### `SKIP`
Do not traverse this node’s children (`'skip'`).
### `Action`
Union of the action types (TypeScript type).
See [`Action` in `unist-util-visit-parents`][github-vp-action].
### `ActionTuple`
List with an action and an index (TypeScript type).
See [`ActionTuple` in `unist-util-visit-parents`][github-vp-action-tuple].
### `BuildVisitor`
Build a typed `Visitor` function from a tree and a test (TypeScript type).
See [`BuildVisitor` in `unist-util-visit-parents`][github-vp-build-visitor].
### `BuildVisitors`
Build typed [`Visitors`][api-visitors] from a tree and a test (TypeScript
type).
See [`BuildVisitors` in `unist-util-visit-parents`][github-vp-build-visitors].
### `Index`
Move to the sibling at `index` next (TypeScript type).
See [`Index` in `unist-util-visit-parents`][github-vp-index].
### `Test`
[`unist-util-is`][github-unist-util-is] compatible test
(TypeScript type).
### `Visitor`
Handle a node (matching `test`, if given) before its children (TypeScript
type).
Like [`Visitor` in `unist-util-visit-parents`][github-vp-visitor],
but with `index` and `parent` instead of `parents`.
###### Parameters
* `node` ([`Node`][github-unist-node])
— found node
* `index` (`number` or `undefined`)
— index of `node` in `parent`,
or `undefined` for the tree itself
* `parent` ([`Node`][github-unist-node] or `undefined`)
— parent of `node`,
or `undefined` for the tree itself
###### Returns
What to do next ([`VisitorResult`][api-visitor-result]).
### `VisitorExit`
Handle a node (matching `test`, if given) after its children (TypeScript type).
Like [`VisitorExit` in `unist-util-visit-parents`][github-vp-visitor-exit],
but with `index` and `parent` instead of `parents`.
###### Parameters
* `node` ([`Node`][github-unist-node])
— found node
* `index` (`number` or `undefined`)
— index of `node` in `parent`,
or `undefined` for the tree itself
* `parent` ([`Node`][github-unist-node] or `undefined`)
— parent of `node`,
or `undefined` for the tree itself
###### Returns
What to do next ([`VisitorResultExit`][api-visitor-result-exit]).
### `VisitorResult`
Any value that can be returned from a visitor (TypeScript type).
See [`VisitorResult` in
`unist-util-visit-parents`][github-vp-visitor-result].
### `VisitorResultExit`
Any value that can be returned from an exit visitor (TypeScript type).
See [`VisitorResultExit` in
`unist-util-visit-parents`][github-vp-visitor-result-exit].
### `Visitors`
Handle nodes when entering and exiting them (TypeScript type).
See [`Visitors` in `unist-util-visit-parents`][github-vp-visitors].
###### Fields
* `enter` ([`Visitor`][api-visitor], optional)
— handle a node before its children
* `exit` ([`VisitorExit`][api-visitor-exit])
— handle a node after its children
## Compatibility
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.
## Related
* [`unist-util-visit-parents`][github-vp]
— walk the tree with a stack of parents
* [`unist-util-filter`](https://github.com/syntax-tree/unist-util-filter)
— create a new tree with all nodes that pass a test
* [`unist-util-map`](https://github.com/syntax-tree/unist-util-map)
— create a new tree with all nodes mapped by a given function
* [`unist-util-flatmap`](https://gitlab.com/staltz/unist-util-flatmap)
— create a new tree by mapping (to an array) with the given function
* [`unist-util-remove`](https://github.com/syntax-tree/unist-util-remove)
— remove nodes from a tree that pass a test
* [`unist-util-select`](https://github.com/syntax-tree/unist-util-select)
— select nodes with CSS-like selectors
## Contribute
See [`contributing.md`][health-contributing] in [`syntax-tree/.github`][health]
for ways to get started.
See [`support.md`][health-support] for ways to get help.
This project has a [code of conduct][health-coc].
By interacting with this repository,
organization,
or community you agree to abide by its terms.
## License
[MIT][file-license] © [Titus Wormer][wooorm]
<!-- Definition -->
[api-action]: #action
[api-action-tuple]: #actiontuple
[api-build-visitor]: #buildvisitor
[api-build-visitors]: #buildvisitors
[api-continue]: #continue
[api-exit]: #exit
[api-index]: #index
[api-skip]: #skip
[api-test]: #test
[api-visit]: #visittree-test-entervisitors-reverse
[api-visitor]: #visitor
[api-visitor-exit]: #visitorexit
[api-visitor-result]: #visitorresult
[api-visitor-result-exit]: #visitorresultexit
[api-visitors]: #visitors
[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-build-visitors]: https://github.com/syntax-tree/unist-util-visit-parents#buildvisitors
[github-vp-index]: https://github.com/syntax-tree/unist-util-visit-parents#index
[github-vp-visitor]: https://github.com/syntax-tree/unist-util-visit-parents#visitor
[github-vp-visitor-exit]: https://github.com/syntax-tree/unist-util-visit-parents#visitorexit
[github-vp-visitor-result]: https://github.com/syntax-tree/unist-util-visit-parents#visitorresult
[github-vp-visitor-result-exit]: https://github.com/syntax-tree/unist-util-visit-parents#visitorresultexit
[github-vp-visitors]: https://github.com/syntax-tree/unist-util-visit-parents#visitors
[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