Last active
December 5, 2023 16:01
-
-
Save uncenter/5db09920e8fe879c38e96e59b2367895 to your computer and use it in GitHub Desktop.
Use any class for markdown-it-container instead of just a single one.
This file contains 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 markdownItContainer = require('markdown-it-container'); | |
const markdownIt = require('markdown-it'); | |
const md = new markdownIt().use(markdownItContainer, 'dynamic', { | |
validate: function () { | |
return true; | |
}, | |
render: function (tokens, idx) { | |
const token = tokens[idx]; | |
return token.nesting === 1 | |
? '<div class="' + token.info.trim() + '">' | |
: '</div>'; | |
}, | |
}); | |
console.log( | |
md.render(` | |
::: my-dynamic-block | |
Hello, world! | |
:::`), | |
); |
What happens if you nest blocks? (That is something that I find important – Pandoc supports it.)
Looks like that works too... kinda:
md.render(`
::: my-dynamic-block
Hello, world!
::: nested-block
Something inside my-dynamic-block!
:::
:::
`);
<div class="my-dynamic-block"><p>Hello, world!</p>
<div class="nested-block"><p>Something inside my-dynamic-block!</p>
</div></div><div class=""></div>
For some reason the closing :::
leaves an extra <div class=""></div>
- or in other words, closing the nested block also closes the block it is inside of:
md.render(`
::: my-dynamic-block
Hello, world!
::: nested-block
Something inside my-dynamic-block!
:::
`);
<div class="my-dynamic-block"><p>Hello, world!</p>
<div class="nested-block"><p>Something inside my-dynamic-block!</p>
</div></div>
Got it. Thanks!
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
What happens if you nest blocks? (That is something that I find important – Pandoc supports it.)