Created
July 5, 2022 21:02
-
-
Save rpaul-stripe/aa6b999e6395fd832eaf3f9efd6e5e3c 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 a definition with **Markdown formatting** | |
{% /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