18
18
* [ Use] ( #use )
19
19
* [ API] ( #api )
20
20
* [ ` findAndReplace(tree, find, replace[, options]) ` ] ( #findandreplacetree-find-replace-options )
21
+ * [ ` defaultIgnore ` ] ( #defaultignore )
22
+ * [ ` Find ` ] ( #find )
23
+ * [ ` FindAndReplaceList ` ] ( #findandreplacelist )
24
+ * [ ` FindAndReplaceSchema ` ] ( #findandreplaceschema )
25
+ * [ ` FindAndReplaceTuple ` ] ( #findandreplacetuple )
26
+ * [ ` Options ` ] ( #options )
27
+ * [ ` RegExpMatchObject ` ] ( #regexpmatchobject )
28
+ * [ ` Replace ` ] ( #replace )
29
+ * [ ` ReplaceFunction ` ] ( #replacefunction )
21
30
* [ Types] ( #types )
22
31
* [ Compatibility] ( #compatibility )
23
32
* [ Security] ( #security )
@@ -40,7 +49,7 @@ One example is when you have some form of “mentions” (such as
40
49
## Install
41
50
42
51
This package is [ ESM only] [ esm ] .
43
- In Node.js (version 12.20+, 14.14+, or 16.0+), install with [ npm] [ ] :
52
+ In Node.js (version 14.14+ and or 16.0+), install with [ npm] [ ] :
44
53
45
54
``` sh
46
55
npm install hast-util-find-and-replace
@@ -118,12 +127,14 @@ element<p>[9]
118
127
119
128
## API
120
129
121
- This package exports the identifiers ` findAndReplace ` and ` defaultIgnore ` .
130
+ This package exports the identifiers [ ` defaultIgnore ` ] [ defaultignore ] and
131
+ [ ` findAndReplace ` ] [ findandreplace ] .
122
132
There is no default export.
123
133
124
134
### ` findAndReplace(tree, find, replace[, options]) `
125
135
126
136
Find patterns in a tree and replace them.
137
+
127
138
The algorithm searches the tree in * [ preorder] [ ] * for complete values in
128
139
[ ` Text ` ] [ text ] nodes.
129
140
Partial matches are not supported.
@@ -136,41 +147,156 @@ Partial matches are not supported.
136
147
###### Parameters
137
148
138
149
* ` tree ` ([ ` Node ` ] [ node ] )
139
- * ` find ` (` string ` or ` RegExp ` )
140
- — value to find and remove (` string ` s are escaped and turned into a global
141
- ` RegExp ` )
142
- * ` replace ` (` string ` or ` Function ` )
143
- — value to insert.
144
- ` string ` s are turned into a [ ` Text ` ] [ text ] node,
145
- ` Function ` s are called with the results of calling ` RegExp.exec ` as
146
- arguments, and they can return a [ ` Node ` ] [ node ] , a ` string ` (which is
147
- wrapped in a [ ` Text ` ] [ text ] node), or ` false ` to not replace
148
- * ` search ` (` Array ` or ` Object ` )
149
- — perform multiple find-and-replaces.
150
- Either an ` Array ` of tuples (` Array ` s) with ` find ` (at ` 0 ` ) and ` replace `
151
- (at ` 1 ` ), or an ` Object ` where each key is ` find ` and each value is
152
- the corresponding ` replace `
153
- * ` options.ignore ` (` Test ` , default: `[ 'title', 'script', 'style', 'svg',
154
- 'math'] `)
155
- — any [ ` hast-util-is-element ` ] [ test ] compatible test (the default list is
156
- exported as ` defaultIgnore ` )
150
+ — tree to change
151
+ * ` find ` ([ ` Find ` ] [ find ] )
152
+ — value to find and remove
153
+ * ` replace ` ([ ` Replace ` ] [ replace ] )
154
+ — thing to replace with
155
+ * ` search ` ([ ` FindAndReplaceSchema ` ] [ findandreplaceschema ] or
156
+ [ ` FindAndReplaceList ` ] [ findandreplacelist ] )
157
+ — several find and replaces
158
+ * ` options ` ([ ` Options ` ] [ options ] )
159
+ — configuration
157
160
158
161
###### Returns
159
162
160
- The given ` tree ` ([ ` Node ` ] [ node ] ).
163
+ Given, modified, tree ([ ` Node ` ] [ node ] ).
164
+
165
+ ### ` defaultIgnore `
166
+
167
+ Default tag names to ignore.
168
+
169
+ The defaults are ` math ` , ` script ` , ` style ` , ` svg ` , and ` title ` .
170
+
171
+ ###### Type
172
+
173
+ ``` ts
174
+ type defaultIgnore = Array <string >
175
+ ` ` `
176
+
177
+ ### ` Find `
178
+
179
+ Pattern to find (TypeScript type).
180
+
181
+ Strings are escaped and then turned into global expressions.
182
+
183
+ ###### Type
184
+
185
+ ` ` ` ts
186
+ type Find = string | RegExp
187
+ ` ` `
188
+
189
+ ### ` FindAndReplaceList `
190
+
191
+ Several find and replaces, in array form (TypeScript type).
192
+
193
+ ###### Type
194
+
195
+ ` ` ` ts
196
+ type FindAndReplaceList = Array <FindAndReplaceTuple >
197
+ ` ` `
198
+
199
+ See [ ` FindAndReplaceTuple ` ][findandreplacetuple].
200
+
201
+ ### ` FindAndReplaceSchema `
202
+
203
+ Several find and replaces, in object form (TypeScript type).
204
+
205
+ ###### Type
206
+
207
+ ` ` ` ts
208
+ type FindAndReplaceSchema = Record <string , Replace >
209
+ ` ` `
210
+
211
+ See [ ` Replace ` ][replace].
212
+
213
+ ### ` FindAndReplaceTuple `
214
+
215
+ Find and replace in tuple form (TypeScript type).
216
+
217
+ ###### Type
218
+
219
+ ` ` ` ts
220
+ type FindAndReplaceTuple = [Find , Replace ]
221
+ ` ` `
222
+
223
+ See [ ` Find ` ][find] and [ ` Replace ` ][replace].
224
+
225
+ ### ` Options `
226
+
227
+ Configuration (TypeScript type).
228
+
229
+ ###### Fields
230
+
231
+ * ` ignore ` ([ ` Test ` ][test], optional)
232
+ — test for which elements to ignore
233
+
234
+ ### ` RegExpMatchObject `
235
+
236
+ Info on the match (TypeScript type).
237
+
238
+ ###### Fields
239
+
240
+ * ` index ` ( ` number ` )
241
+ — the index of the search at which the result was found
242
+ * ` input ` ( ` string ` )
243
+ — a copy of the search string in the text node
244
+ * ` stack ` ([ ` Array <Node >` ][node])
245
+ — all ancestors of the text node, where the last node is the text itself
246
+
247
+ ### ` Replace `
248
+
249
+ Thing to replace with (TypeScript type).
250
+
251
+ ###### Type
252
+
253
+ ` ` ` ts
254
+ type Replace = string | ReplaceFunction
255
+ ` ` `
256
+
257
+ See [ ` ReplaceFunction ` ][replacefunction].
258
+
259
+ ### ` ReplaceFunction `
260
+
261
+ Callback called when a search matches (TypeScript type).
262
+
263
+ ###### Parameters
264
+
265
+ The parameters are the result of corresponding search expression:
266
+
267
+ * ` value ` ( ` string ` )
268
+ — whole match
269
+ * ` ...capture ` ( ` Array <string >` )
270
+ — matches from regex capture groups
271
+ * ` match ` ([ ` RegExpMatchObject ` ][regexpmatchobject])
272
+ — info on the match
273
+
274
+ ###### Returns
275
+
276
+ Thing to replace with:
277
+
278
+ * when ` null ` , ` undefined ` , ` ' ' ` , remove the match
279
+ * …or when ` false ` , do not replace at all
280
+ * …or when ` string ` , replace with a text node of that value
281
+ * …or when ` Node ` or ` Array <Node >` , replace with those nodes
161
282
162
283
## Types
163
284
164
285
This package is fully typed with [TypeScript][].
165
- It exports the additional types ` Find ` , ` Replace ` , ` ReplaceFunction ` ,
166
- ` FindAndReplaceTuple ` , ` FindAndReplaceSchema ` , ` FindAndReplaceList ` ,
167
- ` RegExpMatchObject ` , and ` Options ` .
286
+ It exports the additional types [ ` Find ` ][find],
287
+ [ ` FindAndReplaceList ` ][findandreplacelist],
288
+ [ ` FindAndReplaceSchema ` ][findandreplaceschema],
289
+ [ ` FindAndReplaceTuple ` ][findandreplacetuple],
290
+ [ ` Options ` ][options],
291
+ [ ` RegExpMatchObject ` ][regexpmatchobject],
292
+ [ ` Replace ` ][replace], and
293
+ [ ` ReplaceFunction ` ][replacefunction].
168
294
169
295
## Compatibility
170
296
171
297
Projects maintained by the unified collective are compatible with all maintained
172
298
versions of Node.js.
173
- As of now, that is Node.js 12.20+, 14.14+, and 16.0+.
299
+ As of now, that is Node.js 14.14+ and 16.0+.
174
300
Our projects sometimes work with older versions, but this is not guaranteed.
175
301
176
302
## Security
@@ -277,4 +403,24 @@ abide by its terms.
277
403
278
404
[ hast-util-sanitize ] : https://github.com/syntax-tree/hast-util-sanitize
279
405
280
- [ test ] : https://github.com/syntax-tree/hast-util-is-element#api
406
+ [ test ] : https://github.com/syntax-tree/hast-util-is-element#test
407
+
408
+ [ defaultignore ] : #defaultignore
409
+
410
+ [ findandreplace ] : #findandreplacetree-find-replace-options
411
+
412
+ [ options ] : #options
413
+
414
+ [ find ] : #find
415
+
416
+ [ replace ] : #replace
417
+
418
+ [ replacefunction ] : #replacefunction
419
+
420
+ [ findandreplacelist ] : #findandreplacelist
421
+
422
+ [ findandreplaceschema ] : #findandreplaceschema
423
+
424
+ [ findandreplacetuple ] : #findandreplacetuple
425
+
426
+ [ regexpmatchobject ] : #regexpmatchobject
0 commit comments