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/readme.md b/readme.md index 44053e4..94499f4 100644 --- a/readme.md +++ b/readme.md @@ -49,6 +49,12 @@ 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][mdast] to functions +handling those elements. +Take a look at [`lib/handlers/`][handlers] for examples. + ###### Returns [`HASTNode`][hast]. @@ -100,3 +106,5 @@ are found. The default behaviour is to prefer the last duplicate definition. [unist-position]: https://github.com/syntax-tree/unist#location [hast-util-sanitize]: https://github.com/syntax-tree/hast-util-sanitize + +[handlers]: lib/handlers 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');