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