Skip to content

Commit 1294628

Browse files
sethvincentwooorm
authored andcommitted
Add support for overwriting handlers
Closes GH-26.
1 parent 8748a7a commit 1294628

File tree

3 files changed

+49
-7
lines changed

3 files changed

+49
-7
lines changed

index.js

+5-2
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,14 @@ module.exports = toMDAST;
55
var minify = require('rehype-minify-whitespace')();
66
var xtend = require('xtend');
77
var one = require('./one');
8+
var handlers = require('./handlers');
89

910
h.augment = augment;
1011

11-
function toMDAST(tree) {
12-
return one(h, minify(tree));
12+
function toMDAST(tree, options) {
13+
options = options || {};
14+
h.handlers = xtend(handlers, options.handlers || {});
15+
return one(h, minify(tree), null);
1316
}
1417

1518
function h(node, type, props, children) {

one.js

+4-5
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,14 @@ module.exports = one;
44

55
var has = require('has');
66
var all = require('./all');
7-
var handlers = require('./handlers');
87

98
function one(h, node, parent) {
109
var fn = null;
1110

12-
if (node.type === 'element' && has(handlers, node.tagName)) {
13-
fn = handlers[node.tagName];
14-
} else if (has(handlers, node.type)) {
15-
fn = handlers[node.type];
11+
if (node.type === 'element' && has(h.handlers, node.tagName)) {
12+
fn = h.handlers[node.tagName];
13+
} else if (has(h.handlers, node.type)) {
14+
fn = h.handlers[node.type];
1615
}
1716

1817
return (typeof fn === 'function' ? fn : unknown)(h, node, parent);

tests/index.js

+40
Original file line numberDiff line numberDiff line change
@@ -149,3 +149,43 @@ test('fixtures', function (t) {
149149
});
150150
}
151151
});
152+
153+
test('handlers option', function (t) {
154+
var options = {
155+
handlers: {
156+
div: function (h, node) {
157+
node.children[0].value = 'Beta';
158+
node.type = 'paragraph';
159+
return h(node, 'paragraph', node.children);
160+
}
161+
}
162+
};
163+
164+
t.deepEqual(
165+
toMDAST({
166+
type: 'root',
167+
children: [{
168+
type: 'element',
169+
tagName: 'div',
170+
properties: {},
171+
children: [{
172+
type: 'text',
173+
value: 'Alpha'
174+
}]
175+
}]
176+
}, options),
177+
{
178+
type: 'root',
179+
children: [{
180+
type: 'paragraph',
181+
children: [{
182+
type: 'text',
183+
value: 'Beta'
184+
}]
185+
}]
186+
},
187+
'use handlers passed as option'
188+
);
189+
190+
t.end();
191+
});

0 commit comments

Comments
 (0)