diff --git a/index.js b/index.js index 6e6559b..fa02227 100644 --- a/index.js +++ b/index.js @@ -7,16 +7,26 @@ var sanitize = require('hast-util-sanitize') module.exports = plugin function plugin(options) { - var settings = options || {} - var clean = settings.sanitize - var schema = clean && typeof clean === 'object' ? clean : null - var handlers = settings.handlers || {} + var settings = Object.assign({}, options || {}) + let clean + + if (typeof settings.sanitize === 'boolean') { + clean = settings.sanitize + settings.sanitize = undefined + } + + if (typeof clean !== 'boolean') { + clean = true + } this.Compiler = compiler function compiler(node, file) { var root = node && node.type && node.type === 'root' - var hast = toHast(node, {allowDangerousHtml: !clean, handlers: handlers}) + var hast = toHast(node, { + allowDangerousHtml: !clean, + handlers: settings.handlers + }) var result if (file.extname) { @@ -24,7 +34,7 @@ function plugin(options) { } if (clean) { - hast = sanitize(hast, schema) + hast = sanitize(hast, settings.sanitize) } result = toHtml( diff --git a/test/index.js b/test/index.js index 0c0b2e9..5f1e4e1 100644 --- a/test/index.js +++ b/test/index.js @@ -37,7 +37,7 @@ test('remark-html()', function (t) { 'should throw when not given a node' ) - processor = remark().use(html) + processor = remark().use(html, {sanitize: false}) t.equal( processor.stringify({type: 'alpha'}), @@ -69,6 +69,7 @@ test('remark-html()', function (t) { ) processor = remark().use(html, { + sanitize: false, handlers: { paragraph: function (h, node) { node.children[0].value = 'changed' @@ -91,7 +92,7 @@ test('remark-html()', function (t) { } } }) - .use(html) + .use(html, {sanitize: false}) t.equal( processor.processSync('![hello](example.jpg "overwritten")').toString(), @@ -105,7 +106,7 @@ test('remark-html()', function (t) { ast.children[0].children[0].data = {hName: 'b'} } }) - .use(html) + .use(html, {sanitize: false}) t.equal( processor.processSync('**Bold!**').toString(), @@ -130,7 +131,7 @@ test('remark-html()', function (t) { } } }) - .use(html) + .use(html, {sanitize: false}) t.equal( processor.processSync('`var`').toString(), @@ -171,7 +172,7 @@ test('remark-html()', function (t) { } } }) - .use(html) + .use(html, {sanitize: false}) t.equal( processor.processSync('```js\nvar\n```\n').toString(), @@ -180,7 +181,10 @@ test('remark-html()', function (t) { ) t.equal( - remark().use(html).processSync('## Hello world').toString(), + remark() + .use(html, {sanitize: false}) + .processSync('## Hello world') + .toString(), '