Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 9 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
'use strict'

var visit = require('unist-util-visit-parents')
var is = require('hast-util-is-element')
var is = require('unist-util-is')
var escape = require('escape-string-regexp')

var defaultIgnore = ['title', 'script', 'style', 'svg', 'math']
Expand Down Expand Up @@ -119,6 +119,14 @@ function findAndReplace(tree, find, replace, options) {
function search(tree, options, handler) {
var ignore = options.ignore || defaultIgnore
var result = []
if (!Array.isArray(ignore)) {
ignore = [ignore]
}

ignore = ignore.map(function (x) {
if (typeof x !== 'string') return x
return {tagName: x}
})

visit(tree, 'text', visitor)

Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
],
"dependencies": {
"escape-string-regexp": "^2.0.0",
"hast-util-is-element": "^1.0.0",
"unist-util-is": "4.0.2",
"unist-util-visit-parents": "^3.0.0"
},
"devDependencies": {
Expand Down
42 changes: 42 additions & 0 deletions test.js
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,48 @@ test('findAndReplace', function (t) {
'should ignore from options'
)

t.deepEqual(
findAndReplace(
h('p', [
h('span', 'visible text'),
h('span', {
text: 'hidden text',
style: 'font-size:1px;display:none;line-height:1px;'
})
]),
'text',
'TEXT',
{
ignore: function (node) {
return /display:\s*none/.test(node.properties.style)
}
}
),
h('p', [
h('span', ['visible ', 'TEXT']),
h('span', {
text: 'hidden text',
style: 'font-size:1px;display:none;line-height:1px;'
})
]),
'should ignore using function'
)

t.deepEqual(
findAndReplace(
h('p', [h('span', 'text'), h('sup', 'text')]),
'text',
'TEXT',
{
ignore: {
tagName: 'sup'
}
}
),
h('p', [h('span', 'TEXT'), h('sup', 'text')]),
'should ignore using objects'
)

t.deepEqual(
findAndReplace(h('p', 'Some emphasis, importance, and code.'), {
importance: function (match) {
Expand Down