|
1 | | -import { h } from 'vue' |
2 | 1 | import DefaultTheme from 'vitepress/theme' |
3 | 2 | import type { Theme } from 'vitepress' |
4 | | -import { onMounted, watch, nextTick } from 'vue' |
5 | | -import { useRoute } from 'vitepress' |
6 | 3 | import './custom.css' |
7 | 4 |
|
8 | 5 | export default { |
9 | 6 | extends: DefaultTheme, |
10 | | - Layout: () => { |
11 | | - return h(DefaultTheme.Layout, null, { |
12 | | - // Custom layout slots if needed |
13 | | - }) |
14 | | - }, |
15 | | - enhanceApp() { |
16 | | - // Register global components if needed |
17 | | - // app.component('MyGlobalComponent', MyComponent) |
18 | | - }, |
19 | | - setup() { |
20 | | - const route = useRoute() |
| 7 | + enhanceApp({ router }) { |
| 8 | + if (typeof window !== 'undefined') { |
| 9 | + // Initialize Mermaid when the app starts |
| 10 | + const initMermaid = async () => { |
| 11 | + try { |
| 12 | + console.log('Initializing Mermaid...') |
| 13 | + const { default: mermaid } = await import('mermaid') |
| 14 | + |
| 15 | + mermaid.initialize({ |
| 16 | + startOnLoad: false, |
| 17 | + theme: 'default', |
| 18 | + themeVariables: { |
| 19 | + primaryColor: '#3b82f6', |
| 20 | + primaryTextColor: '#1f2937', |
| 21 | + primaryBorderColor: '#e5e7eb', |
| 22 | + lineColor: '#6b7280', |
| 23 | + secondaryColor: '#f3f4f6', |
| 24 | + tertiaryColor: '#ffffff', |
| 25 | + }, |
| 26 | + }) |
| 27 | + |
| 28 | + console.log('Mermaid initialized successfully') |
21 | 29 |
|
22 | | - const initializeMermaid = async () => { |
23 | | - // Only run on client side |
24 | | - if (typeof window === 'undefined') return |
| 30 | + // Function to render mermaid diagrams |
| 31 | + const renderMermaidDiagrams = async () => { |
| 32 | + const mermaidElements = document.querySelectorAll('pre code.language-mermaid') |
| 33 | + console.log(`Found ${mermaidElements.length} mermaid diagrams to render`) |
| 34 | + |
| 35 | + for (let i = 0; i < mermaidElements.length; i++) { |
| 36 | + const element = mermaidElements[i] as HTMLElement |
| 37 | + |
| 38 | + // Skip if already processed |
| 39 | + if (element.getAttribute('data-mermaid-processed')) { |
| 40 | + console.log(`Skipping already processed diagram ${i}`) |
| 41 | + continue |
| 42 | + } |
| 43 | + |
| 44 | + const graphDefinition = element.textContent || '' |
| 45 | + const graphId = `mermaid-diagram-${Date.now()}-${i}` |
| 46 | + |
| 47 | + console.log(`Processing mermaid diagram ${i}:`, graphDefinition.substring(0, 50) + '...') |
25 | 48 |
|
26 | | - const { default: mermaid } = await import('mermaid') |
27 | | - mermaid.initialize({ |
28 | | - startOnLoad: false, |
29 | | - theme: 'default', |
30 | | - themeVariables: { |
31 | | - primaryColor: '#3b82f6', |
32 | | - primaryTextColor: '#1f2937', |
33 | | - primaryBorderColor: '#e5e7eb', |
34 | | - lineColor: '#6b7280', |
35 | | - secondaryColor: '#f3f4f6', |
36 | | - tertiaryColor: '#ffffff', |
37 | | - }, |
38 | | - }) |
| 49 | + // Mark as being processed |
| 50 | + element.setAttribute('data-mermaid-processed', 'processing') |
39 | 51 |
|
40 | | - // Find all mermaid code blocks and render them |
41 | | - const mermaidElements = document.querySelectorAll( |
42 | | - 'pre code.language-mermaid' |
43 | | - ) |
44 | | - mermaidElements.forEach((element, index) => { |
45 | | - const graphDefinition = element.textContent || '' |
46 | | - const graphId = `mermaid-${Date.now()}-${index}` |
| 52 | + // Create container for the diagram |
| 53 | + const container = document.createElement('div') |
| 54 | + container.className = 'mermaid-diagram' |
| 55 | + container.id = graphId |
47 | 56 |
|
48 | | - // Create a div to hold the rendered diagram |
49 | | - const mermaidDiv = document.createElement('div') |
50 | | - mermaidDiv.id = graphId |
51 | | - mermaidDiv.className = 'mermaid-diagram' |
| 57 | + try { |
| 58 | + // Render the mermaid diagram |
| 59 | + const { svg } = await mermaid.render(graphId, graphDefinition) |
| 60 | + container.innerHTML = svg |
| 61 | + |
| 62 | + // Replace the code block with the rendered diagram |
| 63 | + const preElement = element.parentElement |
| 64 | + if (preElement && preElement.parentElement) { |
| 65 | + preElement.parentElement.replaceChild(container, preElement) |
| 66 | + console.log(`Successfully rendered mermaid diagram ${i}`) |
| 67 | + } |
| 68 | + } catch (error) { |
| 69 | + console.error(`Mermaid rendering failed for diagram ${i}:`, error) |
| 70 | + // Keep the original code block on error |
| 71 | + element.setAttribute('data-mermaid-processed', 'error') |
| 72 | + } |
| 73 | + } |
| 74 | + } |
52 | 75 |
|
53 | | - // Replace the code block with the diagram |
54 | | - element.parentElement?.parentElement?.replaceChild( |
55 | | - mermaidDiv, |
56 | | - element.parentElement |
57 | | - ) |
| 76 | + // Render diagrams on initial load |
| 77 | + renderMermaidDiagrams() |
58 | 78 |
|
59 | | - // Render the diagram |
60 | | - mermaid |
61 | | - .render(graphId, graphDefinition) |
62 | | - .then(({ svg }) => { |
63 | | - mermaidDiv.innerHTML = svg |
64 | | - }) |
65 | | - .catch(error => { |
66 | | - console.error('Mermaid rendering error:', error) |
67 | | - mermaidDiv.innerHTML = `<pre><code>${graphDefinition}</code></pre>` |
68 | | - }) |
69 | | - }) |
70 | | - } |
| 79 | + // Re-render on route changes |
| 80 | + router.onAfterRouteChanged = () => { |
| 81 | + setTimeout(renderMermaidDiagrams, 100) |
| 82 | + } |
71 | 83 |
|
72 | | - onMounted(() => { |
73 | | - initializeMermaid() |
74 | | - }) |
| 84 | + } catch (error) { |
| 85 | + console.error('Failed to initialize Mermaid:', error) |
| 86 | + } |
| 87 | + } |
75 | 88 |
|
76 | | - watch( |
77 | | - () => route.path, |
78 | | - () => nextTick(() => initializeMermaid()), |
79 | | - { immediate: true } |
80 | | - ) |
| 89 | + // Initialize after DOM is ready |
| 90 | + if (document.readyState === 'loading') { |
| 91 | + document.addEventListener('DOMContentLoaded', initMermaid) |
| 92 | + } else { |
| 93 | + initMermaid() |
| 94 | + } |
| 95 | + } |
81 | 96 | }, |
82 | 97 | } satisfies Theme |
0 commit comments