Skip to content

Commit 619c982

Browse files
authored
Add support for unknownHandler
Closes GH-37. Reviewed-by: Titus Wormer <[email protected]>
1 parent c53823f commit 619c982

File tree

4 files changed

+51
-4
lines changed

4 files changed

+51
-4
lines changed

lib/index.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ function factory(tree, options) {
2525
h.footnoteOrder = []
2626
h.augment = augment
2727
h.handlers = Object.assign({}, handlers, settings.handlers)
28+
h.unknownHandler = settings.unknownHandler
2829

2930
visit(tree, 'footnoteDefinition', onfootnotedefinition)
3031

lib/one.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ function unknown(h, node) {
1919
// Visit a node.
2020
function one(h, node, parent) {
2121
var type = node && node.type
22-
var fn = own.call(h.handlers, type) ? h.handlers[type] : null
22+
var fn = own.call(h.handlers, type) ? h.handlers[type] : h.unknownHandler
2323

2424
// Fail on non-nodes.
2525
if (!type) {

readme.md

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -76,13 +76,22 @@ Only do this when using [`hast-util-to-html`][to-html]
7676

7777
Set to `true` (default: `false`) to prefer the first when duplicate definitions
7878
are found.
79-
The default behaviour is to prefer the last duplicate definition.
79+
The default behavior is to prefer the last duplicate definition.
8080

8181
###### `options.handlers`
8282

8383
Object mapping [mdast][] [nodes][mdast-node] to functions handling them.
8484
Take a look at [`lib/handlers/`][handlers] for examples.
8585

86+
###### `options.unknownHandler`
87+
88+
Handler for all unknown nodes.
89+
90+
Default behavior:
91+
92+
* Unknown nodes with [`children`][child] are transformed to `div` elements
93+
* Unknown nodes with `value` are transformed to [`text`][hast-text] nodes
94+
8695
##### Returns
8796

8897
[`HastNode`][hast-node].
@@ -93,8 +102,6 @@ Take a look at [`lib/handlers/`][handlers] for examples.
93102
[`remark-frontmatter`][remark-frontmatter])
94103
* [`html`][mdast-html] nodes are ignored if `allowDangerousHTML` is `false`
95104
* [`position`][position]s are properly patched
96-
* Unknown nodes with [`children`][child] are transformed to `div` elements
97-
* Unknown nodes with `value` are transformed to [`text`][hast-text] nodes
98105
* [`node.data.hName`][hname] configures the hast element’s tag-name
99106
* [`node.data.hProperties`][hproperties] is mixed into the hast element’s
100107
properties

test/handlers-option.js

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,5 +19,44 @@ test('handlers option', function(t) {
1919
'should override default handler'
2020
)
2121

22+
var customMdast = u('paragraph', [
23+
u('custom', 'with value'),
24+
u('custom', [u('image', {url: 'with-children.png'})]),
25+
u('text', 'bravo')
26+
])
27+
28+
t.deepEqual(
29+
to(customMdast, {}),
30+
u('element', {tagName: 'p', properties: {}}, [
31+
u('text', 'with value'),
32+
u('element', {tagName: 'div', properties: {}}, [
33+
u(
34+
'element',
35+
{
36+
tagName: 'img',
37+
properties: {src: 'with-children.png', alt: undefined}
38+
},
39+
[]
40+
)
41+
]),
42+
u('text', 'bravo')
43+
]),
44+
'should use default unknown-handler'
45+
)
46+
47+
t.deepEqual(
48+
to(customMdast, {
49+
unknownHandler: function(n, node) {
50+
return node
51+
}
52+
}),
53+
u('element', {tagName: 'p', properties: {}}, [
54+
u('custom', 'with value'),
55+
u('custom', [u('image', {url: 'with-children.png'})]),
56+
u('text', 'bravo')
57+
]),
58+
'should use custom unknown-handler'
59+
)
60+
2261
t.end()
2362
})

0 commit comments

Comments
 (0)