8
8
[ ![ Backers] [ backers-badge ]] [ collective ]
9
9
[ ![ Chat] [ chat-badge ]] [ chat ]
10
10
11
- [ ** mdast** ] [ mdast ] utility to find and replace text in a [ * tree * ] [ tree ] .
11
+ [ mdast] [ ] utility to find and replace things .
12
12
13
- ## Install
13
+ ## Contents
14
+
15
+ * [ What is this?] ( #what-is-this )
16
+ * [ When should I use this?] ( #when-should-i-use-this )
17
+ * [ Install] ( #install )
18
+ * [ Use] ( #use )
19
+ * [ API] ( #api )
20
+ * [ ` findAndReplace(tree, find, replace[, options]) ` ] ( #findandreplacetree-find-replace-options )
21
+ * [ Types] ( #types )
22
+ * [ Compatibility] ( #compatibility )
23
+ * [ Security] ( #security )
24
+ * [ Related] ( #related )
25
+ * [ Contribute] ( #contribute )
26
+ * [ License] ( #license )
27
+
28
+ ## What is this?
29
+
30
+ This package is a utility that lets you find patterns (` string ` , ` RegExp ` ) in
31
+ text and replace them with nodes.
32
+
33
+ ## When should I use this?
34
+
35
+ This utility is typically useful when you have regexes and want to modify mdast.
36
+ One example is when you have some form of “mentions” (such as
37
+ ` /@([a-z][_a-z0-9])\b/gi ` ) and want to create links to persons from them.
38
+
39
+ A similar package, [ ` hast-util-find-and-replace ` ] [ hast-util-find-and-replace ]
40
+ does the same but on [ hast] [ ] .
14
41
15
- This package is [ ESM only] ( https://gist.github.com/sindresorhus/a39789f98801d908bbc7ff3ecc99d99c ) :
16
- Node 12+ is needed to use it and it must be ` import ` ed instead of ` require ` d.
42
+ ## Install
17
43
18
- [ npm] [ ] :
44
+ This package is [ ESM only] [ esm ] .
45
+ In Node.js (version 12.20+, 14.14+, or 16.0+), install with [ npm] [ ] :
19
46
20
47
``` sh
21
48
npm install mdast-util-find-and-replace
22
49
```
23
50
51
+ In Deno with [ ` esm.sh ` ] [ esmsh ] :
52
+
53
+ ``` js
54
+ import {findAndReplace } from ' https://esm.sh/mdast-util-find-and-replace@4'
55
+ ```
56
+
57
+ In browsers with [ ` esm.sh ` ] [ esmsh ] :
58
+
59
+ ``` html
60
+ <script type =" module" >
61
+ import {findAndReplace } from ' https://esm.sh/mdast-util-find-and-replace@4?bundle'
62
+ </script >
63
+ ```
64
+
24
65
## Use
25
66
26
67
``` js
@@ -36,15 +77,17 @@ const tree = u('paragraph', [
36
77
u (' text' , ' .' )
37
78
])
38
79
39
- findAndReplace (tree, ' and' , ' or' )
40
-
41
- findAndReplace (tree, {emphasis: ' em' , importance: ' strong' })
42
-
43
- findAndReplace (tree, {
44
- Some : function ($0 ) {
45
- return u (' link' , {url: ' //example.com#' + $0}, [u (' text' , $0)])
46
- }
47
- })
80
+ findAndReplace (tree, [
81
+ [/ and/ gi , ' or' ],
82
+ [/ emphasis/ gi , ' em' ],
83
+ [/ importance/ gi , ' strong' ],
84
+ [
85
+ / Some/ g ,
86
+ function ($0 ) {
87
+ return u (' link' , {url: ' //example.com#' + $0}, [u (' text' , $0)])
88
+ }
89
+ ]
90
+ ])
48
91
49
92
console .log (inspect (tree))
50
93
```
@@ -53,83 +96,97 @@ Yields:
53
96
54
97
``` txt
55
98
paragraph[8]
56
- ├─ link[1] [url="//example.com#Some"]
57
- │ └─ text: "Some"
58
- ├─ text: " "
59
- ├─ emphasis[1]
60
- │ └─ text: "em"
61
- ├─ text: " "
62
- ├─ text: "or"
63
- ├─ text: " "
64
- ├─ strong[1]
65
- │ └─ text: "strong"
66
- └─ text: "."
99
+ ├─0 link[1]
100
+ │ │ url: "//example.com#Some"
101
+ │ └─0 text "Some"
102
+ ├─1 text " "
103
+ ├─2 emphasis[1]
104
+ │ └─0 text "em"
105
+ ├─3 text " "
106
+ ├─4 text "or"
107
+ ├─5 text " "
108
+ ├─6 strong[1]
109
+ │ └─0 text "strong"
110
+ └─7 text "."
67
111
```
68
112
69
113
## API
70
114
71
- This package exports the following identifiers: ` findAndReplace ` .
115
+ This package exports the identifier ` findAndReplace ` .
72
116
There is no default export.
73
117
74
- ### ` findAndReplace(tree, find[ , replace] [, options]) `
118
+ ### ` findAndReplace(tree, find, replace[, options]) `
75
119
76
- Find and replace text in [ ** mdast ** ] [ mdast ] [ * tree* ] [ tree ] s .
77
- The algorithm searches the tree in [ * preorder* ] [ preorder ] for complete values
78
- in [ ` Text ` ] [ text ] nodes.
120
+ Find patterns in a tree and replace them .
121
+ The algorithm searches the tree in * [ preorder] [ ] * for complete values in
122
+ [ ` Text ` ] [ text ] nodes.
79
123
Partial matches are not supported.
80
124
81
125
###### Signatures
82
126
83
- * ` findAndReplace(tree, find, replace? [, options]) `
127
+ * ` findAndReplace(tree, find, replace[, options]) `
84
128
* ` findAndReplace(tree, search[, options]) `
85
129
86
130
###### Parameters
87
131
88
132
* ` tree ` ([ ` Node ` ] [ node ] )
89
- — [ ** mdast** ] [ mdast ] [ * tree* ] [ tree ]
90
133
* ` find ` (` string ` or ` RegExp ` )
91
- — Value to find and remove.
92
- When ` string ` , escaped and made into a global ` RegExp `
134
+ — value to find and remove ( ` string ` s are escaped and turned into a global
135
+ ` RegExp ` )
93
136
* ` replace ` (` string ` or ` Function ` )
94
- — Value to insert.
95
- When ` string ` , turned into a [ ` Text ` ] [ text ] node.
96
- When ` Function ` , invoked with the results of calling ` RegExp.exec ` as
97
- arguments, in which case it can return a single or a list of [ ` Node ` ] [ node ] ,
98
- a ` string ` (which is wrapped in a [ ` Text ` ] [ text ] node), or ` false ` to not
99
- replace
100
- * ` search ` (` Object ` or ` Array ` )
101
- — Perform multiple find-and-replaces.
102
- When ` Array ` , each entry is a tuple (` Array ` ) of a ` find ` (at ` 0 ` ) and
103
- ` replace ` (at ` 1 ` ).
104
- When ` Object ` , each key is a ` find ` (in string form) and each value is a
105
- ` replace `
137
+ — value to insert.
138
+ ` string ` s are turned into a [ ` Text ` ] [ text ] node,
139
+ ` Function ` s are called with the results of calling ` RegExp.exec ` as
140
+ arguments, and they can return a [ ` Node ` ] [ node ] , a ` string ` (which is
141
+ wrapped in a [ ` Text ` ] [ text ] node), or ` false ` to not replace
142
+ * ` search ` (` Array ` or ` Object ` )
143
+ — perform multiple find-and-replaces.
144
+ Either an ` Array ` of tuples (` Array ` s) with ` find ` (at ` 0 ` ) and ` replace `
145
+ (at ` 1 ` ), or an ` Object ` where each key is ` find ` and each value is
146
+ the corresponding ` replace `
106
147
* ` options.ignore ` (` Test ` , default: ` [] ` )
107
148
— Any [ ` unist-util-is ` ] [ test ] compatible test.
108
149
109
150
###### Returns
110
151
111
- The given, modified, ` tree ` .
152
+ The given ` tree ` ([ ` Node ` ] [ node ] ).
153
+
154
+ ## Types
155
+
156
+ This package is fully typed with [ TypeScript] [ ] .
157
+ It exports the types ` Find ` , ` Replace ` , ` ReplaceFunction ` ,
158
+ ` FindAndReplaceTuple ` , ` FindAndReplaceSchema ` , ` FindAndReplaceList ` ,
159
+ ` RegExpMatchObject ` , and ` Options ` .
160
+
161
+ ## Compatibility
162
+
163
+ Projects maintained by the unified collective are compatible with all maintained
164
+ versions of Node.js.
165
+ As of now, that is Node.js 12.20+, 14.14+, and 16.0+.
166
+ Our projects sometimes work with older versions, but this is not guaranteed.
112
167
113
168
## Security
114
169
115
- Use of ` mdast-util-find-and-replace ` does not involve [ ** hast** ] [ hast ] or user
116
- content so there are no openings for [ cross-site scripting (XSS)] [ xss ] attacks.
170
+ Use of ` mdast-util-find-and-replace ` does not involve [ hast] [ ] or user content
171
+ so there are no openings for [ cross-site scripting (XSS)] [ xss ] attacks.
117
172
118
173
## Related
119
174
120
175
* [ ` hast-util-find-and-replace ` ] ( https://github.com/syntax-tree/hast-util-find-and-replace )
121
- — hast utility to find and replace text
176
+ — find and replace in hast
177
+ * [ ` hast-util-select ` ] ( https://github.com/syntax-tree/hast-util-select )
178
+ — ` querySelector ` , ` querySelectorAll ` , and ` matches `
122
179
* [ ` unist-util-select ` ] ( https://github.com/syntax-tree/unist-util-select )
123
180
— select unist nodes with CSS-like selectors
124
181
125
182
## Contribute
126
183
127
- See [ ` contributing.md ` in ` syntax-tree/.github ` ] [ contributing ] for ways to get
128
- started.
184
+ See [ ` contributing.md ` ] [ contributing ] in [ ` syntax-tree/.github ` ] [ health ] for
185
+ ways to get started.
129
186
See [ ` support.md ` ] [ support ] for ways to get help.
130
187
131
188
This project has a [ code of conduct] [ coc ] .
132
- By interacting with this repository, organization , or community you agree to
189
+ By interacting with this repository, organisation , or community you agree to
133
190
abide by its terms.
134
191
135
192
## License
@@ -166,28 +223,36 @@ abide by its terms.
166
223
167
224
[ npm ] : https://docs.npmjs.com/cli/install
168
225
226
+ [ esm ] : https://gist.github.com/sindresorhus/a39789f98801d908bbc7ff3ecc99d99c
227
+
228
+ [ esmsh ] : https://esm.sh
229
+
230
+ [ typescript ] : https://www.typescriptlang.org
231
+
169
232
[ license ] : license
170
233
171
234
[ author ] : https://wooorm.com
172
235
173
- [ contributing ] : https://github.com/syntax-tree/.github/blob/HEAD/contributing.md
236
+ [ health ] : https://github.com/syntax-tree/.github
174
237
175
- [ support ] : https://github.com/syntax-tree/.github/blob/HEAD/support .md
238
+ [ contributing ] : https://github.com/syntax-tree/.github/blob/main/contributing .md
176
239
177
- [ coc ] : https://github.com/syntax-tree/.github/blob/HEAD/code-of-conduct.md
240
+ [ support ] : https://github.com/syntax-tree/.github/blob/main/support.md
241
+
242
+ [ coc ] : https://github.com/syntax-tree/.github/blob/main/code-of-conduct.md
178
243
179
244
[ hast ] : https://github.com/syntax-tree/hast
180
245
181
246
[ mdast ] : https://github.com/syntax-tree/mdast
182
247
183
248
[ node ] : https://github.com/syntax-tree/mdast#ndoes
184
249
185
- [ tree ] : https://github.com/syntax-tree/unist#tree
186
-
187
250
[ preorder ] : https://github.com/syntax-tree/unist#preorder
188
251
189
252
[ text ] : https://github.com/syntax-tree/mdast#text
190
253
191
254
[ xss ] : https://en.wikipedia.org/wiki/Cross-site_scripting
192
255
193
256
[ test ] : https://github.com/syntax-tree/unist-util-is#api
257
+
258
+ [ hast-util-find-and-replace ] : https://github.com/syntax-tree/hast-util-find-and-replace
0 commit comments