|
82 | 82 |
|
83 | 83 | <script setup> |
84 | 84 | import { computed, ref, watch } from "vue"; |
85 | | -import { |
86 | | - highlightCpp, |
87 | | - highlightShell, |
88 | | - highlightText, |
89 | | - normalizeLang, |
90 | | -} from "./highlighter"; |
| 85 | +import { highlight, normalizeLang } from "./highlighter"; |
91 | 86 |
|
92 | 87 | const props = defineProps({ |
93 | 88 | title: { type: String, default: "" }, |
@@ -155,21 +150,32 @@ function guessLang(tabKey) { |
155 | 150 | if (props.lang) return props.lang; |
156 | 151 | if (tabKey === "run" || tabKey === "out") return "shell"; |
157 | 152 | const s = (props.code || "").trim(); |
| 153 | + // C++ |
158 | 154 | if (s.includes("#include") || s.includes("int main") || s.includes("std::")) |
159 | 155 | return "cpp"; |
| 156 | + // Shell |
160 | 157 | if (s.startsWith("~$") || s.includes(" vix ") || s.startsWith("$ ")) |
161 | 158 | return "shell"; |
| 159 | + // HTML / Vue |
| 160 | + if ( |
| 161 | + /^\s*<(template|div|span|section|html|!DOCTYPE|script|style)\b/i.test(s) || |
| 162 | + /<\/[a-z]+>/.test(s) |
| 163 | + ) |
| 164 | + return "html"; |
| 165 | + // CSS |
| 166 | + if (/[.#&][\w-]+\s*\{/.test(s) || /^\s*@(media|import|keyframes)/m.test(s)) |
| 167 | + return "css"; |
| 168 | + // JSON |
| 169 | + if (/^\s*[{\[]/.test(s) && /["}\]]\s*$/.test(s) && /":/.test(s)) |
| 170 | + return "json"; |
| 171 | + // JS / TS |
| 172 | + if (/\b(const|let|function|import|export|=>)\b/.test(s)) return "js"; |
162 | 173 | return "cpp"; |
163 | 174 | } |
164 | 175 |
|
165 | | -const activeHtml = computed(() => { |
166 | | - const text = activeText.value || ""; |
167 | | - const lang = normalizeLang(activeLang.value); |
168 | | -
|
169 | | - if (lang === "shell") return highlightShell(text); |
170 | | - if (lang === "cpp") return highlightCpp(text); |
171 | | - return highlightText(text); |
172 | | -}); |
| 176 | +const activeHtml = computed(() => |
| 177 | + highlight(activeText.value || "", normalizeLang(activeLang.value)), |
| 178 | +); |
173 | 179 |
|
174 | 180 | async function copy(text) { |
175 | 181 | try { |
|
0 commit comments