8
8
[ ![ Backers] [ backers-badge ]] [ collective ]
9
9
[ ![ Chat] [ chat-badge ]] [ chat ]
10
10
11
- [ ** hast** ] [ hast ] utility to find and replace text in a [ * tree * ] [ tree ] .
11
+ [ hast] [ ] utility to find and replace things .
12
12
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 (such as elements).
32
+ It’s aware of HTML (such as ignoring ` <style> ` and ` <script> ` by default).
14
33
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.
34
+ ## When should I use this?
17
35
18
- [ npm] [ ] :
36
+ This utility is typically useful when you have regexes and want to modify hast.
37
+ One example is when you have some form of “mentions” (such as
38
+ ` /@([a-z][_a-z0-9])\b/gi ` ) and want to create links to persons from them.
39
+
40
+ ## Install
41
+
42
+ This package is [ ESM only] [ esm ] .
43
+ In Node.js (version 12.20+, 14.14+, or 16.0+), install with [ npm] [ ] :
19
44
20
45
``` sh
21
46
npm install hast-util-find-and-replace
22
47
```
23
48
49
+ In Deno with [ ` esm.sh ` ] [ esmsh ] :
50
+
51
+ ``` js
52
+ import {findAndReplace } from ' https://esm.sh/hast-util-find-and-replace@4'
53
+ ```
54
+
55
+ In browsers with [ ` esm.sh ` ] [ esmsh ] :
56
+
57
+ ``` html
58
+ <script type =" module" >
59
+ import {findAndReplace } from ' https://esm.sh/hast-util-find-and-replace@4?bundle'
60
+ </script >
61
+ ```
62
+
24
63
## Use
25
64
26
65
``` js
27
66
import {h } from ' hastscript'
28
- import {inspect } from ' unist-util-inspect'
29
67
import {findAndReplace } from ' hast-util-find-and-replace'
68
+ import {inspect } from ' unist-util-inspect'
30
69
31
70
const tree = h (' p' , [
32
71
' Some ' ,
@@ -38,48 +77,55 @@ const tree = h('p', [
38
77
' .'
39
78
])
40
79
41
- findAndReplace (tree, ' and' , ' or' )
42
-
43
- findAndReplace (tree, {emphasis: ' em' , importance: ' strong' })
44
-
45
- findAndReplace (tree, {
46
- code : function ($0 ) {
47
- return h (' a' , {href: ' //example.com#' + $0}, $0)
48
- }
49
- })
80
+ findAndReplace (tree, [
81
+ [/ and/ gi , ' or' ],
82
+ [/ emphasis/ gi , ' em' ],
83
+ [/ importance/ gi , ' strong' ],
84
+ [
85
+ / code/ gi ,
86
+ function ($0 ) {
87
+ return h (' a' , {href: ' //example.com#' + $0}, $0)
88
+ }
89
+ ]
90
+ ])
50
91
51
92
console .log (inspect (tree))
52
93
```
53
94
54
95
Yields:
55
96
56
97
``` txt
57
- element[9] [tagName="p"]
58
- ├─ text: "Some "
59
- ├─ element[1] [tagName="em"]
60
- │ └─ text: "em"
61
- ├─ text: ", "
62
- ├─ element[1] [tagName="strong"]
63
- │ └─ text: "strong"
64
- ├─ text: ", "
65
- ├─ text: "or"
66
- ├─ text: " "
67
- ├─ element[1] [tagName="code"]
68
- │ └─ element[1] [tagName="a"][properties={"href":"//example.com#code"}]
69
- │ └─ text: "code"
70
- └─ text: "."
98
+ element<p>[9]
99
+ │ properties: {}
100
+ ├─0 text "Some "
101
+ ├─1 element<em>[1]
102
+ │ │ properties: {}
103
+ │ └─0 text "em"
104
+ ├─2 text ", "
105
+ ├─3 element<strong>[1]
106
+ │ │ properties: {}
107
+ │ └─0 text "strong"
108
+ ├─4 text ", "
109
+ ├─5 text "or"
110
+ ├─6 text " "
111
+ ├─7 element<code>[1]
112
+ │ │ properties: {}
113
+ │ └─0 element<a>[1]
114
+ │ │ properties: {"href":"//example.com#code"}
115
+ │ └─0 text "code"
116
+ └─8 text "."
71
117
```
72
118
73
119
## API
74
120
75
- This package exports the following identifiers: ` findAndReplace ` , ` defaultIgnore ` .
121
+ This package exports the identifiers ` findAndReplace ` and ` defaultIgnore ` .
76
122
There is no default export.
77
123
78
- ### ` findAndReplace(tree, find[ , replace] [, options]) `
124
+ ### ` findAndReplace(tree, find, replace[, options]) `
79
125
80
- Find and replace text in a [ ** hast ** ] [ hast ] [ * tree* ] [ tree ] .
81
- The algorithm searches the tree in [ * preorder* ] [ preorder ] for complete values
82
- in [ ` Text ` ] [ text ] nodes.
126
+ Find patterns in a tree and replace them .
127
+ The algorithm searches the tree in * [ preorder] [ ] * for complete values in
128
+ [ ` Text ` ] [ text ] nodes.
83
129
Partial matches are not supported.
84
130
85
131
###### Signatures
@@ -90,43 +136,56 @@ Partial matches are not supported.
90
136
###### Parameters
91
137
92
138
* ` tree ` ([ ` Node ` ] [ node ] )
93
- — [ ** hast** ] [ hast ] [ * tree* ] [ tree ]
94
139
* ` find ` (` string ` or ` RegExp ` )
95
- — Value to find and remove.
96
- When ` string ` , escaped and made into a global ` RegExp `
140
+ — value to find and remove ( ` string ` s are escaped and turned into a global
141
+ ` RegExp ` )
97
142
* ` replace ` (` string ` or ` Function ` )
98
- — Value to insert.
99
- When ` string ` , turned into a [ ` Text ` ] [ text ] node.
100
- When ` Function ` , called with the results of calling ` RegExp.exec ` as
101
- arguments, in which case it can return a [ ` Node ` ] [ node ] or a ` string ` (which
102
- is wrapped in a [ ` Text ` ] [ text ] node), or ` false ` to not replace
103
- * ` search ` (` Object ` or ` Array ` )
104
- — Perform multiple find-and-replaces.
105
- When ` Array ` , each entry is a tuple (` Array ` ) of a ` find ` (at ` 0 ` ) and
106
- ` replace ` (at ` 1 ` ).
107
- When ` Object ` , each key is a ` find ` (in string form) and each value is a
108
- ` replace `
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 `
109
153
* ` options.ignore ` (` Test ` , default: `[ 'title', 'script', 'style', 'svg',
110
154
'math'] `)
111
- — Any [ ` hast-util-is-element ` ] [ test ] compatible test.
112
- The default list is exported as ` defaultIgnore `
155
+ — any [ ` hast-util-is-element ` ] [ test ] compatible test (the default list is
156
+ exported as ` defaultIgnore ` )
113
157
114
158
###### Returns
115
159
116
- The given, modified, ` tree ` .
160
+ The given ` tree ` ([ ` Node ` ] [ node ] ).
161
+
162
+ ## Types
163
+
164
+ This package is fully typed with [ TypeScript] [ ] .
165
+ It exports the additional types ` Find ` , ` Replace ` , ` ReplaceFunction ` ,
166
+ ` FindAndReplaceTuple ` , ` FindAndReplaceSchema ` , ` FindAndReplaceList ` , and
167
+ ` Options ` .
168
+
169
+ ## Compatibility
170
+
171
+ Projects maintained by the unified collective are compatible with all maintained
172
+ versions of Node.js.
173
+ As of now, that is Node.js 12.20+, 14.14+, and 16.0+.
174
+ Our projects sometimes work with older versions, but this is not guaranteed.
117
175
118
176
## Security
119
177
120
- Improper use of the ` replace ` can open you up to a
121
- [ cross-site scripting (XSS)] [ xss ] attack as the value of ` replace ` is injected
122
- into the syntax tree.
178
+ Use of ` hast-util-find-and-replace ` can open you up to a
179
+ [ cross-site scripting (XSS)] [ xss ] attack if a value used to ` replace ` is unsafe.
180
+ Use [ ` hast-util-santize ` ] [ hast-util-sanitize ] to make the hast tree safe.
181
+
123
182
The following example shows how a script is injected that runs when loaded in a
124
183
browser.
125
184
126
185
``` js
127
- findAndReplace ( h (' p' , ' This and that.' ), ' and ' , function () {
128
- return h ( ' script ' , ' alert(1) ' )
129
- } )
186
+ const tree = h (' p' , ' This and that.' )
187
+
188
+ findAndReplace (tree, ' and ' , () => h ( ' script ' , ' alert(1) ' ) )
130
189
```
131
190
132
191
Yields:
@@ -135,23 +194,23 @@ Yields:
135
194
<p >This <script >alert (1 ) </script > that.</p >
136
195
```
137
196
138
- Do not use user input in ` replace ` or use [ ` hast-util-santize ` ] [ sanitize ] .
139
-
140
197
## Related
141
198
142
199
* [ ` hast-util-select ` ] ( https://github.com/syntax-tree/hast-util-select )
143
200
— ` querySelector ` , ` querySelectorAll ` , and ` matches `
201
+ * [ ` mdast-util-find-and-replace ` ] ( https://github.com/syntax-tree/mdast-util-find-and-replace )
202
+ — find and replace in mdast
144
203
* [ ` unist-util-select ` ] ( https://github.com/syntax-tree/unist-util-select )
145
204
— select unist nodes with CSS-like selectors
146
205
147
206
## Contribute
148
207
149
- See [ ` contributing.md ` in ` syntax-tree/.github ` ] [ contributing ] for ways to get
150
- started.
208
+ See [ ` contributing.md ` ] [ contributing ] in [ ` syntax-tree/.github ` ] [ health ] for
209
+ ways to get started.
151
210
See [ ` support.md ` ] [ support ] for ways to get help.
152
211
153
212
This project has a [ code of conduct] [ coc ] .
154
- By interacting with this repository, organization , or community you agree to
213
+ By interacting with this repository, organisation , or community you agree to
155
214
abide by its terms.
156
215
157
216
## License
@@ -188,28 +247,34 @@ abide by its terms.
188
247
189
248
[ npm ] : https://docs.npmjs.com/cli/install
190
249
250
+ [ esm ] : https://gist.github.com/sindresorhus/a39789f98801d908bbc7ff3ecc99d99c
251
+
252
+ [ esmsh ] : https://esm.sh
253
+
254
+ [ typescript ] : https://www.typescriptlang.org
255
+
191
256
[ license ] : license
192
257
193
258
[ author ] : https://wooorm.com
194
259
195
- [ contributing ] : https://github.com/syntax-tree/.github/blob/HEAD/contributing.md
260
+ [ health ] : https://github.com/syntax-tree/.github
196
261
197
- [ support ] : https://github.com/syntax-tree/.github/blob/HEAD/support .md
262
+ [ contributing ] : https://github.com/syntax-tree/.github/blob/main/contributing .md
198
263
199
- [ coc ] : https://github.com/syntax-tree/.github/blob/HEAD/code-of-conduct.md
264
+ [ support ] : https://github.com/syntax-tree/.github/blob/main/support.md
265
+
266
+ [ coc ] : https://github.com/syntax-tree/.github/blob/main/code-of-conduct.md
200
267
201
268
[ hast ] : https://github.com/syntax-tree/hast
202
269
203
270
[ node ] : https://github.com/syntax-tree/hast#ndoes
204
271
205
- [ tree ] : https://github.com/syntax-tree/unist#tree
206
-
207
272
[ preorder ] : https://github.com/syntax-tree/unist#preorder
208
273
209
274
[ text ] : https://github.com/syntax-tree/hast#text
210
275
211
276
[ xss ] : https://en.wikipedia.org/wiki/Cross-site_scripting
212
277
213
- [ sanitize ] : https://github.com/syntax-tree/hast-util-sanitize
278
+ [ hast-util- sanitize] : https://github.com/syntax-tree/hast-util-sanitize
214
279
215
280
[ test ] : https://github.com/syntax-tree/hast-util-is-element#api
0 commit comments