Skip to content

Commit

Permalink
feat: add DemoRender
Browse files Browse the repository at this point in the history
  • Loading branch information
ChuChencheng committed Jul 4, 2024
1 parent 3cbac00 commit dbff840
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 24 deletions.
23 changes: 23 additions & 0 deletions site/.vitepress/components/DemoRender.vue
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<template>
<ClientOnly>
<div
:style="{
width: '100%',
borderRadius: '8px',
padding: '20px',
backgroundColor: '#FFF',
border: '1px solid lightgray',
}"
>
<component :is="vueComponent" />
</div>
</ClientOnly>
</template>

<script setup lang="ts">
const props = defineProps<{
component: string
}>()
const vueComponent = (await import(`../code/${props.component}.vue`)).default
</script>
17 changes: 4 additions & 13 deletions site/.vitepress/components/code-demo.md
Original file line number Diff line number Diff line change
@@ -1,16 +1,6 @@
<ClientOnly>
<div
:style="{
width: '100%',
borderRadius: '8px',
padding: '20px',
backgroundColor: '#FFF',
border: '1px solid lightgray',
}"
>
<component :is="component" />
</div>
</ClientOnly>
<Suspense>
<DemoRender :component="component" />
</Suspense>

<ClientOnly>
<div :style="{ margin: '16px 0' }">
Expand All @@ -28,6 +18,7 @@
import { ref } from 'vue'
import { useData } from 'vitepress'

import DemoRender from './DemoRender.vue'
import PlaygroundLink from './PlaygroundLink.vue'
import { data as sourceCodeMap } from '../data/code.data'
import { codeDemoI18n } from '../constants/i18n'
Expand Down
12 changes: 1 addition & 11 deletions site/.vitepress/theme/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,19 +7,9 @@ import '@wsfe/vue-tree/style.css'

export default {
extends: DefaultTheme,
async enhanceApp({ app }) {
enhanceApp({ app }) {
// 注册自定义全局组件
app.component('CodeDemo', CodeDemo)
app.component('PlaygroundLink', PlaygroundLink)

const codeDemoComponents = import.meta.glob('../code/*.vue')

for (const path in codeDemoComponents) {
const component = await codeDemoComponents[path]()
const componentName = path.match(/^(?:.*\/)?(.+)\.vue$/)?.[1]
if (componentName && component?.default) {
app.component(componentName, component.default)
}
}
}
} satisfies Theme

0 comments on commit dbff840

Please sign in to comment.