{"author":{"name":"Titus Wormer","email":"tituswormer@gmail.com","url":"https://wooorm.com"},"bugs":{"url":"https://github.com/syntax-tree/mdast-util-find-and-replace/issues"},"contributors":[{"name":"Titus Wormer","email":"tituswormer@gmail.com","url":"https://wooorm.com"}],"dependencies":{"@types/mdast":"^4.0.0","escape-string-regexp":"^5.0.0","unist-util-is":"^6.0.0","unist-util-visit-parents":"^6.0.0"},"description":"mdast utility to find and replace text in a tree","devDependencies":{"@types/node":"^22.0.0","c8":"^10.0.0","prettier":"^3.0.0","remark-cli":"^12.0.0","remark-preset-wooorm":"^10.0.0","type-coverage":"^2.0.0","typescript":"^5.0.0","unist-builder":"^4.0.0","xo":"^0.60.0"},"exports":"./index.js","funding":{"type":"opencollective","url":"https://opencollective.com/unified"},"keywords":["find","markdown","mdast-util","mdast","unist","utility","util","replace"],"license":"MIT","name":"mdast-util-find-and-replace","prettier":{"bracketSpacing":false,"semi":false,"singleQuote":true,"tabWidth":2,"trailingComma":"none","useTabs":false},"remarkConfig":{"plugins":["remark-preset-wooorm"]},"repository":{"type":"git","url":"git+https://github.com/syntax-tree/mdast-util-find-and-replace.git"},"scripts":{"build":"tsc --build --clean && tsc --build && type-coverage","format":"remark --frail --output --quiet -- . && prettier --log-level warn --write -- . && xo --fix","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,"ignoreFiles":["lib/index.d.ts"],"strict":true},"type":"module","version":"3.0.2","xo":{"prettier":true,"rules":{"unicorn/prefer-at":"off"}},"_id":"mdast-util-find-and-replace@3.0.2","gitHead":"fd73ef856ab4f7b6326e3255aea36f439b75e2d5","types":"./index.d.ts","homepage":"https://github.com/syntax-tree/mdast-util-find-and-replace#readme","_nodeVersion":"23.1.0","_npmVersion":"11.0.0","dist":{"integrity":"sha512-Tmd1Vg/m3Xz43afeNxDIhWRtFZgM2VLyaf4vSTYwudTyeuTneoL3qtWMA5jeLyz/O1vDJmmV4QuScFCA2tBPwg==","shasum":"70a3174c894e14df722abf43bc250cbae44b11df","tarball":"http://registry-npm.ywwl.com/mdast-util-find-and-replace/download/mdast-util-find-and-replace-3.0.2.tgz","fileCount":9,"unpackedSize":23400,"signatures":[{"keyid":"SHA256:jl3bwswu80PjjokCgh0o2w5c2U4LhQAE57gj9cz1kzA","sig":"MEUCIQDDLZrJxoCXJwbO+gOtIPUFsxlM+id5LJ7D0exetRB4WgIgNwW16ViM7D2vIwWfoV0jj1ZvNkkUb+1alcYF/hW+qFA="}],"size":7487,"noattachment":false,"key":"/mdast-util-find-and-replace/-/mdast-util-find-and-replace-3.0.2.tgz"},"_npmUser":{"name":"wooorm","email":"tituswormer@gmail.com"},"directories":{},"maintainers":[{"name":"wooorm","email":"tituswormer@gmail.com"},{"name":"kmck","email":"keith@mcknig.ht"}],"_npmOperationalInternal":{"host":"s3://npm-registry-packages-npm-production","tmp":"tmp/mdast-util-find-and-replace_3.0.2_1735907255762_0.9422549044447726"},"_hasShrinkwrap":false,"readme":"# mdast-util-find-and-replace\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[mdast][] utility to find and replace things.\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  * [`findAndReplace(tree, list[, options])`](#findandreplacetree-list-options)\n  * [`Find`](#find)\n  * [`FindAndReplaceList`](#findandreplacelist)\n  * [`FindAndReplaceTuple`](#findandreplacetuple)\n  * [`Options`](#options)\n  * [`RegExpMatchObject`](#regexpmatchobject)\n  * [`Replace`](#replace)\n  * [`ReplaceFunction`](#replacefunction)\n* [Types](#types)\n* [Compatibility](#compatibility)\n* [Security](#security)\n* [Related](#related)\n* [Contribute](#contribute)\n* [License](#license)\n\n## What is this?\n\nThis package is a utility that lets you find patterns (`string`, `RegExp`) in\ntext and replace them with nodes.\n\n## When should I use this?\n\nThis utility is typically useful when you have regexes and want to modify mdast.\nOne example is when you have some form of “mentions” (such as\n`/@([a-z][_a-z0-9])\\b/gi`) and want to create links to persons from them.\n\nA similar package, [`hast-util-find-and-replace`][hast-util-find-and-replace]\ndoes the same but on [hast][].\n\n## Install\n\nThis package is [ESM only][esm].\nIn Node.js (version 16+), install with [npm][]:\n\n```sh\nnpm install mdast-util-find-and-replace\n```\n\nIn Deno with [`esm.sh`][esmsh]:\n\n```js\nimport {findAndReplace} from 'https://esm.sh/mdast-util-find-and-replace@3'\n```\n\nIn browsers with [`esm.sh`][esmsh]:\n\n```html\n<script type=\"module\">\n  import {findAndReplace} from 'https://esm.sh/mdast-util-find-and-replace@3?bundle'\n</script>\n```\n\n## Use\n\n```js\nimport {findAndReplace} from 'mdast-util-find-and-replace'\nimport {u} from 'unist-builder'\nimport {inspect} from 'unist-util-inspect'\n\nconst tree = u('paragraph', [\n  u('text', 'Some '),\n  u('emphasis', [u('text', 'emphasis')]),\n  u('text', ' and '),\n  u('strong', [u('text', 'importance')]),\n  u('text', '.')\n])\n\nfindAndReplace(tree, [\n  [/and/gi, 'or'],\n  [/emphasis/gi, 'em'],\n  [/importance/gi, 'strong'],\n  [\n    /Some/g,\n    function ($0) {\n      return u('link', {url: '//example.com#' + $0}, [u('text', $0)])\n    }\n  ]\n])\n\nconsole.log(inspect(tree))\n```\n\nYields:\n\n```txt\nparagraph[8]\n├─0 link[1]\n│   │ url: \"//example.com#Some\"\n│   └─0 text \"Some\"\n├─1 text \" \"\n├─2 emphasis[1]\n│   └─0 text \"em\"\n├─3 text \" \"\n├─4 text \"or\"\n├─5 text \" \"\n├─6 strong[1]\n│   └─0 text \"strong\"\n└─7 text \".\"\n```\n\n## API\n\nThis package exports the identifier [`findAndReplace`][api-find-and-replace].\nThere is no default export.\n\n### `findAndReplace(tree, list[, options])`\n\nFind patterns in a tree and replace them.\n\nThe algorithm searches the tree in *[preorder][]* for complete values in\n[`Text`][text] nodes.\nPartial matches are not supported.\n\n###### Parameters\n\n* `tree` ([`Node`][node])\n  — tree to change\n* `list` ([`FindAndReplaceList`][api-find-and-replace-list] or\n  [`FindAndReplaceTuple`][api-find-and-replace-tuple])\n  — one or more find-and-replace pairs\n* `options` ([`Options`][api-options])\n  — configuration\n\n###### Returns\n\nNothing (`undefined`).\n\n### `Find`\n\nPattern to find (TypeScript type).\n\nStrings are escaped and then turned into global expressions.\n\n###### Type\n\n```ts\ntype Find = RegExp | string\n```\n\n### `FindAndReplaceList`\n\nSeveral find and replaces, in array form (TypeScript type).\n\n###### Type\n\n```ts\ntype FindAndReplaceList = Array<FindAndReplaceTuple>\n```\n\nSee [`FindAndReplaceTuple`][api-find-and-replace-tuple].\n\n### `FindAndReplaceTuple`\n\nFind and replace in tuple form (TypeScript type).\n\n###### Type\n\n```ts\ntype FindAndReplaceTuple = [Find, Replace?]\n```\n\nSee [`Find`][api-find] and [`Replace`][api-replace].\n\n### `Options`\n\nConfiguration (TypeScript type).\n\n###### Fields\n\n* `ignore` ([`Test`][test], optional)\n  — test for which elements to ignore\n\n### `RegExpMatchObject`\n\nInfo on the match (TypeScript type).\n\n###### Fields\n\n* `index` (`number`)\n  — the index of the search at which the result was found\n* `input` (`string`)\n  — a copy of the search string in the text node\n* `stack` ([`Array<Node>`][node])\n  — all ancestors of the text node, where the last node is the text itself\n\n### `Replace`\n\nThing to replace with (TypeScript type).\n\n###### Type\n\n```ts\ntype Replace = ReplaceFunction | string\n```\n\nSee [`ReplaceFunction`][api-replace-function].\n\n### `ReplaceFunction`\n\nCallback called when a search matches (TypeScript type).\n\n###### Parameters\n\nThe parameters are the result of corresponding search expression:\n\n* `value` (`string`)\n  — whole match\n* `...capture` (`Array<string>`)\n  — matches from regex capture groups\n* `match` ([`RegExpMatchObject`][api-regexp-match-object])\n  — info on the match\n\n###### Returns\n\nThing to replace with:\n\n* when `null`, `undefined`, `''`, remove the match\n* …or when `false`, do not replace at all\n* …or when `string`, replace with a text node of that value\n* …or when `Node` or `Array<Node>`, replace with those nodes\n\n## Types\n\nThis package is fully typed with [TypeScript][].\nIt exports the additional types [`Find`][api-find],\n[`FindAndReplaceList`][api-find-and-replace-list],\n[`FindAndReplaceTuple`][api-find-and-replace-tuple],\n[`Options`][api-options],\n[`RegExpMatchObject`][api-regexp-match-object],\n[`Replace`][api-replace], and\n[`ReplaceFunction`][api-replace-function].\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`mdast-util-find-and-replace@^3`, compatible with Node.js 16.\n\n## Security\n\nUse of `mdast-util-find-and-replace` does not involve [hast][] or user content\nso there are no openings for [cross-site scripting (XSS)][xss] attacks.\n\n## Related\n\n* [`hast-util-find-and-replace`](https://github.com/syntax-tree/hast-util-find-and-replace)\n  — find and replace in hast\n* [`hast-util-select`](https://github.com/syntax-tree/hast-util-select)\n  — `querySelector`, `querySelectorAll`, and `matches`\n* [`unist-util-select`](https://github.com/syntax-tree/unist-util-select)\n  — select unist 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, organisation, or community you agree to\nabide by its terms.\n\n## License\n\n[MIT][license] © [Titus Wormer][author]\n\n<!-- Definition -->\n\n[build-badge]: https://github.com/syntax-tree/mdast-util-find-and-replace/workflows/main/badge.svg\n\n[build]: https://github.com/syntax-tree/mdast-util-find-and-replace/actions\n\n[coverage-badge]: https://img.shields.io/codecov/c/github/syntax-tree/mdast-util-find-and-replace.svg\n\n[coverage]: https://codecov.io/github/syntax-tree/mdast-util-find-and-replace\n\n[downloads-badge]: https://img.shields.io/npm/dm/mdast-util-find-and-replace.svg\n\n[downloads]: https://www.npmjs.com/package/mdast-util-find-and-replace\n\n[size-badge]: https://img.shields.io/badge/dynamic/json?label=minzipped%20size&query=$.size.compressedSize&url=https://deno.bundlejs.com/?q=mdast-util-find-and-replace\n\n[size]: https://bundlejs.com/?q=mdast-util-find-and-replace\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/main/contributing.md\n\n[support]: https://github.com/syntax-tree/.github/blob/main/support.md\n\n[coc]: https://github.com/syntax-tree/.github/blob/main/code-of-conduct.md\n\n[hast]: https://github.com/syntax-tree/hast\n\n[mdast]: https://github.com/syntax-tree/mdast\n\n[node]: https://github.com/syntax-tree/mdast#nodes\n\n[preorder]: https://github.com/syntax-tree/unist#preorder\n\n[text]: https://github.com/syntax-tree/mdast#text\n\n[xss]: https://en.wikipedia.org/wiki/Cross-site_scripting\n\n[test]: https://github.com/syntax-tree/unist-util-is#api\n\n[hast-util-find-and-replace]: https://github.com/syntax-tree/hast-util-find-and-replace\n\n[api-find-and-replace]: #findandreplacetree-list-options\n\n[api-options]: #options\n\n[api-find]: #find\n\n[api-replace]: #replace\n\n[api-replace-function]: #replacefunction\n\n[api-find-and-replace-list]: #findandreplacelist\n\n[api-find-and-replace-tuple]: #findandreplacetuple\n\n[api-regexp-match-object]: #regexpmatchobject\n","publish_time":1735907255988,"_cnpm_publish_time":1735907255988,"dist-tags":{"latest":"3.0.4"}}