diff --git a/index.js b/index.js index 2639fd3..5498d09 100644 --- a/index.js +++ b/index.js @@ -14,25 +14,20 @@ function slug() { function transformer(ast) { slugs.reset(); - visit(ast, 'heading', function (node) { + visit(ast, 'heading', visitor); + + function visitor(node) { var id = slugs.slug(toString(node)); - var data = patch(node, 'data', {}); - - /* Non-html */ - patch(data, 'id', id); - /* Legacy remark-html */ - patch(data, 'htmlAttributes', {}); - /* Current remark-html */ - patch(data, 'hProperties', {}); - patch(data.htmlAttributes, 'id', id); - patch(data.hProperties, 'id', id); - }); -} -function patch(context, key, value) { - if (!context[key]) { - context[key] = value; - } + if (!node.data) { + node.data = {}; + } - return context[key]; + if (!node.data.hProperties) { + node.data.hProperties = {}; + } + + node.data.id = id; + node.data.hProperties.id = id; + } } diff --git a/test.js b/test.js index f7e8a8d..9e2ba0e 100644 --- a/test.js +++ b/test.js @@ -145,10 +145,6 @@ test('slugs', function (t) { function heading(label, id) { return u('heading', { depth: 2, - data: { - id: id, - htmlAttributes: {id: id}, - hProperties: {id: id} - } + data: {id: id, hProperties: {id: id}} }, label ? [u('text', label)] : []); }