2020-08-13 11:47:56 +08:00
|
|
|
import tokenizer from './customBlock'
|
|
|
|
import visit from 'unist-util-visit'
|
|
|
|
import { render } from './page'
|
|
|
|
|
|
|
|
const NAME = 'customblock'
|
|
|
|
|
2021-08-25 17:00:13 +08:00
|
|
|
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) => {
|
2021-08-25 17:00:13 +08:00
|
|
|
let { path, data } = file
|
2020-08-13 11:47:56 +08:00
|
|
|
|
|
|
|
visit(tree, NAME, ({ className, value }, index, parent) => {
|
|
|
|
let { contents } = render(value, path, {})
|
2021-08-25 17:00:13 +08:00
|
|
|
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
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|