markdown-it / markdown-it-container

Fenced container plugin for markdown-it markdown parser
MIT License
496 stars 74 forks source link

Init for multiple similar containers #13

Closed john-cj closed 7 years ago

john-cj commented 7 years ago

Is there a clever way to "repeat" this code for multiple similar containers, so I can create "note", "warning" and "important" asides?

(Currently it works only for "note"). Yes, I understand that I can simply copy and paste all this code block for "warning" and "important" containers (so it will be 30 lines of code instead of current 10), but I think there should be more compact form... Any help is appreciated.

.use(markdownitContainer, 'note', {
    render: function (tokens, idx) {
        var m = tokens[idx].info.trim().match(/^aside\s+(.*)$/);
        if (tokens[idx].nesting === 1) {
            return '<aside class="' + tokens[idx].info.trim() + '">\n';
        } else {
            return '</aside>\n';
        }
    }
})

// ::: note        <aside class="note">
// Text       =>   <p>Text</p>
// :::             </aside>
puzrin commented 7 years ago

You can define outer function, or function fabric. But that's a generic programming question, not related to this package.