Skip to content

Instantly share code, notes, and snippets.

@uncenter
Last active December 5, 2023 16:01
Show Gist options
  • Save uncenter/5db09920e8fe879c38e96e59b2367895 to your computer and use it in GitHub Desktop.
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.
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!
:::`),
);
@rauschma
Copy link

rauschma commented Dec 5, 2023

What happens if you nest blocks? (That is something that I find important – Pandoc supports it.)

@uncenter
Copy link
Author

uncenter commented Dec 5, 2023

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>

@rauschma
Copy link

rauschma commented Dec 5, 2023

Got it. Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment