-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathToken.vue
More file actions
43 lines (37 loc) · 1.24 KB
/
Token.vue
File metadata and controls
43 lines (37 loc) · 1.24 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
<script>
const isAbsent = Symbol()
</script>
<script setup>
import { useSlots, ref, inject, watch, computed } from 'vue'
import { generateSourceCode } from './generate-source.js'
const props = defineProps({
state: null,
component: {
type: null,
default: isAbsent
},
useShiki: {
type: Boolean,
default: true
},
lang: String
})
const usingSlots = computed(() => props.component === isAbsent)
const highlighter = inject('highlighter', null)
const transformCode = (code) => (highlighter && props.useShiki) ? highlighter.codeToHtml(code, { lang: props.lang || 'vue-html' }) : code
const slots = useSlots()
const resultText = ref('')
const updateText = async () => {
if (!usingSlots.value && !props.component) return // bail if the ref hasn't resolved yet
const vnode = usingSlots.value ? slots.default?.() : props.component?.__vnode
const lines = await generateSourceCode(vnode)
const code = lines.join('\n')
resultText.value = transformCode(code)
}
watch(() => props.state, updateText, { deep: true, immediate: true })
if (!usingSlots.value) watch(() => props.component, updateText)
</script>
<template>
<slot :code="resultText" />
<div v-if="usingSlots" v-html="resultText" v-bind="$attrs" class="itsy-token" />
</template>