From a23d542f1005b4ffd1765afd7c0560488f83c101 Mon Sep 17 00:00:00 2001 From: sethvincent Date: Wed, 15 Feb 2017 21:30:28 -0800 Subject: [PATCH 1/3] add support for handlers option --- lib/index.js | 2 ++ lib/one.js | 3 +-- test/handlers-option.js | 23 +++++++++++++++++++++++ test/index.js | 2 ++ 4 files changed, 28 insertions(+), 2 deletions(-) create mode 100644 test/handlers-option.js diff --git a/lib/index.js b/lib/index.js index 7a731b2..c8dea12 100644 --- a/lib/index.js +++ b/lib/index.js @@ -10,6 +10,7 @@ var generated = require('unist-util-generated'); var definitions = require('mdast-util-definitions'); var one = require('./one'); var footer = require('./footer'); +var handlers = require('./handlers'); /* Factory to transform. */ function factory(tree, options) { @@ -20,6 +21,7 @@ function factory(tree, options) { h.definition = definitions(tree, settings); h.footnotes = []; h.augment = augment; + h.handlers = xtend(handlers, (settings.handlers || {})); visit(tree, 'footnoteDefinition', visitor); diff --git a/lib/one.js b/lib/one.js index 230c306..4a01282 100644 --- a/lib/one.js +++ b/lib/one.js @@ -5,7 +5,6 @@ module.exports = one; var u = require('unist-builder'); var has = require('has'); var all = require('./all'); -var handlers = require('./handlers'); /* Transform an unknown node. */ function unknown(h, node) { @@ -19,7 +18,7 @@ function unknown(h, node) { /* Visit a node. */ function one(h, node, parent) { var type = node && node.type; - var fn = has(handlers, type) ? handlers[type] : null; + var fn = has(h.handlers, type) ? h.handlers[type] : null; /* Fail on non-nodes. */ if (!type) { diff --git a/test/handlers-option.js b/test/handlers-option.js new file mode 100644 index 0000000..77fad0e --- /dev/null +++ b/test/handlers-option.js @@ -0,0 +1,23 @@ +'use strict'; + +var test = require('tape'); +var u = require('unist-builder'); +var to = require('..'); +var all = require('../lib/all'); + +test('handlers option', function (t) { + var handlers = { + paragraph: function (h, node) { + node.children[0].value = 'changed'; + return h(node, 'p', all(h, node)); + } + }; + + t.deepEqual( + to(u('paragraph', [u('text', 'bravo')]), {handlers: handlers}), + u('element', {tagName: 'p', properties: {}}, [u('text', 'changed')]), + 'should override default handler' + ); + + t.end(); +}); diff --git a/test/index.js b/test/index.js index d9e3227..7781b0a 100644 --- a/test/index.js +++ b/test/index.js @@ -28,3 +28,5 @@ require('./table.js'); require('./text.js'); require('./thematic-break.js'); require('./yaml.js'); + +require('./handlers-option.js'); From c710fc380809cb15d74905b42c88586c3f979ddf Mon Sep 17 00:00:00 2001 From: sethvincent Date: Wed, 15 Feb 2017 21:34:36 -0800 Subject: [PATCH 2/3] add options.handlers to readme --- readme.md | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/readme.md b/readme.md index 44053e4..76f0ffa 100644 --- a/readme.md +++ b/readme.md @@ -49,6 +49,11 @@ default: `false`). Only do this when compiling later with Set to `true` (default: `false`) to prefer the first when duplicate definitions are found. The default behaviour is to prefer the last duplicate definition. +###### `options.handlers` + +Object mapping [MDAST nodes](https://github.com/syntax-tree/mdast) to functions handling those elements. +Take a look at [`lib/handlers/`][lib/handlers] for examples. + ###### Returns [`HASTNode`][hast]. From cb7aea137b14906d01a20ce844572ff54c78beff Mon Sep 17 00:00:00 2001 From: sethvincent Date: Wed, 15 Feb 2017 21:40:10 -0800 Subject: [PATCH 3/3] fix readme --- readme.md | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/readme.md b/readme.md index 76f0ffa..94499f4 100644 --- a/readme.md +++ b/readme.md @@ -51,8 +51,9 @@ are found. The default behaviour is to prefer the last duplicate definition. ###### `options.handlers` -Object mapping [MDAST nodes](https://github.com/syntax-tree/mdast) to functions handling those elements. -Take a look at [`lib/handlers/`][lib/handlers] for examples. +Object mapping [MDAST nodes][mdast] to functions +handling those elements. +Take a look at [`lib/handlers/`][handlers] for examples. ###### Returns @@ -105,3 +106,5 @@ Take a look at [`lib/handlers/`][lib/handlers] for examples. [unist-position]: https://github.com/syntax-tree/unist#location [hast-util-sanitize]: https://github.com/syntax-tree/hast-util-sanitize + +[handlers]: lib/handlers