Gathering detailed insights and metrics for mdast-util-find-and-replace
Gathering detailed insights and metrics for mdast-util-find-and-replace
Gathering detailed insights and metrics for mdast-util-find-and-replace
Gathering detailed insights and metrics for mdast-util-find-and-replace
mdast-util-definitions
mdast utility to find definition nodes in a tree
mdast-util-to-string
mdast utility to get the plain text content of a node
mdast-util-gfm
mdast extension to parse and serialize GFM (GitHub Flavored Markdown)
mdast-util-from-markdown
mdast utility to parse markdown
mdast utility to find and replace text in a tree
npm install mdast-util-find-and-replace
Module System
Min. Node Version
Typescript Support
Node Version
NPM Version
11 Stars
63 Commits
4 Forks
9 Watching
1 Branches
12 Contributors
Updated on 03 Jun 2024
Minified
Minified + Gzipped
JavaScript (100%)
Cumulative downloads
Total Downloads
Last day
-0.4%
817,151
Compared to previous day
Last week
5.5%
4,318,775
Compared to previous week
Last month
16.5%
17,694,232
Compared to previous month
Last year
93.5%
168,042,707
Compared to previous year
mdast utility to find and replace things.
This package is a utility that lets you find patterns (string
, RegExp
) in
text and replace them with nodes.
This utility is typically useful when you have regexes and want to modify mdast.
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.
A similar package, hast-util-find-and-replace
does the same but on hast.
This package is ESM only. In Node.js (version 16+), install with npm:
1npm install mdast-util-find-and-replace
In Deno with esm.sh
:
1import {findAndReplace} from 'https://esm.sh/mdast-util-find-and-replace@3'
In browsers with esm.sh
:
1<script type="module"> 2 import {findAndReplace} from 'https://esm.sh/mdast-util-find-and-replace@3?bundle' 3</script>
1import {findAndReplace} from 'mdast-util-find-and-replace' 2import {u} from 'unist-builder' 3import {inspect} from 'unist-util-inspect' 4 5const tree = u('paragraph', [ 6 u('text', 'Some '), 7 u('emphasis', [u('text', 'emphasis')]), 8 u('text', ' and '), 9 u('strong', [u('text', 'importance')]), 10 u('text', '.') 11]) 12 13findAndReplace(tree, [ 14 [/and/gi, 'or'], 15 [/emphasis/gi, 'em'], 16 [/importance/gi, 'strong'], 17 [ 18 /Some/g, 19 function ($0) { 20 return u('link', {url: '//example.com#' + $0}, [u('text', $0)]) 21 } 22 ] 23]) 24 25console.log(inspect(tree))
Yields:
1paragraph[8] 2├─0 link[1] 3│ │ url: "//example.com#Some" 4│ └─0 text "Some" 5├─1 text " " 6├─2 emphasis[1] 7│ └─0 text "em" 8├─3 text " " 9├─4 text "or" 10├─5 text " " 11├─6 strong[1] 12│ └─0 text "strong" 13└─7 text "."
This package exports the identifier findAndReplace
.
There is no default export.
findAndReplace(tree, list[, options])
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.
tree
(Node
)
— tree to changelist
(FindAndReplaceList
or
FindAndReplaceTuple
)
— one or more find-and-replace pairsoptions
(Options
)
— configurationNothing (undefined
).
Find
Pattern to find (TypeScript type).
Strings are escaped and then turned into global expressions.
1type Find = RegExp | string
FindAndReplaceList
Several find and replaces, in array form (TypeScript type).
1type FindAndReplaceList = Array<FindAndReplaceTuple>
See FindAndReplaceTuple
.
FindAndReplaceTuple
Find and replace in tuple form (TypeScript type).
1type FindAndReplaceTuple = [Find, Replace?]
Options
Configuration (TypeScript type).
ignore
(Test
, optional)
— test for which elements to ignoreRegExpMatchObject
Info on the match (TypeScript type).
index
(number
)
— the index of the search at which the result was foundinput
(string
)
— a copy of the search string in the text nodestack
(Array<Node>
)
— all ancestors of the text node, where the last node is the text itselfReplace
Thing to replace with (TypeScript type).
1type Replace = ReplaceFunction | string
See ReplaceFunction
.
ReplaceFunction
Callback called when a search matches (TypeScript type).
The parameters are the result of corresponding search expression:
value
(string
)
— whole match...capture
(Array<string>
)
— matches from regex capture groupsmatch
(RegExpMatchObject
)
— info on the matchThing to replace with:
null
, undefined
, ''
, remove the matchfalse
, do not replace at allstring
, replace with a text node of that valueNode
or Array<Node>
, replace with those nodesThis package is fully typed with TypeScript.
It exports the additional types Find
,
FindAndReplaceList
,
FindAndReplaceTuple
,
Options
,
RegExpMatchObject
,
Replace
, and
ReplaceFunction
.
Projects maintained by the unified collective are compatible with maintained versions of Node.js.
When we cut a new major release, we drop support for unmaintained versions of
Node.
This means we try to keep the current release line,
mdast-util-find-and-replace@^3
, compatible with Node.js 16.
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.
hast-util-find-and-replace
— find and replace in hasthast-util-select
— querySelector
, querySelectorAll
, and matches
unist-util-select
— select unist nodes with CSS-like selectorsSee 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.
No vulnerabilities found.
Reason
no dangerous workflow patterns detected
Reason
no binaries found in the repo
Reason
0 existing vulnerabilities detected
Reason
license file detected
Details
Reason
security policy file detected
Details
Reason
0 commit(s) and 1 issue activity found in the last 90 days -- score normalized to 0
Reason
Found 1/30 approved changesets -- score normalized to 0
Reason
dependency not pinned by hash detected -- score normalized to 0
Details
Reason
detected GitHub workflow tokens with excessive permissions
Details
Reason
no effort to earn an OpenSSF best practices badge detected
Reason
project is not fuzzed
Details
Reason
branch protection not enabled on development/release branches
Details
Reason
SAST tool is not run on all commits -- score normalized to 0
Details
Score
Last Scanned on 2024-11-18
The Open Source Security Foundation is a cross-industry collaboration to improve the security of open source software (OSS). The Scorecard provides security health metrics for open source projects.
Learn More