Package Exports
- mdast-util-find-and-replace
This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (mdast-util-find-and-replace) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.
Readme
mdast-util-find-and-replace
mdast utility to find and replace text in a tree.
Install
This package is ESM only:
Node 12+ is needed to use it and it must be import
ed instead of require
d.
npm:
npm install mdast-util-find-and-replace
Use
import {u} from 'unist-builder'
import {inspect} from 'unist-util-inspect'
import {findAndReplace} from 'mdast-util-find-and-replace'
const tree = u('paragraph', [
u('text', 'Some '),
u('emphasis', [u('text', 'emphasis')]),
u('text', ' and '),
u('strong', [u('text', 'importance')]),
u('text', '.')
])
findAndReplace(tree, 'and', 'or')
findAndReplace(tree, {emphasis: 'em', importance: 'strong'})
findAndReplace(tree, {
Some: function ($0) {
return u('link', {url: '//example.com#' + $0}, [u('text', $0)])
}
})
console.log(inspect(tree))
Yields:
paragraph[8]
├─ link[1] [url="//example.com#Some"]
│ └─ text: "Some"
├─ text: " "
├─ emphasis[1]
│ └─ text: "em"
├─ text: " "
├─ text: "or"
├─ text: " "
├─ strong[1]
│ └─ text: "strong"
└─ text: "."
API
This package exports the following identifiers: findAndReplace
.
There is no default export.
findAndReplace(tree, find[, replace][, options])
Find and replace text in mdast trees.
The algorithm searches the tree in preorder for complete values
in Text
nodes.
Partial matches are not supported.
Signatures
findAndReplace(tree, find, replace?[, options])
findAndReplace(tree, search[, options])
Parameters
tree
(Node
) — mdast treefind
(string
orRegExp
) — Value to find and remove. Whenstring
, escaped and made into a globalRegExp
replace
(string
orFunction
) — Value to insert. Whenstring
, turned into aText
node. WhenFunction
, invoked with the results of callingRegExp.exec
as arguments, in which case it can return a single or a list ofNode
, astring
(which is wrapped in aText
node), orfalse
to not replacesearch
(Object
orArray
) — Perform multiple find-and-replaces. WhenArray
, each entry is a tuple (Array
) of afind
(at0
) andreplace
(at1
). WhenObject
, each key is afind
(in string form) and each value is areplace
options.ignore
(Test
, default:[]
) — Anyunist-util-is
compatible test.
Returns
The given, modified, tree
.
Security
Use of mdast-util-find-and-replace
does not involve hast or user
content so there are no openings for cross-site scripting (XSS) attacks.
Related
hast-util-find-and-replace
— hast utility to find and replace textunist-util-select
— select unist nodes with CSS-like selectors
Contribute
See contributing.md
in syntax-tree/.github
for ways to get
started.
See support.md
for ways to get help.
This project has a code of conduct. By interacting with this repository, organization, or community you agree to abide by its terms.