Skip to content

Add Mermaid diagram support - #5

Merged
cursor[bot] merged 13 commits into
mainfrom
t/mermaid-diagram-support-8185
Jul 29, 2026
Merged

Add Mermaid diagram support#5
cursor[bot] merged 13 commits into
mainfrom
t/mermaid-diagram-support-8185

Conversation

@0xtlt

@0xtlt 0xtlt commented Jul 29, 2026

Copy link
Copy Markdown
Owner

Summary

md2pdf previously treated mermaid fences as plain code. This PR adds in-process Mermaid rendering so diagrams appear as images in the PDF.

  • Detect mermaid / mmd fenced blocks during Markdown conversion
  • Render them to SVG with mermaid-rs-renderer (pure Rust, no browser/Node)
  • Crop excess Mermaid canvas padding without clipping diamonds/circles
  • Size simple diagrams compactly and complex diagrams more generously for readability
  • Force DejaVu Sans so Typst can render diagram labels with embedded fonts
  • Always use Mermaid's classic light palette (independent of --code-theme) so diagrams match the light PDF page
  • Fail conversion with a clear error when Mermaid source is invalid

Review follow-ups included

  • Hardened SVG attribute parsing (x vs rx, width vs stroke-width)
  • Skip tiny transform-local marker circles during crop
  • Clamp crop size so it cannot expand the original canvas
  • Added padded-diamond crop regression test
  • Hard-coded light Mermaid colors + dark-canvas rewrite guard
  • Cache-busted README preview (preview-v2.png) so the old dark diagram image is not served from CDN

Testing

  • cargo fmt --check
  • cargo clippy --all-targets --locked -- -D warnings
  • cargo test --locked
  • Manual: complex architecture / class / sequence example PDF
  • Regression: flowchart diamonds stay inside cropped viewBox
  • Regression: Mermaid SVG stays light even when --code-theme dark
Open in Web Open in Cursor 

cursoragent and others added 12 commits July 29, 2026 10:57
Render ```mermaid / ```mmd fences to SVG with mermaid-rs-renderer and
embed them as virtual Typst assets, keeping PDF generation browser-free.

Co-authored-by: Thomas <0xtlt@users.noreply.github.com>
Typst resolves a single SVG font family, so use the embedded DejaVu face
instead of the Mermaid Inter/system stack that would drop diagram labels.

Co-authored-by: Thomas <0xtlt@users.noreply.github.com>
Crop excess Mermaid canvas padding and size SVGs from CSS pixels into the
printable page box so diagrams are neither tiny nor oversized.

Co-authored-by: Thomas <0xtlt@users.noreply.github.com>
Use denser CSS-pixel conversion and a tighter page box so diagrams stay
closer to body-text size instead of dominating the page.

Co-authored-by: Thomas <0xtlt@users.noreply.github.com>
Keep simple diagrams compact, but let large charts use more of the page so
architecture and sequence labels stay readable in print.

Co-authored-by: Thomas <0xtlt@users.noreply.github.com>
Co-authored-by: Thomas <0xtlt@users.noreply.github.com>
Include polygon/circle/ellipse bounds, skip tiny marker arrowheads, and
avoid expanding already-tight SVGs. Add a diamond flowchart regression test.

Co-authored-by: Thomas <0xtlt@users.noreply.github.com>
Match whole attribute names, skip tiny marker circles, clamp crop size to
the original canvas, and add a padded-diamond crop regression test.

Co-authored-by: Thomas <0xtlt@users.noreply.github.com>
Regenerate docs/assets/preview.png from the current example.md so the
README screenshot shows the Mermaid diagram section.

Co-authored-by: Thomas <0xtlt@users.noreply.github.com>
Keep diagrams on Mermaid's classic light theme so they match the light page
background instead of following the dark code-block theme. Refresh the README
preview.

Co-authored-by: Thomas <0xtlt@users.noreply.github.com>
Use --code-theme light for the example screenshot so code blocks and
Mermaid diagrams both sit on a light page without dark panels.

Co-authored-by: Thomas <0xtlt@users.noreply.github.com>
Hard-code the classic light Mermaid colors (independent of --code-theme),
rewrite any dark canvas background that leaks through, and replace the
README preview asset path so GitHub/CDN cannot keep serving the old dark
diagram image.

Co-authored-by: Thomas <0xtlt@users.noreply.github.com>
@0xtlt
0xtlt marked this pull request as ready for review July 29, 2026 13:00
@cursor

cursor Bot commented Jul 29, 2026

Copy link
Copy Markdown

Bugbot is not enabled for your account, so this pull request was not reviewed.

Enable Bugbot in the Cursor dashboard to get automatic reviews on future PRs.

@cursor
cursor Bot merged commit 743020f into main Jul 29, 2026
2 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants