shadcn-vue/apps/www/.vitepress/theme/plugins/previewer.ts
zernonia 72f9bd5ef5
refactor: code preview (#411)
* feat: generate code dynamically

* chore: cleanup and transform path on component

* feat: create config sheet

* feat: code wrapper

* fix: not acting immediately

* chore: add key to vnode

* chore: add vue-sonner to demos dependencies, add placeholder for codeConfigs

* chore: fix wrong icons

* chore: improve crawling logic

---------

Co-authored-by: Sadegh Barati <sadeghbaratiwork@gmail.com>
2024-03-14 18:28:13 +08:00

22 lines
803 B
TypeScript

import type { MarkdownRenderer } from 'vitepress'
import { parseProps } from './utils'
export default function (md: MarkdownRenderer) {
function addRenderRule(type: string) {
const defaultRender = md.renderer.rules[type]
md.renderer.rules[type] = (tokens, idx, options, env, self) => {
const token = tokens[idx]
const content = token.content.trim()
if (!content.match(/^<ComponentPreview\s/) || !content.endsWith('/>'))
return defaultRender!(tokens, idx, options, env, self)
const props = parseProps(content)
const { attrs } = props
const demoScripts = `<ComponentPreview ${attrs ?? ''} v-bind='${JSON.stringify(props)}'></ComponentPreview>`.trim()
return demoScripts
}
}
addRenderRule('html_block')
addRenderRule('html_inline')
}