UNPKG

remark-container

Version:

markdown container like [markdown-it-container](https://github.com/markdown-it/markdown-it-container)

63 lines (60 loc) 1.66 kB
const defaultOptions = { className: 'remark-container', } function plugin(opt = {}) { const { className } = Object.assign(defaultOptions, opt) const parser = this.Parser const { blockTokenizers, blockMethods, interruptParagraph } = parser.prototype const paragraph = 'paragraph' function tokenizer(eat, value, silent) { const reg = /^\s*:::\s*(\w+)(.*?)[\n\r]([\s\S]+?)\s*:::\s*?/ const match = value.match(reg) if (!match) { /* eslint-disable-next-line */ return false } /* istanbul ignore if - never used (yet) */ if (silent) { return true } // const [input, type, title, content] = match const input = match[0] const type = match[1] const title = match[2] const content = match[3] const start = eat.now() const add = eat(input) const end = eat.now() let children = [ { type: paragraph, children: [ { type: 'text', value: (title || type).trim(), }, ], data: { hProperties: { className: [`${className}-title`] } }, }, ] children = children.concat(this.tokenizeBlock(content.trim(), {})) return add({ type: 'container', children, data: { hName: 'div', hProperties: { className: [className, type.toLowerCase()] }, }, position: { start, end, }, }) } tokenizer.notInList = true tokenizer.notInLink = true blockTokenizers.container = tokenizer blockMethods.splice(blockMethods.indexOf('newline') + 1, 0, 'container') interruptParagraph.unshift(['container']) } module.exports = plugin