Skip to content

Commit bf58270

Browse files
committed
Add lazy-loaded Mermaid initialization to Layout
- Lazy load mermaid module only on pages with diagrams - Configure with Egyptian design system theme matching astro.config.mjs - Handle View Transitions by removing data-processed attributes - Support Statsbomb case study technical diagrams
1 parent 74ac956 commit bf58270

File tree

1 file changed

+57
-0
lines changed

1 file changed

+57
-0
lines changed

src/layouts/Layout.astro

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,63 @@ const ogImageURL = new URL(ogImage, Astro.site);
130130
headroomInstance.init();
131131
});
132132
</script>
133+
134+
<!-- Lazy load Mermaid only on pages with diagrams -->
135+
<script>
136+
import type { MermaidConfig } from 'mermaid';
137+
138+
// Track if mermaid has been loaded to avoid duplicate imports
139+
let mermaidModule: typeof import('mermaid') | null = null;
140+
141+
const initMermaid = async () => {
142+
// Check if page has any Mermaid diagrams
143+
const hasMermaid = document.querySelector('.mermaid-wrapper, pre.mermaid, code.mermaid');
144+
if (!hasMermaid) return;
145+
146+
// Lazy load mermaid module only when needed
147+
if (!mermaidModule) {
148+
mermaidModule = await import('mermaid');
149+
150+
// Initialize with Egyptian design system config (matching astro.config.mjs)
151+
const config: MermaidConfig = {
152+
startOnLoad: false,
153+
theme: 'base',
154+
themeVariables: {
155+
primaryColor: '#F5F1E8',
156+
primaryBorderColor: '#CBD5E1',
157+
primaryTextColor: '#0F172A',
158+
fontFamily: 'Inter, system-ui, sans-serif',
159+
fontSize: '14px',
160+
lineColor: '#475569',
161+
edgeLabelBackground: '#FEFDFB',
162+
clusterBkg: '#FEFDFB',
163+
clusterBorder: '#CBD5E1',
164+
secondaryColor: '#DBEAFE',
165+
tertiaryColor: '#FEF3C7',
166+
},
167+
flowchart: {
168+
htmlLabels: true,
169+
titleTopMargin: 12,
170+
subGraphTitleMargin: {
171+
top: 12,
172+
bottom: 6
173+
}
174+
}
175+
};
176+
mermaidModule.default.initialize(config);
177+
}
178+
179+
// Remove data-processed attributes to allow re-rendering on View Transitions
180+
const diagrams = document.querySelectorAll('.mermaid-wrapper [data-processed], pre.mermaid[data-processed], code.mermaid[data-processed]');
181+
diagrams.forEach(diagram => diagram.removeAttribute('data-processed'));
182+
183+
// Render diagrams
184+
await mermaidModule.default.run();
185+
};
186+
187+
// Initialize on page load (handles both initial load and View Transitions)
188+
document.addEventListener('astro:page-load', initMermaid);
189+
</script>
133190
</head>
134191
<body class="bg-background text-text antialiased">
135192
<!-- Scroll Progress Indicator (long-form content pages only) -->

0 commit comments

Comments
 (0)