# Mermaid

Draw diagrams from Mermaid code blocks.

`mermaidPlugin()` renders a `mermaid` code block as a [Mermaid](https://mermaid.js.org) diagram, with a component you write.

## Setup [#setup]

### Add the Plugin [#add-the-plugin-step]

```ts title="vuesho.config.ts"
import { mermaidPlugin } from "vuesho/markdown"; // [!code ++]

createSite(docs.toSource()).plugins(
  recommendedPreset(),
  mermaidPlugin(), // [!code ++]
);
```

### Install Mermaid [#install-mermaid-step]

```npm
npm i mermaid
```

### Create the Component [#create-the-component-step]

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:

```vue title="src/components/content/Mermaid.vue"
<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 [#usage]

Write the diagram in a `mermaid` code block:

````md title="content/build.md"
```mermaid
graph TD
  A[Write] --> B[Build]
```
````

The page's [Markdown version](/ai) keeps the code block, so AI agents can read the diagram as text.
