Skip to content

Render Mermaid diagrams to inline SVG at build time (offline support) #625

@srid

Description

@srid

Problem

Currently, Mermaid diagrams require client-side JavaScript loaded from a CDN (cdn.jsdelivr.net/npm/mermaid). This means generated sites cannot render diagrams when viewed offline.

Mermaid is opt-in via <snippet var="js.mermaid" /> in page.headHtml, configured in index.yaml.

Proposed Solution

Render mermaid code blocks to inline SVG at build time, eliminating the CDN dependency entirely. This is the approach sukr.io uses.

Implementation

  1. Add @mermaid-js/mermaid-cli (mmdc) as a build dependency (via Nix)
  2. Create a new Pandoc block renderer (Emanote/Pandoc/Renderer/Mermaid.hs) that intercepts CodeBlock nodes with the mermaid class
  3. Run mmdc to render the mermaid definition to SVG
  4. Replace the code block with an inline RawBlock "html" svgContent
  5. Graceful fallback: if rendering fails, preserve the original code block with an error comment

Benefits

  • Generated site works fully offline
  • No client-side JavaScript needed for diagrams
  • Faster page loads (no JS parsing/rendering delay)
  • Diagrams are part of the HTML — visible to search engines and screen readers

Tradeoffs

  • Adds mmdc as a build-time dependency
  • Some complex diagram types may render differently than the JS version
  • Build time increases slightly

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions