|
1 | | -import { h } from 'vue' |
2 | | -import DefaultTheme from 'vitepress/theme' |
3 | | -import type { Theme } from 'vitepress' |
| 1 | +import DefaultTheme from 'vitepress/theme'; |
| 2 | +import type { Theme } from 'vitepress'; |
| 3 | +import './custom.css'; |
4 | 4 |
|
5 | 5 | export default { |
6 | 6 | extends: DefaultTheme, |
7 | | - Layout: () => { |
8 | | - return h(DefaultTheme.Layout, null, { |
9 | | - // Custom layout slots if needed |
10 | | - }) |
11 | | - }, |
12 | | - enhanceApp() { |
13 | | - // Register global components if needed |
14 | | - // app.component('MyGlobalComponent', MyComponent) |
| 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'); |
| 29 | + |
| 30 | + // Function to render mermaid diagrams |
| 31 | + const renderMermaidDiagrams = async () => { |
| 32 | + // Try multiple selectors to find mermaid code blocks |
| 33 | + let mermaidElements = document.querySelectorAll( |
| 34 | + 'pre code.language-mermaid' |
| 35 | + ); |
| 36 | + |
| 37 | + // If not found, try alternative selectors |
| 38 | + if (mermaidElements.length === 0) { |
| 39 | + mermaidElements = document.querySelectorAll( |
| 40 | + 'code.language-mermaid' |
| 41 | + ); |
| 42 | + } |
| 43 | + if (mermaidElements.length === 0) { |
| 44 | + mermaidElements = document.querySelectorAll( |
| 45 | + 'pre[class*="language-mermaid"] code' |
| 46 | + ); |
| 47 | + } |
| 48 | + if (mermaidElements.length === 0) { |
| 49 | + mermaidElements = document.querySelectorAll( |
| 50 | + '[class*="language-mermaid"]' |
| 51 | + ); |
| 52 | + } |
| 53 | + |
| 54 | + console.log( |
| 55 | + `Found ${mermaidElements.length} mermaid diagrams to render` |
| 56 | + ); |
| 57 | + |
| 58 | + for (let i = 0; i < mermaidElements.length; i++) { |
| 59 | + const element = mermaidElements[i] as HTMLElement; |
| 60 | + |
| 61 | + // Skip if already processed |
| 62 | + if (element.getAttribute('data-mermaid-processed')) { |
| 63 | + continue; |
| 64 | + } |
| 65 | + |
| 66 | + let graphDefinition = element.textContent || ''; |
| 67 | + |
| 68 | + // Clean up the text content - remove "mermaid" prefix if present |
| 69 | + if (graphDefinition.startsWith('mermaid')) { |
| 70 | + graphDefinition = graphDefinition.substring(7).trim(); |
| 71 | + } |
| 72 | + |
| 73 | + const graphId = `mermaid-diagram-${Date.now()}-${i}`; |
| 74 | + |
| 75 | + // Mark as being processed |
| 76 | + element.setAttribute('data-mermaid-processed', 'processing'); |
| 77 | + |
| 78 | + // Create container for the diagram |
| 79 | + const container = document.createElement('div'); |
| 80 | + container.className = 'mermaid-diagram'; |
| 81 | + container.id = graphId; |
| 82 | + |
| 83 | + try { |
| 84 | + // Render the mermaid diagram |
| 85 | + const { svg } = await mermaid.render(graphId, graphDefinition); |
| 86 | + container.innerHTML = svg; |
| 87 | + |
| 88 | + // Find the correct parent element to replace (the <pre> element) |
| 89 | + const preElement = element.parentElement; // This should be <pre> |
| 90 | + console.log('Element structure:', { |
| 91 | + element: element.tagName, |
| 92 | + parent: preElement?.tagName, |
| 93 | + grandparent: preElement?.parentElement?.tagName, |
| 94 | + }); |
| 95 | + |
| 96 | + if (preElement && preElement.tagName === 'PRE') { |
| 97 | + // Replace only the <pre> element, not its parent |
| 98 | + preElement.parentElement?.replaceChild(container, preElement); |
| 99 | + } else { |
| 100 | + // Fallback: replace the element itself if structure is different |
| 101 | + element.parentElement?.replaceChild(container, element); |
| 102 | + } |
| 103 | + } catch (error) { |
| 104 | + console.error('Mermaid rendering failed:', error); |
| 105 | + // Keep the original code block on error |
| 106 | + element.setAttribute('data-mermaid-processed', 'error'); |
| 107 | + } |
| 108 | + } |
| 109 | + }; |
| 110 | + |
| 111 | + // Render diagrams on initial load |
| 112 | + renderMermaidDiagrams(); |
| 113 | + |
| 114 | + // Re-render on route changes |
| 115 | + router.onAfterRouteChange = () => { |
| 116 | + setTimeout(renderMermaidDiagrams, 100); |
| 117 | + }; |
| 118 | + } catch (error) { |
| 119 | + console.error('Failed to initialize Mermaid:', error); |
| 120 | + } |
| 121 | + }; |
| 122 | + |
| 123 | + // Initialize after DOM is ready |
| 124 | + if (document.readyState === 'loading') { |
| 125 | + document.addEventListener('DOMContentLoaded', initMermaid); |
| 126 | + } else { |
| 127 | + initMermaid(); |
| 128 | + } |
| 129 | + } |
15 | 130 | }, |
16 | | -} satisfies Theme |
| 131 | +} satisfies Theme; |
0 commit comments