{"_id":"unist-util-visit-parents@6.1.0","bugs":{"url":"https://github.com/syntax-tree/unist-util-visit-parents/issues"},"dist":{"shasum":"eff74226782b378957731bcf62cf5e9d8a080606","tarball":"http://registry-npm.ywwl.com/unist-util-visit-parents/download/unist-util-visit-parents-6.1.0.tgz","fileCount":14,"integrity":"sha512-vKXxz6O38sKDj1mtx/k2cWvkkFBuYGx6uGrlzBkVeWCRF/F8sdI0j3Wbkz/5rVNRRuVc8UrgRdbCxNhmYXgP3w==","signatures":[{"sig":"MEUCIQCn4UtAh+1CsUT4pyCq3xTwN91xchGWEOe7PXBLLVu2EwIgX+cEPb4HT1WwDHlxgUckZc/X0d3cbbo0azo6E6l+mKg=","keyid":"SHA256:DhQ8wR5APBvFHLF/+Tc+AYvPOdTpcIDqOhxsBHRwC7U"},{"keyid":"SHA256:DhQ8wR5APBvFHLF/+Tc+AYvPOdTpcIDqOhxsBHRwC7U","sig":"MEYCIQDuLdL9igozE0rHykMc+ih8GEhv7OE57YdlA0TIOJ1MJQIhAICi0r1IygvppA6iGhcl9DSgCELvqCCQFaCXA9cbVqIk"}],"unpackedSize":62728,"size":13163,"noattachment":false,"key":"/unist-util-visit-parents/-/unist-util-visit-parents-6.1.0.tgz"},"name":"unist-util-visit-parents","type":"module","types":"./index.d.ts","author":{"url":"https://wooorm.com","name":"Titus Wormer","email":"tituswormer@gmail.com"},"exports":{".":"./index.js","./do-not-use-color":{"node":"./lib/color.node.js","default":"./lib/color.js"}},"funding":{"url":"https://opencollective.com/unified","type":"opencollective"},"gitHead":"9b6fef55105f069ef5df809efcb977d58f5be277","license":"MIT","scripts":{"test":"npm run build && npm run format && npm run test-coverage","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"},"version":"6.1.0","_npmUser":{"name":"wooorm","email":"tituswormer@gmail.com"},"homepage":"https://github.com/syntax-tree/unist-util-visit-parents#readme","keywords":["ast","check","parents","parent","traverse","tree","unist-util","unist","utility","util","visit","walk"],"prettier":{"semi":false,"useTabs":false,"tabWidth":2,"singleQuote":true,"trailingComma":"none","bracketSpacing":false},"repository":{"url":"git+https://github.com/syntax-tree/unist-util-visit-parents.git","type":"git"},"_npmVersion":"12.2.1","description":"unist utility to recursively walk over nodes, with ancestral information","directories":{},"maintainers":[{"name":"wooorm","email":"tituswormer@gmail.com"},{"name":"kmck","email":"keith@mcknig.ht"}],"sideEffects":false,"_nodeVersion":"26.12.1","contributors":[{"url":"https://wooorm.com","name":"Titus Wormer","email":"tituswormer@gmail.com"}],"dependencies":{"@types/unist":"^3.0.0","unist-util-is":"^6.0.0"},"remarkConfig":{"plugins":["remark-preset-wooorm"]},"typeCoverage":{"#":"needed `any`s","strict":true,"atLeast":100,"ignoreFiles":["lib/index.d.ts"]},"_hasShrinkwrap":false,"devDependencies":{"c8":"^12.0.0","xo":"^5.0.0","tsd":"^0.33.0","prettier":"^3.0.0","remark-cli":"^12.0.0","strip-ansi":"^7.0.0","typescript":"^6.0.0","@types/hast":"^3.0.0","@types/node":"^26.0.0","@types/xast":"^2.0.0","@types/mdast":"^4.0.0","type-coverage":"^2.0.0","mdast-util-gfm":"^3.0.0","remark-preset-wooorm":"^11.0.0","micromark-extension-gfm":"^3.0.0","mdast-util-from-markdown":"^2.0.0"},"_npmOperationalInternal":{"host":"s3://npm-registry-packages-npm-production","tmp":"tmp/unist-util-visit-parents_6.1.0_1792864196424_0.33605904108911666"},"readme":"# unist-util-visit-parents\n\n[![Build][build-badge]][build]\n[![Coverage][coverage-badge]][coverage]\n[![Downloads][downloads-badge]][downloads]\n[![Size][size-badge]][size]\n[![Sponsors][sponsors-badge]][collective]\n[![Backers][backers-badge]][collective]\n[![Chat][chat-badge]][chat]\n\n[unist][] utility to walk the tree with a stack of parents.\n\n## Contents\n\n* [What is this?](#what-is-this)\n* [When should I use this?](#when-should-i-use-this)\n* [Install](#install)\n* [Use](#use)\n* [API](#api)\n  * [`visitParents(tree[, test], enter|visitors[, reverse])`](#visitparentstree-test-entervisitors-reverse)\n  * [`CONTINUE`](#continue)\n  * [`EXIT`](#exit)\n  * [`SKIP`](#skip)\n  * [`Action`](#action)\n  * [`ActionTuple`](#actiontuple)\n  * [`BuildVisitor`](#buildvisitor)\n  * [`BuildVisitors`](#buildvisitors)\n  * [`Index`](#index)\n  * [`Test`](#test)\n  * [`Visitor`](#visitor)\n  * [`VisitorExit`](#visitorexit)\n  * [`VisitorResult`](#visitorresult)\n  * [`VisitorResultExit`](#visitorresultexit)\n  * [`Visitors`](#visitors)\n* [Types](#types)\n* [Compatibility](#compatibility)\n* [Related](#related)\n* [Contribute](#contribute)\n* [License](#license)\n\n## What is this?\n\nThis is a very important utility for working with unist as it lets you walk the\ntree.\n\n## When should I use this?\n\nYou can use this utility when you want to walk the tree and want to know about\nevery parent of each node.\nYou can use [`unist-util-visit`][unist-util-visit] if you don’t care about the\nentire stack of parents.\n\n## Install\n\nThis package is [ESM only][esm].\nIn Node.js (version 16+), install with [npm][]:\n\n```sh\nnpm install unist-util-visit-parents\n```\n\nIn Deno with [`esm.sh`][esmsh]:\n\n```js\nimport {visitParents} from 'https://esm.sh/unist-util-visit-parents@6'\n```\n\nIn browsers with [`esm.sh`][esmsh]:\n\n```html\n<script type=\"module\">\n  import {visitParents} from 'https://esm.sh/unist-util-visit-parents@6?bundle'\n</script>\n```\n\n## Use\n\n```js\nimport {visitParents} from 'unist-util-visit-parents'\nimport {fromMarkdown} from 'mdast-util-from-markdown'\n\nconst tree = fromMarkdown('Some *emphasis*, **strong**, and `code`.')\n\nvisitParents(tree, 'strong', function (node, ancestors) {\n  console.log(node.type, ancestors.map(ancestor => ancestor.type))\n})\n```\n\nYields:\n\n```js\nstrong ['root', 'paragraph']\n```\n\n## API\n\nThis package exports the identifiers [`CONTINUE`][api-continue],\n[`EXIT`][api-exit], [`SKIP`][api-skip], and [`visitParents`][api-visitparents].\nThere is no default export.\n\n### `visitParents(tree[, test], enter|visitors[, reverse])`\n\nVisit nodes, with ancestral information.\n\nThis algorithm performs *[depth-first][]* *[tree traversal][tree-traversal]*\nin *[preorder][]* (**NLR**) or if `reverse` is given,\nin *reverse preorder* (**NRL**).\nAn [`exit`][api-visitors] visitor is called in *[postorder][]* (**LRN**)\nor if `reverse` is given, in *reverse postorder* (**RLN**).\n\nYou can choose which nodes are visited by passing a `test`.\nFor complex tests, you should test yourself in a visitor, as it will be\nfaster and will have improved type information.\n\nWalking the tree is an intensive task.\nMake use of the return values of the visitor when possible.\nInstead of walking a tree multiple times, walk it once, use\n[`unist-util-is`][unist-util-is] to check if a node matches, and then perform\ndifferent operations.\n\nYou can change the tree.\nChanges inside a node made when entering it are walked,\nchanges made when exiting it are not.\nSee [`Visitor`][api-visitor] and [`VisitorExit`][api-visitorexit] for more\ninfo.\n\n###### Signatures\n\n* `visitParents(tree[, test], enter[, reverse])`\n* `visitParents(tree[, test], visitors[, reverse])`\n\n###### Parameters\n\n* `tree` ([`Node`][node])\n  — tree to traverse\n* `test` ([`Test`][api-test], optional)\n  — [`unist-util-is`][unist-util-is]-compatible test\n* `enter` ([`Visitor`][api-visitor])\n  — handle each node when entering it\n* `visitors` ([`Visitors`][api-visitors])\n  — handle each node when exiting it,\n  and optionally when entering it\n* `reverse` (`boolean`, default: `false`)\n  — walk children from last to first;\n  for `enter`, reverse preorder (NRL) instead of preorder (NLR);\n  for `exit`, reverse postorder (RLN) instead of postorder (LRN)\n\n###### Returns\n\nNothing (`undefined`).\n\n### `CONTINUE`\n\nContinue traversing as normal (`true`).\n\n### `EXIT`\n\nStop traversing immediately (`false`).\n\n### `SKIP`\n\nDo not traverse this node’s children (`'skip'`).\nOnly works when entering a node.\n\n### `Action`\n\nUnion of the action types (TypeScript type).\n\n###### Type\n\n```ts\ntype Action = typeof CONTINUE | typeof EXIT | typeof SKIP\n```\n\n### `ActionTuple`\n\nList with one or two values, the first an action, the second an index\n(TypeScript type).\n\n###### Type\n\n```ts\ntype ActionTuple = [\n  (Action | null | undefined | void)?,\n  (Index | null | undefined)?\n]\n```\n\n### `BuildVisitor`\n\nBuild a typed `Visitor` function from a tree and a test (TypeScript type).\n\nIt will infer which values are passed as `node` and which as `parents`.\n\n###### Type parameters\n\n* `Tree` ([`Node`][node], default: `Node`)\n  — tree type\n* `Check` ([`Test`][api-test], default: `Test`)\n  — test type\n\n###### Returns\n\n[`Visitor`][api-visitor].\n\n### `BuildVisitors`\n\nBuild typed [`Visitors`][api-visitors] from a tree and a test (TypeScript\ntype).\n\nIt will infer which values are passed as `node` and which as `parents`.\n\n###### Type parameters\n\n* `Tree` ([`Node`][node], default: `Node`)\n  — tree type\n* `Check` ([`Test`][api-test], default: `Test`)\n  — test type\n\n###### Returns\n\n[`Visitors`][api-visitors].\n\n### `Index`\n\nMove to the sibling at `index` next (after node itself is completely\ntraversed) (TypeScript type).\n\nUseful if mutating the tree, such as removing the node the visitor is currently\non, or any of its previous siblings.\nResults less than `0` or greater than or equal to `children.length` stop\ntraversing the parent.\n\n###### Type\n\n```ts\ntype Index = number\n```\n\n### `Test`\n\n[`unist-util-is`][unist-util-is] compatible test (TypeScript type).\n\n### `Visitor`\n\nHandle a node (matching `test`, if given) before its children (TypeScript\ntype).\n\nChanges inside `node` are walked next.\nReplacing or removing it is easier in `exit`.\nWhen replacing or removing it here,\nreturn `SKIP` (as otherwise its old children are walked),\nor `[SKIP, index]` to continue with the node now at `index`.\nChanging next siblings (in walk order) is fine;\nchanging previous siblings needs a new `Index`.\nRemoving an ancestor or changing its previous siblings is not supported.\n\n###### Parameters\n\n* `node` ([`Node`][node])\n  — found node\n* `parents` ([`Array<Node>`][node])\n  — ancestors of `node`\n* `index` (`number` or `undefined`)\n  — index of `node` in its parent (the last of `parents`),\n  or `undefined` for the tree itself\n\n###### Returns\n\nWhat to do next ([`VisitorResult`][api-visitorresult]).\n\nPassing a tuple back ([`ActionTuple`][api-actiontuple]) only makes sense as\n`[SKIP, Index]`.\n\n### `VisitorExit`\n\nHandle a node (matching `test`, if given) after its children (TypeScript type).\n\nChanges inside `node` are not walked,\nwhich makes this a good place to change `node` based on its children,\nor to replace or remove it.\nWhen removing `node` or changing previous siblings (in walk order),\nreturn a new `Index`.\nRemoving an ancestor or changing its previous siblings is not supported.\n\n###### Parameters\n\n* `node` ([`Node`][node])\n  — found node\n* `parents` ([`Array<Node>`][node])\n  — ancestors of `node`\n* `index` (`number` or `undefined`)\n  — index of `node` in its parent (the last of `parents`),\n  or `undefined` for the tree itself\n\n###### Returns\n\nWhat to do next ([`VisitorResultExit`][api-visitorresultexit]).\n\n`CONTINUE` (`true`, nothing) continues as normal,\n`EXIT` (`false`) stops traversing,\nand `Index` continues at that sibling.\n\n### `VisitorResult`\n\nAny value that can be returned from an enter visitor (TypeScript type).\n\nLike [`VisitorResultExit`][api-visitorresultexit] but `SKIP` can be returned to\nnot walk children of the node,\nand [`ActionTuple`][api-actiontuple] can be used to combine an action with an\nindex.\n\n###### Type\n\n```ts\ntype VisitorResult =\n  | Action\n  | ActionTuple\n  | Index\n  | null\n  | undefined\n  | void\n```\n\n### `VisitorResultExit`\n\nAny value that can be returned from an exit visitor (TypeScript type).\n\n###### Type\n\n```ts\ntype VisitorResultExit = Index | boolean | null | undefined | void\n```\n\n### `Visitors`\n\nHandle nodes when entering and exiting them (TypeScript type).\nThe `enter` visitor is optional while `exit` is required.\n\nPass an `enter` visitor directly to only handle nodes when entering them.\n\n`enter` is called before children (if any) are walked and `exit` after that.\n`exit` is still called when `enter` returns `SKIP`.\nBoth `enter` and `exit` are only called for nodes that match an optional `test`.\n\n###### Fields\n\n* `enter` ([`Visitor`][api-visitor], optional)\n  — handle a node before its children\n* `exit` ([`VisitorExit`][api-visitorexit])\n  — handle a node after its children\n\n## Types\n\nThis package is fully typed with [TypeScript][].\nIt exports the additional types [`Action`][api-action],\n[`ActionTuple`][api-actiontuple],\n[`BuildVisitor`][api-buildvisitor],\n[`BuildVisitors`][api-buildvisitors],\n[`Index`][api-index],\n[`Test`][api-test],\n[`Visitor`][api-visitor],\n[`VisitorExit`][api-visitorexit],\n[`VisitorResult`][api-visitorresult],\n[`VisitorResultExit`][api-visitorresultexit], and\n[`Visitors`][api-visitors].\n\n## Compatibility\n\nProjects maintained by the unified collective are compatible with maintained\nversions of Node.js.\n\nWhen we cut a new major release, we drop support for unmaintained versions of\nNode.\nThis means we try to keep the current release line,\n`unist-util-visit-parents@^6`, compatible with Node.js 16.\n\n## Related\n\n* [`unist-util-visit`][unist-util-visit]\n  — walk the tree with one parent\n* [`unist-util-filter`](https://github.com/syntax-tree/unist-util-filter)\n  — create a new tree with all nodes that pass a test\n* [`unist-util-map`](https://github.com/syntax-tree/unist-util-map)\n  — create a new tree with all nodes mapped by a given function\n* [`unist-util-flatmap`](https://gitlab.com/staltz/unist-util-flatmap)\n  — create a new tree by mapping (to an array) with the given function\n* [`unist-util-remove`](https://github.com/syntax-tree/unist-util-remove)\n  — remove nodes from a tree that pass a test\n* [`unist-util-select`](https://github.com/syntax-tree/unist-util-select)\n  — select nodes with CSS-like selectors\n\n## Contribute\n\nSee [`contributing.md`][contributing] in [`syntax-tree/.github`][health] for\nways to get started.\nSee [`support.md`][support] for ways to get help.\n\nThis project has a [code of conduct][coc].\nBy interacting with this repository, organization, or community you agree to\nabide by its terms.\n\n## License\n\n[MIT][license] © [Titus Wormer][author]\n\n<!-- Definition -->\n\n[api-action]: #action\n\n[api-actiontuple]: #actiontuple\n\n[api-buildvisitor]: #buildvisitor\n\n[api-buildvisitors]: #buildvisitors\n\n[api-continue]: #continue\n\n[api-exit]: #exit\n\n[api-index]: #index\n\n[api-skip]: #skip\n\n[api-test]: #test\n\n[api-visitor]: #visitor\n\n[api-visitorexit]: #visitorexit\n\n[api-visitorresult]: #visitorresult\n\n[api-visitorresultexit]: #visitorresultexit\n\n[api-visitors]: #visitors\n\n[api-visitparents]: #visitparentstree-test-entervisitors-reverse\n\n[author]: https://wooorm.com\n\n[backers-badge]: https://opencollective.com/unified/backers/badge.svg\n\n[build]: https://github.com/syntax-tree/unist-util-visit-parents/actions\n\n[build-badge]: https://github.com/syntax-tree/unist-util-visit-parents/workflows/main/badge.svg\n\n[chat]: https://github.com/syntax-tree/unist/discussions\n\n[chat-badge]: https://img.shields.io/badge/chat-discussions-success.svg\n\n[coc]: https://github.com/syntax-tree/.github/blob/HEAD/code-of-conduct.md\n\n[collective]: https://opencollective.com/unified\n\n[contributing]: https://github.com/syntax-tree/.github/blob/HEAD/contributing.md\n\n[coverage]: https://codecov.io/github/syntax-tree/unist-util-visit-parents\n\n[coverage-badge]: https://img.shields.io/codecov/c/github/syntax-tree/unist-util-visit-parents.svg\n\n[depth-first]: https://github.com/syntax-tree/unist#depth-first-traversal\n\n[downloads]: https://www.npmjs.com/package/unist-util-visit-parents\n\n[downloads-badge]: https://img.shields.io/npm/dm/unist-util-visit-parents.svg\n\n[esm]: https://gist.github.com/sindresorhus/a39789f98801d908bbc7ff3ecc99d99c\n\n[esmsh]: https://esm.sh\n\n[health]: https://github.com/syntax-tree/.github\n\n[license]: license\n\n[node]: https://github.com/syntax-tree/unist#node\n\n[npm]: https://docs.npmjs.com/cli/install\n\n[postorder]: https://github.com/syntax-tree/unist#postorder\n\n[preorder]: https://github.com/syntax-tree/unist#preorder\n\n[size]: https://bundlejs.com/?q=unist-util-visit-parents\n\n[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\n\n[sponsors-badge]: https://opencollective.com/unified/sponsors/badge.svg\n\n[support]: https://github.com/syntax-tree/.github/blob/HEAD/support.md\n\n[tree-traversal]: https://github.com/syntax-tree/unist#tree-traversal\n\n[typescript]: https://www.typescriptlang.org\n\n[unist]: https://github.com/syntax-tree/unist\n\n[unist-util-is]: https://github.com/syntax-tree/unist-util-is\n\n[unist-util-visit]: https://github.com/syntax-tree/unist-util-visit\n","publish_time":1792864196518,"_cnpm_publish_time":1792864196518,"dist-tags":{"latest":"6.1.0"}}