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