Skip to content

feat/lesson-mermaid-diagrams - #123

Merged
ebouchut merged 4 commits into
devfrom
feat/lesson-mermaid-diagrams
Jul 29, 2026
Merged

feat/lesson-mermaid-diagrams#123
ebouchut merged 4 commits into
devfrom
feat/lesson-mermaid-diagrams

Conversation

@ebouchut

@ebouchut ebouchut commented Jul 29, 2026

Copy link
Copy Markdown
Owner

This PR renders Mermaid diagrams on lesson pages client-side with JavaScript.
It fixes #122.
ADR-0016 recorded the approach and architecture decision.

What problem does this PR solve?

Previously, lessons showed Mermaid fences only source, whereas students need rendered diagrams in the browser.

What changed?

  • Added orgjars.npm:mermaid dependency (pinned trans deps excluded).
  • Added new JavaScript lesson-mermaid.js to:
  • Lazily load Mermaid when a mermaid fence exists.
  • Initialize Mermaid with securityLevel strict.
  • Replace code fences with figure + SVG while keeping original code block as fallback.
  • Provide show/hide source toggle with aria-expanded/aria-controls.
  • Ensure color-scheme-aware rendering and re-render on scheme.
  • Handle invalid diagrams by degrading to styled source and removing detached error.
  • Constrain wide diagrams to scroll inside the.
  • Test to assert server-side rendering contract (language-mermaid and preserved source; no SVG server-side).
  • Documentation ADR recorded the client-side rendering decision.

The server still outputs sanitized code blocks (ADR-0016).

  • Adds a lazily loaded, version-pinned, self-hosted Mermaidjar served from /webars.
  • Introduces lesson-mermaid.js to upgrade mermaid code fences SVG diagrams with accessibility.
  • Tests pin the contract: language-mermaid class and source survive rendering.
  • No SVG in server output.

ebouchut added 4 commits July 29, 2026 09:46
Lessons should render Mermaid fences as diagrams. ADR-0016 keeps the
server out of it: commonmark-java already emits the fence as a
sanitized code block (no extension exists or is needed), and the
browser upgrades it to SVG with a lazily loaded, version-pinned,
self-hosted Mermaid running at securityLevel strict. Server-side
alternatives (mermaid-cli, Kroki) are rejected as infrastructure
disproportionate to the feature. The code block stays in the DOM as
the no-JavaScript, syntax-error, and accessibility fallback.

Refs #122
Mermaid ships self-hosted through org.webjars.npm:mermaid so lesson
diagrams never load from a CDN and the version is pinned where
Dependabot can see it. The npm WebJar declares 21 npm-mirror
transitive dependencies; a wildcard exclusion drops them all, leaving
the single mermaid artifact, verified with dependency:tree. Spring
Boot serves the bundle from the classpath at the versioned /webjars
path; lesson pages require authentication, so the existing catch-all
rule already authorizes it.

Refs #122
The Mermaid feature has no server-side code at all: the browser
renders whatever the sanitized code block contains (ADR-0016). That
makes the renderer output the load-bearing contract, so a test pins
it: the language-mermaid class and the diagram source survive
rendering and sanitization, and no SVG appears in server output.

Refs #122
Mermaid fences used to stay visible as source code. lesson-mermaid.js,
the project's first client-side JavaScript feature, now upgrades them
to SVG diagrams in the browser, exactly as ADR-0016 decided: the
server keeps shipping sanitized code blocks and knows nothing about
diagrams.

The script loads the self-hosted Mermaid bundle lazily, only when the
lesson actually contains a language-mermaid block, and initializes it
with securityLevel strict. Each diagram gets a figure plus a
show/hide source toggle wired with aria-expanded and aria-controls;
the original code block stays in the DOM as the no-JavaScript,
syntax-error, and screen-reader fallback (an accTitle line in the
source becomes the SVG title). Diagrams follow the color scheme at
load and re-render on scheme change; invalid diagrams degrade to the
styled source block and Mermaid's detached error artifact is removed.
Wide diagrams scroll inside the figure, never the page (RGAA 10.11).
The lesson form hints at the fence and the accTitle line.

Fixes #122
@codecov

codecov Bot commented Jul 29, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 81.57%. Comparing base (ed3cd5c) to head (fdb213a).

Additional details and impacted files
@@            Coverage Diff            @@
##                dev     #123   +/-   ##
=========================================
  Coverage     81.57%   81.57%           
  Complexity      201      201           
=========================================
  Files            38       38           
  Lines           863      863           
  Branches         53       53           
=========================================
  Hits            704      704           
  Misses          125      125           
  Partials         34       34           

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@ebouchut
ebouchut merged commit b9b2803 into dev Jul 29, 2026
7 checks passed
@ebouchut
ebouchut deleted the feat/lesson-mermaid-diagrams branch July 29, 2026 08:23
@github-project-automation github-project-automation Bot moved this from In Review to Done in learn-dev-project Jul 29, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

Status: Done

Development

Successfully merging this pull request may close these issues.

Render Mermaid diagrams in lesson Markdown

1 participant