Created
July 5, 2022 20:57
-
-
Save segphault/cf858d6edca3bf89f29c56145c8609c2 to your computer and use it in GitHub Desktop.
Definition lists in Markdoc
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const Markdoc = require('@markdoc/markdoc'); | |
const example = ` | |
{% definition-list %} | |
{% definition term="this is the term" %} | |
This is the definition | |
{% /definition %} | |
{% definition term="another term" %} | |
This is another definition | |
{% /definition %} | |
{% /definition-list %} | |
`; | |
const config = { | |
tags: { | |
'definition-list': { render: 'dl' }, | |
definition: { | |
attributes: { | |
term: { type: String }, | |
}, | |
transform(node, config) { | |
var children = node.transformChildren(config); | |
return [ | |
new Markdoc.Tag('dt', {}, node.attributes.term), | |
new Markdoc.Tag('dd', {}, children[0].children), | |
]; | |
}, | |
}, | |
}, | |
}; | |
const ast = Markdoc.parse(example); | |
const content = Markdoc.transform(ast, config); | |
const output = Markdoc.renderers.html(content); | |
console.log(output); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment