docs_vue2/one/build/rehype-demo.js

50 lines
1.0 KiB
JavaScript
Raw Permalink Normal View History

2020-08-13 11:47:56 +08:00
import visit from 'unist-util-visit'
import h from 'hastscript'
const RE_DEMO = /^one-demo-[a-f0-9]+/i
export default function attacher () {
return (tree, { data }) => {
visit(tree, 'element', (node, index, parent) => {
let { tagName } = node
let [name] = tagName.match(RE_DEMO) || []
if (name) {
2021-10-25 20:18:05 +08:00
let { code, desc, browser, path } = data.demos[name] || {}
2020-08-13 11:47:56 +08:00
if (!code) {
return
}
let demo = h('one-demo',
{
2021-10-25 20:18:05 +08:00
browser,
path
2020-08-13 11:47:56 +08:00
},
[
node,
h(
'template',
{
slot: 'source'
},
2021-09-25 15:17:57 +08:00
{
type: 'raw',
value: code
}
2020-08-13 11:47:56 +08:00
),
h(
'template',
{
slot: 'desc'
},
{
type: 'raw',
value: desc
}
)
])
parent.children.splice(index, 1, demo)
}
})
}
}