Markdown can use various HTML tags, but this article is about how to use class tags. Weโll use the Node.js package library, markdown-it-container, to enable the use of Markdown class tags.
With markdown-it-container, we can add CSS classes in Markdown.
Introduction
First, install markdown-it-container.
npm install markdown-it-container --save

Below is an example of using markdown-it-container to add CSS classes in Markdown.
var md = require('markdown-it')();
md.use(require('markdown-it-container'), 'spoiler', {
validate: function(params) {
return params.trim().match(/^spoiler\s+(.*)$/);
},
render: function (tokens, idx) {
var m = tokens[idx].info.trim().match(/^spoiler\s+(.*)$/);
if (tokens[idx].nesting === 1) {
// opening tag
return '<details><summary>' + m[1] + '</summary>\n';
} else {
// closing tag
return '</details>\n';
}
}
});
md.use(require('markdown-it-container'), 'class', {
validate: function(params) {
return params.trim().match(/^class\s+(.*)$/);
},
render: function (tokens, idx) {
var m = tokens[idx].info.trim().match(/^class\s+(.*)$/);
if (tokens[idx].nesting === 1) {
// opening tag
return `<div class="${m[1]}">\n`;
} else {
// closing tag
return '</div>\n';
}
}
});
console.log("\n");
console.log(md.render('::: spoiler click me\n*contentcontent*\n:::\n'));
console.log(md.render('::: spoiler \n*contentcontent*\n:::\n'));
console.log(md.render('::: spoiler\n*contentcontent*\n:::\n'));
console.log(md.render('::: class warning\n*contentcontent*\n:::\n'));
console.log(md.render('::: class info\n*contentcontent*\n:::\n'));
console.log(md.render('::: class note gray\n*contentcontent*\n:::\n'));
console.log(md.render('::: class note gray\n# hoge \n## hogehoge \n\something\n:::\n'));
Explanation
The above code first uses markdown-it-container to create a tag named spoiler.

This enables adding CSS classes in Markdown.
You can add classes using :class as follows:
<details><summary>click me</summary>
<em>contentcontent</em>
</details>
::: spoiler
<em>contentcontent</em>
:::
::: spoiler
<em>contentcontent</em>
:::
<div class="warning">
<em>contentcontent</em>
</div>
<div class="info">
<em>contentcontent</em>
</div>
<div class="note gray">
<em>contentcontent</em>
</div>
<div class="note gray">
# hoge
## hogehoge
something
</div>