|
5 | 5 | :key="equation.id" |
6 | 6 | :class="{ |
7 | 7 | active: equation.id === currentId, |
8 | | - 'new-equation': equation.id === 'new' |
| 8 | + 'new-equation': equation.id === 'new', |
9 | 9 | }" |
10 | 10 | @click="selectEquation(equation.id)" |
11 | 11 | > |
|
15 | 15 | </template> |
16 | 16 |
|
17 | 17 | <script setup lang="ts"> |
18 | | -import { ref, onMounted } from 'vue' |
19 | | -import { parseContent } from '../../utils/parser' |
| 18 | +import { ref, onMounted } from "vue"; |
| 19 | +import { parseContent } from "../../utils/parser"; |
20 | 20 |
|
21 | 21 | interface EquationInfo { |
22 | | - id: string |
23 | | - title: string |
24 | | - content: string |
| 22 | + id: string; |
| 23 | + title: string; |
| 24 | + content: string; |
25 | 25 | } |
26 | 26 |
|
27 | 27 | const emit = defineEmits<{ |
28 | | - change: [markdown: string] |
29 | | -}>() |
| 28 | + change: [markdown: string]; |
| 29 | +}>(); |
30 | 30 |
|
31 | | -const equations = ref<EquationInfo[]>([]) |
32 | | -const currentId = ref('') |
| 31 | +const equations = ref<EquationInfo[]>([]); |
| 32 | +const currentId = ref(""); |
33 | 33 |
|
34 | 34 | // Vite glob import for markdown files |
35 | | -const equationFiles = import.meta.glob('../../examples/*.md', { query: '?raw', import: 'default', eager: true }) |
| 35 | +const equationFiles = import.meta.glob("../../examples/*.md", { |
| 36 | + query: "?raw", |
| 37 | + import: "default", |
| 38 | + eager: true, |
| 39 | +}); |
36 | 40 |
|
37 | 41 | async function loadEquations(): Promise<EquationInfo[]> { |
38 | | - const result: EquationInfo[] = [] |
| 42 | + const result: EquationInfo[] = []; |
39 | 43 |
|
40 | 44 | for (const path in equationFiles) { |
41 | | - const content = equationFiles[path] as string |
42 | | - const filename = path.split('/').pop() || '' |
43 | | - const id = filename.replace('.md', '') |
44 | | - const parsed = parseContent(content) |
| 45 | + const content = equationFiles[path] as string; |
| 46 | + const filename = path.split("/").pop() || ""; |
| 47 | + const id = filename.replace(".md", ""); |
| 48 | + const parsed = parseContent(content); |
45 | 49 |
|
46 | | - result.push({ id, title: parsed.title, content }) |
| 50 | + result.push({ id, title: parsed.title, content }); |
47 | 51 | } |
48 | 52 |
|
49 | 53 | return result.sort((a, b) => { |
50 | | - if (a.id === 'new') return -1 |
51 | | - if (b.id === 'new') return 1 |
52 | | - return a.title.localeCompare(b.title) |
53 | | - }) |
| 54 | + if (a.id === "new") return -1; |
| 55 | + if (b.id === "new") return 1; |
| 56 | + return a.title.localeCompare(b.title); |
| 57 | + }); |
54 | 58 | } |
55 | 59 |
|
56 | 60 | function selectEquation(id: string) { |
57 | | - const equation = equations.value.find(eq => eq.id === id) |
58 | | - if (!equation) return |
| 61 | + const equation = equations.value.find((eq) => eq.id === id); |
| 62 | + if (!equation) return; |
59 | 63 |
|
60 | | - currentId.value = id |
61 | | - window.location.hash = id |
62 | | - emit('change', equation.content) |
| 64 | + currentId.value = id; |
| 65 | + window.location.hash = id; |
| 66 | + emit("change", equation.content); |
63 | 67 | } |
64 | 68 |
|
65 | 69 | function getIdFromHash(): string { |
66 | | - return window.location.hash.slice(1) || '' |
| 70 | + return window.location.hash.slice(1) || ""; |
67 | 71 | } |
68 | 72 |
|
69 | 73 | onMounted(async () => { |
70 | | - equations.value = await loadEquations() |
| 74 | + equations.value = await loadEquations(); |
71 | 75 |
|
72 | 76 | // Get initial equation from URL hash or default |
73 | | - const hashId = getIdFromHash() |
74 | | - const exists = equations.value.some(eq => eq.id === hashId) |
75 | | - const initialId = exists ? hashId : (equations.value[0]?.id ?? '') |
| 77 | + const hashId = getIdFromHash(); |
| 78 | + const exists = equations.value.some((eq) => eq.id === hashId); |
| 79 | + const initialId = exists ? hashId : "schrodinger"; |
76 | 80 |
|
77 | 81 | if (initialId) { |
78 | | - selectEquation(initialId) |
| 82 | + selectEquation(initialId); |
79 | 83 | } |
80 | 84 |
|
81 | 85 | // Listen for hash changes |
82 | | - window.addEventListener('hashchange', () => { |
83 | | - const newId = getIdFromHash() |
| 86 | + window.addEventListener("hashchange", () => { |
| 87 | + const newId = getIdFromHash(); |
84 | 88 | if (newId && newId !== currentId.value) { |
85 | | - selectEquation(newId) |
| 89 | + selectEquation(newId); |
86 | 90 | } |
87 | | - }) |
88 | | -}) |
| 91 | + }); |
| 92 | +}); |
89 | 93 | </script> |
90 | 94 |
|
91 | 95 | <style scoped> |
|
0 commit comments