Skip to content

Commit

Permalink
Add support for handlers in options
Browse files Browse the repository at this point in the history
Closes GH-10.
  • Loading branch information
sethvincent authored and wooorm committed Feb 16, 2017
1 parent e5f4be4 commit 0f00b83
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 2 deletions.
2 changes: 2 additions & 0 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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);

Expand Down
3 changes: 1 addition & 2 deletions lib/one.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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) {
Expand Down
8 changes: 8 additions & 0 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -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].
Expand Down Expand Up @@ -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
23 changes: 23 additions & 0 deletions test/handlers-option.js
Original file line number Diff line number Diff line change
@@ -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();
});
2 changes: 2 additions & 0 deletions test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,3 +28,5 @@ require('./table.js');
require('./text.js');
require('./thematic-break.js');
require('./yaml.js');

require('./handlers-option.js');

0 comments on commit 0f00b83

Please sign in to comment.