Mermaid
Draw diagrams from Mermaid code blocks.
mermaidPlugin() renders a mermaid code block as a Mermaid diagram, with a component you write.
Setup
Add the Plugin
import { mermaidPlugin } from "vuesho/markdown";
createSite(docs.toSource()).plugins(
recommendedPreset(),
mermaidPlugin(),
);Install Mermaid
npm i mermaidCreate the Component
The plugin renders each code block as <Mermaid chart="…" />. The component below loads Mermaid in the browser on pages with a diagram, and redraws the diagram when the color mode changes:
<script setup lang="ts">
import { onMounted, ref, useId, watch } from "vue";
import { useColorMode } from "vuesho/client";
const props = defineProps<{ chart: string }>();
const id = useId();
const svg = ref("");
const { isDark } = useColorMode();
onMounted(() => {
watch(
[() => props.chart, isDark],
async ([chart, dark]) => {
const { default: mermaid } = await import("mermaid");
mermaid.initialize({ startOnLoad: false, theme: dark ? "dark" : "default" });
svg.value = (await mermaid.render(`mermaid-${id}`, chart)).svg;
},
{ immediate: true },
);
});
</script>
<template>
<div v-html="svg" />
</template>Usage
Write the diagram in a mermaid code block:
```mermaid
graph TD
A[Write] --> B[Build]
```The page's Markdown version keeps the code block, so AI agents can read the diagram as text.