18
18
* [ Use] ( #use )
19
19
* [ API] ( #api )
20
20
* [ ` findAndReplace(tree, find, replace[, options]) ` ] ( #findandreplacetree-find-replace-options )
21
+ * [ ` Find ` ] ( #find )
22
+ * [ ` FindAndReplaceList ` ] ( #findandreplacelist )
23
+ * [ ` FindAndReplaceSchema ` ] ( #findandreplaceschema )
24
+ * [ ` FindAndReplaceTuple ` ] ( #findandreplacetuple )
25
+ * [ ` Options ` ] ( #options )
26
+ * [ ` RegExpMatchObject ` ] ( #regexpmatchobject )
27
+ * [ ` Replace ` ] ( #replace )
28
+ * [ ` ReplaceFunction ` ] ( #replacefunction )
21
29
* [ Types] ( #types )
22
30
* [ Compatibility] ( #compatibility )
23
31
* [ Security] ( #security )
@@ -42,7 +50,7 @@ does the same but on [hast][].
42
50
## Install
43
51
44
52
This package is [ ESM only] [ esm ] .
45
- In Node.js (version 12.20+, 14.14+, or 16.0+), install with [ npm] [ ] :
53
+ In Node.js (version 14.14+ and or 16.0+), install with [ npm] [ ] :
46
54
47
55
``` sh
48
56
npm install mdast-util-find-and-replace
@@ -112,12 +120,13 @@ paragraph[8]
112
120
113
121
## API
114
122
115
- This package exports the identifier ` findAndReplace ` .
123
+ This package exports the identifier [ ` findAndReplace ` ] [ api-findandreplace ] .
116
124
There is no default export.
117
125
118
126
### ` findAndReplace(tree, find, replace[, options]) `
119
127
120
128
Find patterns in a tree and replace them.
129
+
121
130
The algorithm searches the tree in * [ preorder] [ ] * for complete values in
122
131
[ ` Text ` ] [ text ] nodes.
123
132
Partial matches are not supported.
@@ -130,39 +139,144 @@ Partial matches are not supported.
130
139
###### Parameters
131
140
132
141
* ` tree ` ([ ` Node ` ] [ node ] )
133
- * ` find ` (` string ` or ` RegExp ` )
134
- — value to find and remove (` string ` s are escaped and turned into a global
135
- ` RegExp ` )
136
- * ` replace ` (` string ` or ` Function ` )
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 `
147
- * ` options.ignore ` (` Test ` , default: ` [] ` )
148
- — any [ ` unist-util-is ` ] [ test ] compatible test.
142
+ — tree to change
143
+ * ` find ` ([ ` Find ` ] [ api-find ] )
144
+ — value to find and remove
145
+ * ` replace ` ([ ` Replace ` ] [ api-replace ] )
146
+ — thing to replace with
147
+ * ` search ` ([ ` FindAndReplaceSchema ` ] [ api-findandreplaceschema ] or
148
+ [ ` FindAndReplaceList ` ] [ api-findandreplacelist ] )
149
+ — several find and replaces
150
+ * ` options ` ([ ` Options ` ] [ api-options ] )
151
+ — configuration
152
+
153
+ ###### Returns
154
+
155
+ Given, modified, tree ([ ` Node ` ] [ node ] ).
156
+
157
+ ### ` Find `
158
+
159
+ Pattern to find (TypeScript type).
160
+
161
+ Strings are escaped and then turned into global expressions.
162
+
163
+ ###### Type
164
+
165
+ ``` ts
166
+ type Find = string | RegExp
167
+ ` ` `
168
+
169
+ ### ` FindAndReplaceList `
170
+
171
+ Several find and replaces, in array form (TypeScript type).
172
+
173
+ ###### Type
174
+
175
+ ` ` ` ts
176
+ type FindAndReplaceList = Array <FindAndReplaceTuple >
177
+ ` ` `
178
+
179
+ See [ ` FindAndReplaceTuple ` ][api-findandreplacetuple].
180
+
181
+ ### ` FindAndReplaceSchema `
182
+
183
+ Several find and replaces, in object form (TypeScript type).
184
+
185
+ ###### Type
186
+
187
+ ` ` ` ts
188
+ type FindAndReplaceSchema = Record <string , Replace >
189
+ ` ` `
190
+
191
+ See [ ` Replace ` ][api-replace].
192
+
193
+ ### ` FindAndReplaceTuple `
194
+
195
+ Find and replace in tuple form (TypeScript type).
196
+
197
+ ###### Type
198
+
199
+ ` ` ` ts
200
+ type FindAndReplaceTuple = [Find , Replace ]
201
+ ` ` `
202
+
203
+ See [ ` Find ` ][api-find] and [ ` Replace ` ][api-replace].
204
+
205
+ ### ` Options `
206
+
207
+ Configuration (TypeScript type).
208
+
209
+ ###### Fields
210
+
211
+ * ` ignore ` ([ ` Test ` ][test], optional)
212
+ — test for which elements to ignore
213
+
214
+ ### ` RegExpMatchObject `
215
+
216
+ Info on the match (TypeScript type).
217
+
218
+ ###### Fields
219
+
220
+ * ` index ` ( ` number ` )
221
+ — the index of the search at which the result was found
222
+ * ` input ` ( ` string ` )
223
+ — a copy of the search string in the text node
224
+ * ` stack ` ([ ` Array <Node >` ][node])
225
+ — all ancestors of the text node, where the last node is the text itself
226
+
227
+ ### ` Replace `
228
+
229
+ Thing to replace with (TypeScript type).
230
+
231
+ ###### Type
232
+
233
+ ` ` ` ts
234
+ type Replace = string | ReplaceFunction
235
+ ` ` `
236
+
237
+ See [ ` ReplaceFunction ` ][api-replacefunction].
238
+
239
+ ### ` ReplaceFunction `
240
+
241
+ Callback called when a search matches (TypeScript type).
242
+
243
+ ###### Parameters
244
+
245
+ The parameters are the result of corresponding search expression:
246
+
247
+ * ` value ` ( ` string ` )
248
+ — whole match
249
+ * ` ...capture ` ( ` Array <string >` )
250
+ — matches from regex capture groups
251
+ * ` match ` ([ ` RegExpMatchObject ` ][api-regexpmatchobject])
252
+ — info on the match
149
253
150
254
###### Returns
151
255
152
- The given ` tree ` ([ ` Node ` ] [ node ] ).
256
+ Thing to replace with:
257
+
258
+ * when ` null ` , ` undefined ` , ` ' ' ` , remove the match
259
+ * …or when ` false ` , do not replace at all
260
+ * …or when ` string ` , replace with a text node of that value
261
+ * …or when ` Node ` or ` Array <Node >` , replace with those nodes
153
262
154
263
## Types
155
264
156
265
This package is fully typed with [TypeScript][].
157
- It exports the types ` Find ` , ` Replace ` , ` ReplaceFunction ` ,
158
- ` FindAndReplaceTuple ` , ` FindAndReplaceSchema ` , ` FindAndReplaceList ` ,
159
- ` RegExpMatchObject ` , and ` Options ` .
266
+ It exports the additional types [ ` Find ` ][api-find],
267
+ [ ` FindAndReplaceList ` ][api-findandreplacelist],
268
+ [ ` FindAndReplaceSchema ` ][api-findandreplaceschema],
269
+ [ ` FindAndReplaceTuple ` ][api-findandreplacetuple],
270
+ [ ` Options ` ][api-options],
271
+ [ ` RegExpMatchObject ` ][api-regexpmatchobject],
272
+ [ ` Replace ` ][api-replace], and
273
+ [ ` ReplaceFunction ` ][api-replacefunction].
160
274
161
275
## Compatibility
162
276
163
277
Projects maintained by the unified collective are compatible with all maintained
164
278
versions of Node.js.
165
- As of now, that is Node.js 12.20+, 14.14+, and 16.0+.
279
+ As of now, that is Node.js 14.14+ and 16.0+.
166
280
Our projects sometimes work with older versions, but this is not guaranteed.
167
281
168
282
## Security
@@ -245,7 +359,7 @@ abide by its terms.
245
359
246
360
[mdast]: https://github.com/syntax-tree/mdast
247
361
248
- [ node ] : https://github.com/syntax-tree/mdast#ndoes
362
+ [node]: https://github.com/syntax-tree/mdast#nodes
249
363
250
364
[preorder]: https://github.com/syntax-tree/unist#preorder
251
365
@@ -256,3 +370,21 @@ abide by its terms.
256
370
[test]: https://github.com/syntax-tree/unist-util-is#api
257
371
258
372
[hast-util-find-and-replace]: https://github.com/syntax-tree/hast-util-find-and-replace
373
+
374
+ [api-findandreplace]: #findandreplacetree-find-replace-options
375
+
376
+ [api-options]: #options
377
+
378
+ [api-find]: #find
379
+
380
+ [api-replace]: #replace
381
+
382
+ [api-replacefunction]: #replacefunction
383
+
384
+ [api-findandreplacelist]: #findandreplacelist
385
+
386
+ [api-findandreplaceschema]: #findandreplaceschema
387
+
388
+ [api-findandreplacetuple]: #findandreplacetuple
389
+
390
+ [api-regexpmatchobject]: #regexpmatchobject
0 commit comments