|
| 1 | +--- |
| 2 | +title: "Mermaid in Astro" |
| 3 | +slug: mermaid-in-astro |
| 4 | +tags: [site, web] |
| 5 | +date: 2025-03-20 21:20:00 |
| 6 | +--- |
| 7 | + |
| 8 | +<Info icon={false}> |
| 9 | +* [mermaid][1] is a library that allows you to create a variety of diagrams and charts with a relatively simple markdown syntax |
| 10 | +* [Astro][2] is a web framework to build static sites as well as sites with interaction islands. Think Jekyll, but for 2025. |
| 11 | +</Info> |
| 12 | + |
| 13 | +The [release notes of Astro 5.5][3] explicitly mentioned that it should now be much easier to integrate mermaid-based |
| 14 | +diagramming into Astro sites, which immediately peaked my interest as this was something I thought of as nice-to-have |
| 15 | +to support some technical discussion e.g. with a flowchart or a sequence diagram. |
| 16 | + |
| 17 | +Setting it up was not that difficult, yet it still presented me with some gotchas that you may also trip over as a novice |
| 18 | +in those particular technologies. Hence this post essentially serves as a documentation what I had to do. |
| 19 | +Let's start with an overview: |
| 20 | + |
| 21 | +```mermaid |
| 22 | +graph TD |
| 23 | + S((Start)) --> A[Ensure Astro 5.5+] |
| 24 | + A --> B[Install necessary dependencies] |
| 25 | + B --> C[Change Astro configuration] |
| 26 | + C --> D{Need to deploy?} |
| 27 | + D -- Yes --> E[Make sure Playwright gets installed] |
| 28 | + D -- No --> F((Finished)) |
| 29 | + E --> F |
| 30 | +``` |
| 31 | + |
| 32 | +## Ensure Astro 5.5+ |
| 33 | + |
| 34 | +Piece of cake (assuming you already have an Astro site going): |
| 35 | +```bash |
| 36 | +npx @astrojs/upgrade |
| 37 | +``` |
| 38 | + |
| 39 | +## Install necessary dependencies |
| 40 | + |
| 41 | +The following packages were relevant: |
| 42 | + |
| 43 | +```json title="package.json" ins={5-6} |
| 44 | + "dependencies": { |
| 45 | + "astro": "^5.5.3", |
| 46 | + "@astrojs/mdx": "^4.2.1", |
| 47 | + "...": "...", |
| 48 | + "playwright": "^1.51.1", |
| 49 | + "rehype-mermaid": "^3.0.0" |
| 50 | + } |
| 51 | +``` |
| 52 | + |
| 53 | +[playwright][5] is apparently necessary to be able to create the images generated from mermaid markdown statically, i.e. |
| 54 | +not directly in the browser of the visitor hitting realfiction.net. |
| 55 | + |
| 56 | +Also, make sure playwright can work correctly: |
| 57 | + |
| 58 | +```bash |
| 59 | +npx playwright install --with-deps chromium |
| 60 | +``` |
| 61 | + |
| 62 | +## Change Astro configuration |
| 63 | + |
| 64 | +Time to touch the site's configuration: |
| 65 | + |
| 66 | +```javascript title="astro-config.mjs" ins={1,8,10} |
| 67 | +import rehypeMermaid from "rehype-mermaid"; |
| 68 | + |
| 69 | +export default defineConfig({ |
| 70 | + ... |
| 71 | + markdown: { |
| 72 | + syntaxHighlight: { |
| 73 | + type: "shiki", |
| 74 | + excludeLangs: ["mermaid"] |
| 75 | + }, |
| 76 | + rehypePlugins: [[rehypeMermaid, { strategy: "img-svg", dark: true, colorScheme: "forest" }]] |
| 77 | + }, |
| 78 | + integrations: [ |
| 79 | + ... |
| 80 | + mdx(), |
| 81 | + sitemap(), |
| 82 | + ], |
| 83 | +}); |
| 84 | + |
| 85 | +``` |
| 86 | + |
| 87 | +Check out the [rehype mermaid repo][4] to see what options are available to you. |
| 88 | + |
| 89 | +## Deployment? Make sure Playwright gets installed correctly |
| 90 | + |
| 91 | +The final stumbling block was the actual deployment that I do via github actions. Here it is necessary to also ensure |
| 92 | +that playwright will work correctly. |
| 93 | + |
| 94 | +```yaml title="deploy.yml" ins={7-8} |
| 95 | +jobs: |
| 96 | + build: |
| 97 | + runs-on: ubuntu-latest |
| 98 | + steps: |
| 99 | + - name: Checkout your repository using git |
| 100 | + uses: actions/checkout@v4 |
| 101 | + - name: Get playwright for mermaid stuff |
| 102 | + run: npx playwright install --with-deps chromium |
| 103 | + - name: Install, build, and upload your site |
| 104 | + uses: withastro/action@v2 |
| 105 | +``` |
| 106 | +
|
| 107 | +## Conclusion |
| 108 | +
|
| 109 | +and this is pretty much it! Of course the generated diagrams may not win beauty contests, but dark mode is supported |
| 110 | +and it is a pretty easy workflow to eg tell chat gpt to make some mermaid workflow from an informal description and then |
| 111 | +paste the text here. Btw, the above workflow looks like this in the workflow: |
| 112 | +
|
| 113 | +```` |
| 114 | +```mermaid |
| 115 | +graph TD |
| 116 | + S((Start)) --> A[Ensure Astro 5.5+] |
| 117 | + A --> B[Install necessary dependencies] |
| 118 | + B --> C[Change Astro configuration] |
| 119 | + C --> D{Need to deploy?} |
| 120 | + D -- Yes --> E[Make sure Playwright gets installed] |
| 121 | + D -- No --> F((Finished)) |
| 122 | + E --> F |
| 123 | +``` |
| 124 | +```` |
| 125 | +
|
| 126 | +[1]: https://mermaid.js.org |
| 127 | +[2]: https://astro.build |
| 128 | +[3]: https://astro.build/blog/astro-550/ |
| 129 | +[4]: https://github.com/remcohaszing/rehype-mermaid |
| 130 | +[5]: https://playwright.dev |
0 commit comments