{"id":13483329,"url":"https://github.com/syntax-tree/unist-util-visit-parents","last_synced_at":"2025-06-29T22:03:44.752Z","repository":{"id":41153156,"uuid":"54721341","full_name":"syntax-tree/unist-util-visit-parents","owner":"syntax-tree","description":"utility to recursively walk over unist nodes, with ancestral information","archived":false,"fork":false,"pushed_at":"2025-01-23T06:26:07.000Z","size":206,"stargazers_count":75,"open_issues_count":0,"forks_count":7,"subscribers_count":8,"default_branch":"main","last_synced_at":"2025-06-26T21:11:17.719Z","etag":null,"topics":["syntax-tree","unist","unist-util","util","visit"],"latest_commit_sha":null,"homepage":"https://unifiedjs.com","language":"JavaScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/syntax-tree.png","metadata":{"files":{"readme":"readme.md","changelog":null,"contributing":null,"funding":null,"license":"license","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null},"funding":{"github":"unifiedjs","open_collective":"unified"}},"created_at":"2016-03-25T13:47:08.000Z","updated_at":"2025-06-21T05:52:56.000Z","dependencies_parsed_at":"2024-01-13T18:23:59.426Z","dependency_job_id":"ac3ea357-58a1-400a-9f91-b10df8a49ff3","html_url":"https://github.com/syntax-tree/unist-util-visit-parents","commit_stats":{"total_commits":120,"total_committers":4,"mean_commits":30.0,"dds":"0.033333333333333326","last_synced_commit":"b4e685d4864cf039f39a6fa8f7a085bb62a82815"},"previous_names":[],"tags_count":24,"template":false,"template_full_name":null,"purl":"pkg:github/syntax-tree/unist-util-visit-parents","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/syntax-tree%2Funist-util-visit-parents","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/syntax-tree%2Funist-util-visit-parents/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/syntax-tree%2Funist-util-visit-parents/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/syntax-tree%2Funist-util-visit-parents/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/syntax-tree","download_url":"https://codeload.github.com/syntax-tree/unist-util-visit-parents/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/syntax-tree%2Funist-util-visit-parents/sbom","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":262293630,"owners_count":23288782,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["syntax-tree","unist","unist-util","util","visit"],"created_at":"2024-07-31T17:01:10.161Z","updated_at":"2025-06-29T22:03:44.705Z","avatar_url":"https://github.com/syntax-tree.png","language":"JavaScript","funding_links":["https://github.com/sponsors/unifiedjs","https://opencollective.com/unified"],"categories":["JavaScript"],"sub_categories":[],"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], visitor[, reverse])`](#visitparentstree-test-visitor-reverse)\n  * [`CONTINUE`](#continue)\n  * [`EXIT`](#exit)\n  * [`SKIP`](#skip)\n  * [`Action`](#action)\n  * [`ActionTuple`](#actiontuple)\n  * [`BuildVisitor`](#buildvisitor)\n  * [`Index`](#index)\n  * [`Test`](#test)\n  * [`Visitor`](#visitor)\n  * [`VisitorResult`](#visitorresult)\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\u003cscript type=\"module\"\u003e\n  import {visitParents} from 'https://esm.sh/unist-util-visit-parents@6?bundle'\n\u003c/script\u003e\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 =\u003e 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], visitor[, 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, in *reverse preorder*\n(**NRL**).\n\nYou can choose for which nodes `visitor` is called by passing a `test`.\nFor complex tests, you should test yourself in `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.\nSee [`Visitor`][api-visitor] for more info.\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* `visitor` ([`Visitor`][api-visitor])\n  — handle each node\n* `reverse` (`boolean`, default: `false`)\n  — traverse in reverse preorder (NRL) instead of the default preorder (NLR)\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'`).\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### `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) (TypeScript type).\n\nVisitors are free to transform `node`.\nThey can also transform the parent of node (the last of `ancestors`).\n\nReplacing `node` itself, if `SKIP` is not returned, still causes its\ndescendants to be walked (which is a bug).\n\nWhen adding or removing previous siblings of `node` (or next siblings, in\ncase of reverse), the `Visitor` should return a new `Index` to specify the\nsibling to traverse after `node` is traversed.\nAdding or removing next siblings of `node` (or previous siblings, in case\nof reverse) is handled as expected without needing to return a new `Index`.\n\nRemoving the children property of an ancestor still results in them being\ntraversed.\n\n###### Parameters\n\n* `node` ([`Node`][node])\n  — found node\n* `parents` ([`Array\u003cNode\u003e`][node])\n  — ancestors of `node`\n\n###### Returns\n\nWhat to do next.\n\nAn `Index` is treated as a tuple of `[CONTINUE, Index]`.\nAn `Action` is treated as a tuple of `[Action]`.\n\nPassing a tuple back only makes sense if the `Action` is `SKIP`.\nWhen the `Action` is `EXIT`, that action can be returned.\nWhen the `Action` is `CONTINUE`, `Index` can be returned.\n\n### `VisitorResult`\n\nAny value that can be returned from a visitor (TypeScript type).\n\n###### Type\n\n```ts\ntype VisitorResult =\n  | Action\n  | ActionTuple\n  | Index\n  | null\n  | undefined\n  | void\n```\n\n## Types\n\nThis package is fully typed with [TypeScript][].\nIt exports the additional types [`Action`][api-action],\n[`ActionTuple`][api-actiontuple], [`BuildVisitor`][api-buildvisitor],\n[`Index`][api-index], [`Test`][api-test], [`Visitor`][api-visitor], and\n[`VisitorResult`][api-visitorresult].\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`](https://github.com/syntax-tree/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\u003c!-- Definition --\u003e\n\n[build-badge]: https://github.com/syntax-tree/unist-util-visit-parents/workflows/main/badge.svg\n\n[build]: https://github.com/syntax-tree/unist-util-visit-parents/actions\n\n[coverage-badge]: https://img.shields.io/codecov/c/github/syntax-tree/unist-util-visit-parents.svg\n\n[coverage]: https://codecov.io/github/syntax-tree/unist-util-visit-parents\n\n[downloads-badge]: https://img.shields.io/npm/dm/unist-util-visit-parents.svg\n\n[downloads]: https://www.npmjs.com/package/unist-util-visit-parents\n\n[size-badge]: https://img.shields.io/badge/dynamic/json?label=minzipped%20size\u0026query=$.size.compressedSize\u0026url=https://deno.bundlejs.com/?q=unist-util-visit-parents\n\n[size]: https://bundlejs.com/?q=unist-util-visit-parents\n\n[sponsors-badge]: https://opencollective.com/unified/sponsors/badge.svg\n\n[backers-badge]: https://opencollective.com/unified/backers/badge.svg\n\n[collective]: https://opencollective.com/unified\n\n[chat-badge]: https://img.shields.io/badge/chat-discussions-success.svg\n\n[chat]: https://github.com/syntax-tree/unist/discussions\n\n[npm]: https://docs.npmjs.com/cli/install\n\n[esm]: https://gist.github.com/sindresorhus/a39789f98801d908bbc7ff3ecc99d99c\n\n[esmsh]: https://esm.sh\n\n[typescript]: https://www.typescriptlang.org\n\n[license]: license\n\n[author]: https://wooorm.com\n\n[health]: https://github.com/syntax-tree/.github\n\n[contributing]: https://github.com/syntax-tree/.github/blob/HEAD/contributing.md\n\n[support]: https://github.com/syntax-tree/.github/blob/HEAD/support.md\n\n[coc]: https://github.com/syntax-tree/.github/blob/HEAD/code-of-conduct.md\n\n[unist]: https://github.com/syntax-tree/unist\n\n[node]: https://github.com/syntax-tree/unist#node\n\n[depth-first]: https://github.com/syntax-tree/unist#depth-first-traversal\n\n[tree-traversal]: https://github.com/syntax-tree/unist#tree-traversal\n\n[preorder]: https://github.com/syntax-tree/unist#preorder\n\n[unist-util-visit]: https://github.com/syntax-tree/unist-util-visit\n\n[unist-util-is]: https://github.com/syntax-tree/unist-util-is\n\n[api-visitparents]: #visitparentstree-test-visitor-reverse\n\n[api-continue]: #continue\n\n[api-exit]: #exit\n\n[api-skip]: #skip\n\n[api-action]: #action\n\n[api-actiontuple]: #actiontuple\n\n[api-buildvisitor]: #buildvisitor\n\n[api-index]: #index\n\n[api-test]: #test\n\n[api-visitor]: #visitor\n\n[api-visitorresult]: #visitorresult\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fsyntax-tree%2Funist-util-visit-parents","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fsyntax-tree%2Funist-util-visit-parents","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fsyntax-tree%2Funist-util-visit-parents/lists"}