-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCodePreview.vue
More file actions
126 lines (107 loc) · 3.43 KB
/
CodePreview.vue
File metadata and controls
126 lines (107 loc) · 3.43 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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
<script setup lang="ts">
import { codeToHtml } from 'shiki'
import { computed, onMounted, ref, resolveComponent, watch } from 'vue'
const props = defineProps<{
path: string
minHeight?: string
previewClasses?: string
prose?: boolean
}>()
const code = ref('')
const highlightedCode = ref('')
const copied = ref(false)
// Extract filename from path for display
const filename = computed(() => {
return props.path.split('/').pop() || 'code.txt'
})
// Determine language from file extension
const language = computed(() => {
const extension = props.path.split('.').pop() || 'text'
const languageMap: Record<string, string> = {
'ts': 'typescript',
'js': 'javascript',
'vue': 'vue',
'html': 'html',
'css': 'css',
'json': 'json',
'md': 'markdown',
'tsx': 'tsx',
'jsx': 'jsx'
}
return languageMap[extension] || 'text'
})
async function loadCodeAndComponent() {
try {
// Get all example files using a glob pattern that includes all possible example directories
const files = import.meta.glob('../../examples/**/*', { as: 'raw', eager: false })
console.log('files', files)
// Normalize the path by removing @/ if present and any leading/trailing slashes
let normalizedPath = props.path
.replace(/^@\//, '')
.replace(/^\/+|\/+$/g, '')
console.log('Looking for file:', normalizedPath)
// Try to find the file with the exact path first
let fileKey = Object.keys(files).find(key =>
key.endsWith(`/${normalizedPath}`)
)
// If not found, try to find it by just the filename
if (!fileKey) {
const filename = normalizedPath.split('/').pop()
fileKey = Object.keys(files).find(key => key.endsWith(`/${filename}`))
}
console.log('Found file at:', fileKey)
if (!fileKey) {
throw new Error(`File not found: ${normalizedPath}. Available files: ${Object.keys(files).slice(0, 10).join(', ')}...`)
}
const loadRaw = files[fileKey]
if (!loadRaw) {
throw new Error(`Failed to load file: ${fileKey}`)
}
code.value = await loadRaw()
highlightedCode.value = await codeToHtml(code.value, {
lang: language.value,
theme: 'github-dark',
})
} catch (error) {
console.error(`Error loading file: ${props.path}`, error)
code.value = `// Error loading file: ${props.path}\n// ${error instanceof Error ? error.message : String(error)}`
highlightedCode.value = await codeToHtml(code.value, {
lang: 'javascript',
theme: 'github-dark',
})
}
}
async function copyCode() {
await navigator.clipboard.writeText(code.value)
copied.value = true
setTimeout(() => (copied.value = false), 1500)
}
onMounted(loadCodeAndComponent)
watch(() => props.path, () => {
loadCodeAndComponent()
})
</script>
<template>
<div class="code-preview my-6">
<div class="relative">
<Badge sm neutral class="absolute top-2 left-2 font-mono text-base-content/50">
{{ filename }}
</Badge>
<Button xs neutral class="absolute top-2 right-4 cursor-pointer" @click="copyCode">
{{ copied ? 'Copied!' : 'Copy' }}
</Button>
<pre class="overflow-x-auto rounded-lg p-4" v-html="highlightedCode" />
</div>
</div>
</template>
<style>
pre.shiki {
/* Diagonal stripes, subtle effect */
background-color: var(--tw-prose-pre-bg) !important;
color: var(--tw-prose-pre-code) !important;
}
pre.shiki code {
display: flex;
flex-direction: column;
}
</style>