Skip to content

Commit aa4c006

Browse files
committed
Add improved docs
1 parent a17aa0a commit aa4c006

File tree

2 files changed

+209
-41
lines changed

2 files changed

+209
-41
lines changed

Diff for: lib/index.js

+37-15
Original file line numberDiff line numberDiff line change
@@ -22,26 +22,36 @@
2222
* @callback ReplaceFunction
2323
* Callback called when a search matches.
2424
* @param {...any} parameters
25-
* The parameters are the result of the search expressions, which can be
26-
* several if a regex captures groups.
25+
* The parameters are the result of corresponding search expression:
2726
*
28-
* The last parameter is a `RegExpMatchObject`.
27+
* * `value` (`string`) — whole match
28+
* * `...capture` (`Array<string>`) — matches from regex capture groups
29+
* * `match` (`RegExpMatchObject`) — info on the match
2930
* @returns {Array<Content> | Content | string | false | undefined | null}
30-
* Resulting nodes (where `string` is turned into a `Text` node).
31+
* Thing to replace with.
3132
*
32-
* The values `null` and `undefined` and an empty string mean the match is
33-
* removed.
34-
*
35-
* The value `false` means that this isn’t a match after all, and it is not
36-
* replaced.
33+
* * when `null`, `undefined`, `''`, remove the match
34+
* * …or when `false`, do not replace at all
35+
* * …or when `string`, replace with a text node of that value
36+
* * …or when `Node` or `Array<Node>`, replace with those nodes
3737
*
3838
* @typedef {string | RegExp} Find
39-
* @typedef {string | ReplaceFunction} Replace
40-
* @typedef {[Find, Replace]} FindAndReplaceTuple
41-
* @typedef {Record<string, Replace>} FindAndReplaceSchema
39+
* Pattern to find.
40+
*
41+
* Strings are escaped and then turned into global expressions.
42+
*
4243
* @typedef {Array<FindAndReplaceTuple>} FindAndReplaceList
44+
* Several find and replaces, in array form.
45+
* @typedef {Record<string, Replace>} FindAndReplaceSchema
46+
* Several find and replaces, in object form.
47+
* @typedef {[Find, Replace]} FindAndReplaceTuple
48+
* Find and replace in tuple form.
49+
* @typedef {string | ReplaceFunction} Replace
50+
* Thing to replace with.
4351
* @typedef {[RegExp, ReplaceFunction]} Pair
52+
* Normalized find and replace.
4453
* @typedef {Array<Pair>} Pairs
54+
* All find and replaced.
4555
*
4656
* @typedef Options
4757
* Configuration.
@@ -55,7 +65,14 @@ import escape from 'escape-string-regexp'
5565

5666
const own = {}.hasOwnProperty
5767

58-
export const defaultIgnore = ['title', 'script', 'style', 'svg', 'math']
68+
/**
69+
* Default tag names to ignore.
70+
*
71+
* The defaults are `math`, `script`, `style`, `svg`, and `title`.
72+
*
73+
* @type {Array<string>}
74+
*/
75+
export const defaultIgnore = ['math', 'script', 'style', 'svg', 'title']
5976

6077
/**
6178
* Find patterns in a tree and replace them.
@@ -64,14 +81,18 @@ export const defaultIgnore = ['title', 'script', 'style', 'svg', 'math']
6481
* nodes.
6582
* Partial matches are not supported.
6683
*
67-
* @param {Node} tree
84+
* @template {Node} Tree
85+
* Node type.
86+
* @param {Tree} tree
6887
* Tree to change.
6988
* @param {Find | FindAndReplaceSchema | FindAndReplaceList} find
7089
* Patterns to find.
7190
* @param {Replace | Options | null | undefined} [replace]
72-
* `Replace` (when `find` is `Find`) or configuration.
91+
* Things to replace with (when `find` is `Find`) or configuration.
7392
* @param {Options | null | undefined} [options]
7493
* Configuration (when `find` is not `Find`).
94+
* @returns {Tree}
95+
* Given, modified, tree.
7596
*/
7697
export function findAndReplace(tree, find, replace, options) {
7798
/** @type {Options | null | undefined} */
@@ -101,6 +122,7 @@ export function findAndReplace(tree, find, replace, options) {
101122
visitParents(tree, 'text', visitor)
102123
}
103124

125+
// To do next major: don’t return the given tree.
104126
return tree
105127

106128
/** @type {import('unist-util-visit-parents/complex-types.js').BuildVisitor<Node, 'text'>} */

Diff for: readme.md

+172-26
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,15 @@
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

4251
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][]:
4453

4554
```sh
4655
npm 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].
122132
There is no default export.
123133

124134
### `findAndReplace(tree, find, replace[, options])`
125135

126136
Find patterns in a tree and replace them.
137+
127138
The algorithm searches the tree in *[preorder][]* for complete values in
128139
[`Text`][text] nodes.
129140
Partial 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
164285
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].
168294
169295
## Compatibility
170296
171297
Projects maintained by the unified collective are compatible with all maintained
172298
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+.
174300
Our 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

Comments
 (0)