miaopanel
1<script setup lang="ts">
2import { computed } from 'vue'
3
4const props = defineProps<{ text: string }>()
5
6function escapeHtml(input: string): string {
7 return input
8 .replace(/&/g, '&')
9 .replace(/</g, '<')
10 .replace(/>/g, '>')
11 .replace(/"/g, '"')
12}
13
14function renderInline(input: string): string {
15 let out = escapeHtml(input)
16 out = out.replace(/`([^`\n]+)`/g, '<code>$1</code>')
17 out = out.replace(/\*\*([^*\n]+)\*\*/g, '<b>$1</b>')
18 return out.replace(/\n/g, '<br>')
19}
20
21/** Minimal markdown: code blocks, inline code, bold and line breaks. */
22const html = computed(() => {
23 const source = props.text ?? ''
24 const parts = source.split('```')
25 return parts
26 .map((part, index) => {
27 if (index % 2 === 1) {
28 const lines = part.split('\n')
29 if (lines[0] && !lines[0].includes(' ') && lines[0].length < 20) lines.shift()
30 return `<pre class="md-code">${escapeHtml(lines.join('\n'))}</pre>`
31 }
32 return renderInline(part)
33 })
34 .join('')
35})
36</script>
37
38<template>
39 <div class="md" v-html="html"></div>
40</template>
41
42<style scoped>
43.md {
44 line-height: 1.7;
45 word-break: break-word;
46}
47.md :deep(.md-code) {
48 margin: 8px 0;
49 padding: 10px 12px;
50 border-radius: 10px;
51 background: #f2faf8;
52 border: 1px solid var(--line);
53 font-family: 'JetBrains Mono', Consolas, monospace;
54 font-size: 12.5px;
55 white-space: pre-wrap;
56 overflow-x: auto;
57}
58.md :deep(code) {
59 padding: 1px 4px;
60 border-radius: 5px;
61 background: #eef9f6;
62 font-family: 'JetBrains Mono', Consolas, monospace;
63 font-size: 12.5px;
64}
65</style>