← Back to Pull requests

Add native Mermaid diagram support #300

Closed
unknown closed this pull request without merging on Oct 06, 2026 20:35 UTC
View on GitHub
unknown unknown commented on Oct 04, 2026 11:18 UTC
Author

This adds native support for Mermaid diagrams in mermaid fenced code blocks.

Design

  • ndg-commonmark has a new mermaid option. When it is set, the processor renders each mermaid code block as <pre class="mermaid"> with the diagram source as text, and does not highlight it. Other code blocks are highlighted as before.

  • ndg-config has a new [mermaid] section:

  •   [mermaid]
      enable = true
      # A URL, or a local file that NDG copies to `assets/`.
      script = "https://cdn.jsdelivr.net/npm/mermaid@11/dist/mermaid.min.js"
      

    script defaults to Mermaid 11 from jsDelivr. A value that starts with https://, http:// or // is a URL. Any other value is a local path, and validation fails if the file does not exist.
  • When Mermaid is enabled, NDG adds assets/mermaid-init.js to each page, with the library location in data-mermaid-src. The script loads the library only on a page that has a diagram, because the library is large. It initializes Mermaid with startOnLoad: false, securityLevel: "strict" and the Mermaid theme that agrees with data-theme on <html>, or with the color scheme of the browser. On ndg:themechange (from the theme toggle), it draws the diagrams again in the new theme. A MutationObserver draws diagrams that the page adds later.

  • NDG copies a local library to assets/ as is, without the JS postprocessing, because the library is already minified. Users can override mermaid-init.js with a template directory, as with main.js.

  • ndg-builder has a mermaid option. It uses the library from mdbook-mermaid, so that a site (and a ZIM archive) does not need a CDN.
  • Mermaid is off by default, so the output does not change for existing sites.

    Alternatives

  • Render diagrams at build time (for example with mermaid-cli). This needs a headless browser at build time, so I did not do this.

  • Always use a CDN. This does not work offline or for ZIM archives, so script also takes a local file.
  • Tests

  • ndg-commonmark/tests/mermaid.rs: the block conversion, with highlighting on and off.

  • ndg-config: the [mermaid] overrides and the URL or file decision.

  • ndg-html/tests/custom_scripts.rs: the loader tag for a URL and for a local file.

  • ndg/tests/integration.rs: the asset copy, also with JS minification.

  • mermaid-init.test.js: the library loads only with diagrams, loads once, draws diagrams added later, and draws them again when the theme changes.

  • checks.mermaid: an ndg-builder site with a diagram.
  • Assisted-by: Claude:claude-opus-5-5

    https://claude.ai/code/session_01RJvbghuj5GtCBiCjmBepNJ

    Diff: 329 lines (+246 / -83) across 4 files Reviewer: NotAShelf Review duration: 3437m
    All Vouch review confidence checks passed
    Residual risk 0.17 = Change Risk (0.42) × (1 − Review Confidence (0.60)). Review depth and reviewer attention state are adequate.
    Model 1 · Change Risk (XGBoost)
    Score: 0.42 · 329 lines diff across 4 files
    0.42
    Model 2 · Review Depth (DistilBERT)
    Depth Score: 0.17 · 1 substantive comment(s)
    0.17
    Model 3 · Review Timing & Attention
    Session adequacy: 100% · Reviewer familiarity: 35%
    60%
    Bedrock Claude 3 · Risk Narration
    (disconnected)
    Groq
    Groq Risk Narration
    AWS Bedrock is currently disconnected. Generate high-confidence PR triage narration on-demand using Groq.
    Model 1 · Change Risk Attribution
    SHAP Weights
    Diff Size diff_size
    0.347
    Shallow Review Depth shallow_review_depth
    0.289
    Real PR Conversation Timeline
    1 comments
    "Review active on #300"
    general weight: 0.35
    Reviewers
    Assignees
    Unassigned
    Labels
    risk: low confidence: 60.5% reviewed
    Vouch Risk Breakdown
    AI-powered review risk analysis
    Change Risk
    Change Risk · Model 1 (XGBoost)
    Predicted defect probability (0.00–1.00) based on diff churn, modified sensitive files, and author risk history.
    Score: 42.1% (MEDIUM) · Higher = larger blast radius
    42.1%
    Review Confidence
    Review Confidence · Models 2 & 3
    Scrutiny score (0.00–1.00) combining DistilBERT semantic review depth with reviewer familiarity and session dwell time.
    Score: 60.5% · Lower = rushed or shallow review
    60.5%
    Residual Risk
    Residual Risk · Core Equation
    Residual = Change × (1 − Confidence)
    Unmitigated risk surviving review. ≥ 0.65 triggers an automated re-queue to protect production.
    Score: 16.6% · Safe to merge
    16.6%
    Repository