1818* [ Use] ( #use )
1919* [ API] ( #api )
2020 * [ ` 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 )
2130* [ Types] ( #types )
2231* [ Compatibility] ( #compatibility )
2332* [ Security] ( #security )
@@ -40,7 +49,7 @@ One example is when you have some form of “mentions” (such as
4049## Install
4150
4251This 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] [ ] :
4453
4554``` sh
4655npm install hast-util-find-and-replace
@@ -118,12 +127,14 @@ element<p>[9]
118127
119128## API
120129
121- This package exports the identifiers ` findAndReplace ` and ` defaultIgnore ` .
130+ This package exports the identifiers [ ` defaultIgnore ` ] [ defaultignore ] and
131+ [ ` findAndReplace ` ] [ findandreplace ] .
122132There is no default export.
123133
124134### ` findAndReplace(tree, find, replace[, options]) `
125135
126136Find patterns in a tree and replace them.
137+
127138The algorithm searches the tree in * [ preorder] [ ] * for complete values in
128139[ ` Text ` ] [ text ] nodes.
129140Partial matches are not supported.
@@ -136,41 +147,156 @@ Partial matches are not supported.
136147###### Parameters
137148
138149* ` 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
157160
158161###### Returns
159162
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
161282
162283## Types
163284
164285This 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].
168294
169295## Compatibility
170296
171297Projects maintained by the unified collective are compatible with all maintained
172298versions 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+.
174300Our projects sometimes work with older versions, but this is not guaranteed.
175301
176302## Security
@@ -277,4 +403,24 @@ abide by its terms.
277403
278404[ hast-util-sanitize ] : https://github.com/syntax-tree/hast-util-sanitize
279405
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