Skip to content

Commit bb51862

Browse files
committed
Render Mermaid diagrams at build time for blog posts
Add a remark-based Mermaid renderer that converts MDX mermaid fences into static light/dark SVG, document the workflow, and draft a new homelab post that uses the format. Also tighten package-manager guidance around the new pnpm-based dependency additions. Constraint: The blog is a static Astro site and should not ship Mermaid runtime code to browsers Rejected: Client-side Mermaid runtime injection | adds JS weight and delays diagram rendering until after hydration Confidence: high Scope-risk: moderate Directive: Keep Mermaid rendering build-time only with strict security settings; do not introduce browser-side Mermaid unless the static approach is demonstrably insufficient Tested: pnpm install --frozen-lockfile Tested: pnpm exec astro check Tested: pnpm build Not-tested: Published rendering of the new homelab post because frontmatter keeps publish=false
1 parent a273d41 commit bb51862

11 files changed

Lines changed: 2982 additions & 25 deletions

File tree

AGENTS.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,20 @@ This file provides guidance to Claude Code (claude.ai/code) when working with co
55
## Commands
66

77
```bash
8+
pnpm install # Install dependencies and update pnpm-lock.yaml
89
pnpm dev # Start dev server at http://localhost:4321
910
pnpm build # prebuild (copy assets) → astro build → pagefind index
1011
pnpm preview # Preview production build locally
1112
```
1213

14+
## Package manager
15+
16+
This repository uses `pnpm` only.
17+
18+
- Use `pnpm install`, `pnpm add`, `pnpm remove`, and related `pnpm` commands for dependency changes.
19+
- Commit `pnpm-lock.yaml` whenever dependencies change.
20+
- Do not run `bun install` in this repo or commit `bun.lock`; GitHub Actions installs with `pnpm install --frozen-lockfile`.
21+
1322
**Search requires a prior build.** In dev mode, `/pagefind/` requests are proxied to `dist/` by a custom Vite middleware — run `pnpm build` at least once before testing search in dev.
1423

1524
## Architecture

CLAUDE.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,5 @@
11
@AGENTS.md
2+
3+
## Package manager
4+
5+
This repository uses `pnpm` only. Use `pnpm install`, `pnpm add`, and `pnpm remove`, keep `pnpm-lock.yaml` in sync with dependency changes, and do not create or commit `bun.lock`.

README.md

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,14 @@ pnpm dev
2828

2929
Open [http://localhost:4321](http://localhost:4321).
3030

31+
## Package Manager
32+
33+
This repository uses `pnpm` only.
34+
35+
- Use `pnpm install`, `pnpm add`, `pnpm remove`, and other `pnpm` commands for dependency changes.
36+
- Commit `pnpm-lock.yaml` whenever dependencies change.
37+
- Do not run `bun install` in this repo or commit `bun.lock`; deployment uses `pnpm install --frozen-lockfile`.
38+
3139
## Deploy to GitHub Pages
3240

3341
1. Push to `main` branch
@@ -53,6 +61,20 @@ src/content/posts/YYYY-MM-DD-slug/
5361
└── image.png # Co-located assets (optional)
5462
```
5563

64+
## Mermaid Diagram
65+
66+
Mermaid diagrams are rendered to static SVG during the build. Use a normal fenced code block with the `mermaid` language tag.
67+
68+
````mdx
69+
```mermaid
70+
flowchart TD
71+
A[Write Mermaid code in MDX] --> B[Astro build renders SVG]
72+
B --> C[No Mermaid runtime shipped to the browser]
73+
```
74+
````
75+
76+
The site renders light and dark SVG variants at build time and toggles them with CSS, so no Mermaid client bundle is needed.
77+
5678
## License
5779

5880
MIT

astro.config.mjs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import sitemap from "@astrojs/sitemap";
33
import tailwindcss from "@tailwindcss/vite";
44
import { transformerNotationDiff, transformerNotationHighlight } from "@shikijs/transformers";
55
import { defineConfig } from "astro/config";
6+
import remarkRenderMermaid from "./scripts/remark-render-mermaid.mjs";
67
import remarkRewritePostAssets from "./scripts/remark-rewrite-post-assets.mjs";
78

89
function ghPagesConfig() {
@@ -18,7 +19,7 @@ function ghPagesConfig() {
1819
export default defineConfig({
1920
site: "https://clickin.github.io",
2021
integrations: [
21-
mdx({ remarkPlugins: [remarkRewritePostAssets] }),
22+
mdx({ remarkPlugins: [remarkRenderMermaid, remarkRewritePostAssets] }),
2223
sitemap({
2324
filter: (page) => !page.includes("/blog/tag/"),
2425
}),
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
# Mermaid Introduction Design
2+
3+
## Goal
4+
5+
Add Mermaid support to MDX posts without shipping Mermaid runtime code to the browser.
6+
7+
## Chosen Approach
8+
9+
Use a remark-based build renderer that converts ` ```mermaid ` code fences into static SVG during the Astro build via `@mermaid-js/mermaid-cli`.
10+
11+
## Why This Approach
12+
13+
- The final HTML contains only SVG, which matches the blog's static-build and SEO goals.
14+
- No Mermaid runtime bundle is shipped to the browser.
15+
- Theme switching can be handled with pre-rendered light/dark SVG variants and CSS only.
16+
17+
## Safety Constraints
18+
19+
- Keep Mermaid on `securityLevel: "strict"`.
20+
- Render diagrams only at build time.
21+
- Avoid client-side HTML reparsing entirely.
22+
23+
## Verification
24+
25+
- `pnpm install --frozen-lockfile` must succeed after the dependency and lockfile are updated.
26+
- `pnpm build` must succeed.
27+
- A `mermaid` code fence must be replaced with static SVG during build.

package.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,9 @@
2626
},
2727
"devDependencies": {
2828
"@astrojs/check": "^0.9.6",
29+
"@mermaid-js/mermaid-cli": "^11.12.0",
2930
"pagefind": "1.5.0-beta.1",
31+
"puppeteer": "^23.11.1",
3032
"sharp": "^0.34.5",
3133
"typescript": "^5.9.3"
3234
}

0 commit comments

Comments
 (0)