Skip to content

Commit 536e7e5

Browse files
committed
.
0 parents  commit 536e7e5

10 files changed

+776
-0
lines changed

.editorconfig

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
root = true
2+
3+
[*]
4+
indent_style = space
5+
indent_size = 2
6+
end_of_line = lf
7+
charset = utf-8
8+
trim_trailing_whitespace = true
9+
insert_final_newline = true

.gitignore

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
.DS_Store
2+
*.log
3+
.nyc_output/
4+
coverage/
5+
node_modules/
6+
yarn.lock

.npmrc

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
package-lock=false

.prettierignore

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
coverage/
2+
*.json
3+
*.md

.travis.yml

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
language: node_js
2+
node_js:
3+
- lts/dubnium
4+
- node
5+
after_script: bash <(curl -s https://codecov.io/bash)

index.js

+183
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,183 @@
1+
'use strict'
2+
3+
module.exports = findAndReplace
4+
5+
var visit = require('unist-util-visit-parents')
6+
var convert = require('unist-util-is/convert')
7+
var escape = require('escape-string-regexp')
8+
9+
var splice = [].splice
10+
11+
function findAndReplace(tree, find, replace, options) {
12+
var settings
13+
var schema
14+
15+
if (
16+
typeof find === 'string' ||
17+
(find && typeof find.lastIndex === 'number')
18+
) {
19+
schema = [[find, replace]]
20+
} else {
21+
schema = find
22+
options = replace
23+
}
24+
25+
settings = options || {}
26+
27+
search(tree, settings, handlerFactory(toPairs(schema)))
28+
29+
return tree
30+
31+
function handlerFactory(pairs) {
32+
var pair = pairs[0]
33+
34+
return handler
35+
36+
function handler(node, parent) {
37+
var find = pair[0]
38+
var replace = pair[1]
39+
var nodes = []
40+
var start = 0
41+
var index = parent.children.indexOf(node)
42+
var position
43+
var match
44+
var subhandler
45+
var value
46+
47+
find.lastIndex = 0
48+
49+
match = find.exec(node.value)
50+
51+
while (match) {
52+
position = match.index
53+
value = replace.apply(
54+
null,
55+
[].concat(match, {index: match.index, input: match.input})
56+
)
57+
58+
if (value !== false) {
59+
if (start !== position) {
60+
nodes.push({type: 'text', value: node.value.slice(start, position)})
61+
}
62+
63+
if (typeof value === 'string' && value.length !== 0) {
64+
value = {type: 'text', value: value}
65+
}
66+
67+
if (value) {
68+
nodes.push(value)
69+
}
70+
71+
start = position + match[0].length
72+
}
73+
74+
if (!find.global) {
75+
break
76+
}
77+
78+
match = find.exec(node.value)
79+
}
80+
81+
if (position === undefined) {
82+
nodes = [node]
83+
index--
84+
} else {
85+
if (start < node.value.length) {
86+
nodes.push({type: 'text', value: node.value.slice(start)})
87+
}
88+
89+
nodes.unshift(index, 1)
90+
splice.apply(parent.children, nodes)
91+
}
92+
93+
if (pairs.length > 1) {
94+
subhandler = handlerFactory(pairs.slice(1))
95+
position = -1
96+
97+
while (++position < nodes.length) {
98+
node = nodes[position]
99+
100+
if (node.type === 'text') {
101+
subhandler(node, parent)
102+
} else {
103+
search(node, settings, subhandler)
104+
}
105+
}
106+
}
107+
108+
return index + nodes.length + 1
109+
}
110+
}
111+
}
112+
113+
function search(tree, settings, handler) {
114+
var ignored = convert(settings.ignore || [])
115+
var result = []
116+
117+
visit(tree, 'text', visitor)
118+
119+
return result
120+
121+
function visitor(node, parents) {
122+
var index = -1
123+
var parent
124+
var grandparent
125+
126+
while (++index < parents.length) {
127+
parent = parents[index]
128+
129+
if (
130+
ignored(
131+
parent,
132+
grandparent ? grandparent.children.indexOf(parent) : undefined,
133+
grandparent
134+
)
135+
) {
136+
return
137+
}
138+
139+
grandparent = parent
140+
}
141+
142+
return handler(node, grandparent)
143+
}
144+
}
145+
146+
function toPairs(schema) {
147+
var result = []
148+
var key
149+
var index
150+
151+
if (typeof schema !== 'object') {
152+
throw new Error('Expected array or object as schema')
153+
}
154+
155+
if ('length' in schema) {
156+
index = -1
157+
158+
while (++index < schema.length) {
159+
result.push([
160+
toExpression(schema[index][0]),
161+
toFunction(schema[index][1])
162+
])
163+
}
164+
} else {
165+
for (key in schema) {
166+
result.push([toExpression(key), toFunction(schema[key])])
167+
}
168+
}
169+
170+
return result
171+
}
172+
173+
function toExpression(find) {
174+
return typeof find === 'string' ? new RegExp(escape(find), 'g') : find
175+
}
176+
177+
function toFunction(replace) {
178+
return typeof replace === 'function' ? replace : returner
179+
180+
function returner() {
181+
return replace
182+
}
183+
}

license

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
(The MIT License)
2+
3+
Copyright (c) 2020 Titus Wormer <[email protected]>
4+
5+
Permission is hereby granted, free of charge, to any person obtaining
6+
a copy of this software and associated documentation files (the
7+
'Software'), to deal in the Software without restriction, including
8+
without limitation the rights to use, copy, modify, merge, publish,
9+
distribute, sublicense, and/or sell copies of the Software, and to
10+
permit persons to whom the Software is furnished to do so, subject to
11+
the following conditions:
12+
13+
The above copyright notice and this permission notice shall be
14+
included in all copies or substantial portions of the Software.
15+
16+
THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND,
17+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
19+
IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
20+
CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
21+
TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
22+
SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

package.json

+76
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
{
2+
"name": "mdast-util-find-and-replace",
3+
"version": "0.0.0",
4+
"description": "mdast utility to find and replace text in a tree",
5+
"license": "MIT",
6+
"keywords": [
7+
"unist",
8+
"mdast",
9+
"mdast-util",
10+
"util",
11+
"utility",
12+
"markdown",
13+
"find",
14+
"replace"
15+
],
16+
"repository": "syntax-tree/mdast-util-find-and-replace",
17+
"bugs": "https://github.com/syntax-tree/mdast-util-find-and-replace/issues",
18+
"funding": {
19+
"type": "opencollective",
20+
"url": "https://opencollective.com/unified"
21+
},
22+
"author": "Titus Wormer <[email protected]> (https://wooorm.com)",
23+
"contributors": [
24+
"Titus Wormer <[email protected]> (https://wooorm.com)"
25+
],
26+
"files": [
27+
"index.js"
28+
],
29+
"dependencies": {
30+
"escape-string-regexp": "^4.0.0",
31+
"unist-util-is": "^4.0.0",
32+
"unist-util-visit-parents": "^3.0.0"
33+
},
34+
"devDependencies": {
35+
"nyc": "^15.0.0",
36+
"prettier": "^2.0.0",
37+
"remark-cli": "^9.0.0",
38+
"remark-preset-wooorm": "^8.0.0",
39+
"tape": "^5.0.0",
40+
"unist-builder": "^2.0.0",
41+
"xo": "^0.33.0"
42+
},
43+
"scripts": {
44+
"format": "remark . -qfo && prettier . -w --loglevel warn && xo --fix",
45+
"test-api": "node test",
46+
"test-coverage": "nyc --reporter lcov tape test.js",
47+
"test": "npm run format && npm run test-coverage"
48+
},
49+
"prettier": {
50+
"tabWidth": 2,
51+
"useTabs": false,
52+
"singleQuote": true,
53+
"bracketSpacing": false,
54+
"semi": false,
55+
"trailingComma": "none"
56+
},
57+
"xo": {
58+
"prettier": true,
59+
"esnext": false,
60+
"rules": {
61+
"unicorn/prefer-type-error": "off",
62+
"guard-for-in": "off"
63+
}
64+
},
65+
"nyc": {
66+
"check-coverage": true,
67+
"lines": 100,
68+
"functions": 100,
69+
"branches": 100
70+
},
71+
"remarkConfig": {
72+
"plugins": [
73+
"preset-wooorm"
74+
]
75+
}
76+
}

0 commit comments

Comments
 (0)