Skip to content

Commit 5bbb92c

Browse files
committed
Add improved docs
1 parent 589b5b0 commit 5bbb92c

File tree

1 file changed

+123
-58
lines changed

1 file changed

+123
-58
lines changed

readme.md

Lines changed: 123 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -8,19 +8,60 @@
88
[![Backers][backers-badge]][collective]
99
[![Chat][chat-badge]][chat]
1010

11-
[**mdast**][mdast] utility to find and replace text in a [*tree*][tree].
11+
[mdast][] utility to find and replace things.
1212

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][].
1441

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
1743

18-
[npm][]:
44+
This package is [ESM only][esm].
45+
In Node.js (version 12.20+, 14.14+, or 16.0+), install with [npm][]:
1946

2047
```sh
2148
npm install mdast-util-find-and-replace
2249
```
2350

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+
2465
## Use
2566

2667
```js
@@ -36,15 +77,17 @@ const tree = u('paragraph', [
3677
u('text', '.')
3778
])
3879

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+
])
4891

4992
console.log(inspect(tree))
5093
```
@@ -53,83 +96,97 @@ Yields:
5396

5497
```txt
5598
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 "."
67111
```
68112

69113
## API
70114

71-
This package exports the following identifiers: `findAndReplace`.
115+
This package exports the identifier `findAndReplace`.
72116
There is no default export.
73117

74-
### `findAndReplace(tree, find[, replace][, options])`
118+
### `findAndReplace(tree, find, replace[, options])`
75119

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.
79123
Partial matches are not supported.
80124

81125
###### Signatures
82126

83-
* `findAndReplace(tree, find, replace?[, options])`
127+
* `findAndReplace(tree, find, replace[, options])`
84128
* `findAndReplace(tree, search[, options])`
85129

86130
###### Parameters
87131

88132
* `tree` ([`Node`][node])
89-
[**mdast**][mdast] [*tree*][tree]
90133
* `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`)
93136
* `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`
106147
* `options.ignore` (`Test`, default: `[]`)
107148
— Any [`unist-util-is`][test] compatible test.
108149

109150
###### Returns
110151

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.
112167

113168
## Security
114169

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.
117172

118173
## Related
119174

120175
* [`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`
122179
* [`unist-util-select`](https://github.com/syntax-tree/unist-util-select)
123180
— select unist nodes with CSS-like selectors
124181

125182
## Contribute
126183

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.
129186
See [`support.md`][support] for ways to get help.
130187

131188
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
133190
abide by its terms.
134191

135192
## License
@@ -166,28 +223,36 @@ abide by its terms.
166223

167224
[npm]: https://docs.npmjs.com/cli/install
168225

226+
[esm]: https://gist.github.com/sindresorhus/a39789f98801d908bbc7ff3ecc99d99c
227+
228+
[esmsh]: https://esm.sh
229+
230+
[typescript]: https://www.typescriptlang.org
231+
169232
[license]: license
170233

171234
[author]: https://wooorm.com
172235

173-
[contributing]: https://github.com/syntax-tree/.github/blob/HEAD/contributing.md
236+
[health]: https://github.com/syntax-tree/.github
174237

175-
[support]: https://github.com/syntax-tree/.github/blob/HEAD/support.md
238+
[contributing]: https://github.com/syntax-tree/.github/blob/main/contributing.md
176239

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
178243

179244
[hast]: https://github.com/syntax-tree/hast
180245

181246
[mdast]: https://github.com/syntax-tree/mdast
182247

183248
[node]: https://github.com/syntax-tree/mdast#ndoes
184249

185-
[tree]: https://github.com/syntax-tree/unist#tree
186-
187250
[preorder]: https://github.com/syntax-tree/unist#preorder
188251

189252
[text]: https://github.com/syntax-tree/mdast#text
190253

191254
[xss]: https://en.wikipedia.org/wiki/Cross-site_scripting
192255

193256
[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

Comments
 (0)