docs_vue2/one/build/remark-custom.js

46 lines
1.2 KiB
JavaScript
Raw Normal View History

2021-09-25 15:17:57 +08:00
import tokenizer from './custom-block'
2020-08-13 11:47:56 +08:00
import visit from 'unist-util-visit'
import { render } from './page'
const NAME = 'customblock'
const typeMap = {
tip: 'info',
warning: 'warning'
}
2020-08-13 11:47:56 +08:00
export default function attacher () {
let proto = this.Parser.prototype
proto.blockTokenizers[NAME] = tokenizer
proto.interruptParagraph.push([NAME])
proto.interruptList.push([NAME])
proto.interruptBlockquote.push([NAME])
let methods = proto.blockMethods
methods.unshift(NAME)
return (tree, file) => {
let { path, data } = file
2020-08-13 11:47:56 +08:00
visit(tree, NAME, ({ className, value }, index, parent) => {
let { contents } = render(value, path, {})
if (typeMap[className]) {
if (!data.hasAlert) {
data.hasAlert = true
}
parent.children.splice(index, 1, {
type: 'html',
value: `<veui-alert ui="s" type="${typeMap[className]}">${contents}</veui-alert>`
})
} else {
className = className ? `${className} custom-block` : 'custom-block'
parent.children.splice(index, 1, {
type: 'html',
value: `<div class="${className}">${contents}</div>`
})
}
2020-08-13 11:47:56 +08:00
})
}
}