To render Astro Mermaid diagrams, add mermaid to markdown.syntaxHighlight.excludeLangs so Shiki leaves the code blocks alone, then load Mermaid in a small <script> that converts each language-mermaid block into a diagram. Pages without diagrams never download Mermaid, because the script imports it only when it finds a block.
Key Takeaways
- Write diagrams in fenced code blocks with the
mermaidlanguage.excludeLangs: ['mermaid', 'math']stops Shiki from turning them into highlighted code.- A dynamic
import('mermaid')keeps the library off pages with no diagrams.- Build-time rendering to SVG is possible but needs a headless browser in your build.
Write a diagram
<!-- src/content/blog/deploy-flow.md -->
```mermaid
flowchart LR
A[Push to main] --> B[CI build]
B --> C{Tests pass?}
C -- yes --> D[Deploy]
C -- no --> E[Notify]
```
Exclude it from syntax highlighting
// astro.config.mjs
import { defineConfig } from 'astro/config';
export default defineConfig({
markdown: {
syntaxHighlight: {
type: 'shiki',
excludeLangs: ['mermaid', 'math'],
},
},
});
Keep math in the list; it is in the default and you lose it if you omit it. Excluded blocks render as plain <pre><code class="language-mermaid">.
Render on the client
npm install mermaid
---
// src/components/Mermaid.astro
---
<script>
async function renderMermaid() {
const blocks = document.querySelectorAll<HTMLElement>('pre > code.language-mermaid');
if (blocks.length === 0) return;
const { default: mermaid } = await import('mermaid');
blocks.forEach((code) => {
const div = document.createElement('div');
div.className = 'mermaid';
div.textContent = code.textContent ?? '';
code.parentElement?.replaceWith(div);
});
const dark = matchMedia('(prefers-color-scheme: dark)').matches;
mermaid.initialize({ startOnLoad: false, theme: dark ? 'dark' : 'default' });
await mermaid.run({ querySelector: '.mermaid' });
}
renderMermaid();
</script>
Add <Mermaid /> to your post layout. Astro bundles the script, and Vite splits Mermaid into its own chunk that loads only when import() runs. If you use <ClientRouter />, call renderMermaid() on the astro:page-load event instead of once; our script rerun tip shows the pattern.
Reserve space to limit layout shift while the diagram renders:
/* src/styles/global.css */
pre:has(> code.language-mermaid), .mermaid {
min-height: 12rem;
}
Render at build time instead
Client rendering is simple but ships a large library and shows code briefly before the diagram appears. To output static SVG during the build, use a rehype plugin such as rehype-mermaid with the unified() processor. It renders with a headless browser (Playwright), so your build machine and CI need it installed:
// astro.config.mjs
import { defineConfig } from 'astro/config';
import { unified } from '@astrojs/markdown-remark';
import rehypeMermaid from 'rehype-mermaid';
export default defineConfig({
markdown: {
syntaxHighlight: { type: 'shiki', excludeLangs: ['mermaid', 'math'] },
processor: unified({ rehypePlugins: [[rehypeMermaid, { strategy: 'inline-svg' }]] }),
},
});
Choose build time for docs sites with many diagrams and a CI you control; choose the client script for occasional diagrams. The Mermaid syntax docs list every diagram type, our remark and rehype plugins guide covers the processor, and our documentation themes suit diagram-heavy content. See the content docs for where Markdown settings live.
FAQ
Does Astro support Mermaid diagrams?
Not built in, but it takes a few lines. Exclude mermaid from Shiki with excludeLangs, then load Mermaid with a client script, or render diagrams at build time with a rehype plugin.
Why does my Mermaid code block show as highlighted code?
Shiki is highlighting it. Add mermaid to markdown.syntaxHighlight.excludeLangs so the block is left as plain code for Mermaid to find.
Can I use Mermaid in MDX in Astro?
Yes. MDX uses the same syntax highlighting settings, so fenced mermaid blocks in .mdx files work with the same script.