hast utility to find and replace things.
- What is this?
- When should I use this?
- Install
- Use
- API
- Types
- Compatibility
- Security
- Related
- Contribute
- License
This package is a utility that lets you find patterns (string
, RegExp
) in
text and replace them with nodes (such as elements).
It’s aware of HTML (such as ignoring <style>
and <script>
by default).
This utility is typically useful when you have regexes and want to modify hast.
One example is when you have some form of “mentions” (such as
/@([a-z][_a-z0-9])\b/gi
) and want to create links to persons from them.
This package is ESM only. In Node.js (version 12.20+, 14.14+, or 16.0+), install with npm:
npm install hast-util-find-and-replace
In Deno with esm.sh
:
import {findAndReplace} from 'https://esm.sh/hast-util-find-and-replace@4'
In browsers with esm.sh
:
<script type="module">
import {findAndReplace} from 'https://esm.sh/hast-util-find-and-replace@4?bundle'
</script>
import {h} from 'hastscript'
import {findAndReplace} from 'hast-util-find-and-replace'
import {inspect} from 'unist-util-inspect'
const tree = h('p', [
'Some ',
h('em', 'emphasis'),
', ',
h('strong', 'importance'),
', and ',
h('code', 'code'),
'.'
])
findAndReplace(tree, [
[/and/gi, 'or'],
[/emphasis/gi, 'em'],
[/importance/gi, 'strong'],
[
/code/gi,
function ($0) {
return h('a', {href: '//example.com#' + $0}, $0)
}
]
])
console.log(inspect(tree))
Yields:
element<p>[9]
│ properties: {}
├─0 text "Some "
├─1 element<em>[1]
│ │ properties: {}
│ └─0 text "em"
├─2 text ", "
├─3 element<strong>[1]
│ │ properties: {}
│ └─0 text "strong"
├─4 text ", "
├─5 text "or"
├─6 text " "
├─7 element<code>[1]
│ │ properties: {}
│ └─0 element<a>[1]
│ │ properties: {"href":"//example.com#code"}
│ └─0 text "code"
└─8 text "."
This package exports the identifiers findAndReplace
and defaultIgnore
.
There is no default export.
Find patterns in a tree and replace them.
The algorithm searches the tree in preorder for complete values in
Text
nodes.
Partial matches are not supported.
findAndReplace(tree, find, replace[, options])
findAndReplace(tree, search[, options])
tree
(Node
)find
(string
orRegExp
) — value to find and remove (string
s are escaped and turned into a globalRegExp
)replace
(string
orFunction
) — value to insert.string
s are turned into aText
node,Function
s are called with the results of callingRegExp.exec
as arguments, and they can return aNode
, astring
(which is wrapped in aText
node), orfalse
to not replacesearch
(Array
orObject
) — perform multiple find-and-replaces. Either anArray
of tuples (Array
s) withfind
(at0
) andreplace
(at1
), or anObject
where each key isfind
and each value is the correspondingreplace
options.ignore
(Test
, default:['title', 'script', 'style', 'svg', 'math']
) — anyhast-util-is-element
compatible test (the default list is exported asdefaultIgnore
)
The given tree
(Node
).
This package is fully typed with TypeScript.
It exports the additional types Find
, Replace
, ReplaceFunction
,
FindAndReplaceTuple
, FindAndReplaceSchema
, FindAndReplaceList
,
RegExpMatchObject
, and Options
.
Projects maintained by the unified collective are compatible with all maintained versions of Node.js. As of now, that is Node.js 12.20+, 14.14+, and 16.0+. Our projects sometimes work with older versions, but this is not guaranteed.
Use of hast-util-find-and-replace
can open you up to a
cross-site scripting (XSS) attack if a value used to replace
is unsafe.
Use hast-util-santize
to make the hast tree safe.
The following example shows how a script is injected that runs when loaded in a browser.
const tree = h('p', 'This and that.')
findAndReplace(tree, 'and', () => h('script', 'alert(1)'))
Yields:
<p>This <script>alert(1)</script> that.</p>
hast-util-select
—querySelector
,querySelectorAll
, andmatches
mdast-util-find-and-replace
— find and replace in mdastunist-util-select
— select unist nodes with CSS-like selectors
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, organisation, or community you agree to abide by its terms.