Skip to content

feat(preview): render Mermaid diagrams on demand - #31

Merged
phatpham9 merged 4 commits into
mainfrom
feat/preview-mermaid
Nov 24, 2026
Merged

phatpham9 merged 4 commits into
mainfrom
feat/preview-mermaid

Conversation

@phatpham9

@phatpham9 phatpham9 commented Nov 24, 2026 •

Copy link
Copy Markdown
Member

Summary

Mermaid fences in previews now render as diagrams when the reader opts in. The setting is off by default, and the renderer is fetched only when a diagram is present.

extractBlocks
  PRE
+   lang="mermaid" / class="mermaid" / highlight-source-* → language: "mermaid"

renderMermaidDiagram
+   strip %%{...}%% directives + YAML config frontmatter
+   mermaid.initialize({ securityLevel: "strict" })
+   render(svg) → DOMPurify SVG pass → restore sanitized foreignObject labels
+   import only the sanitized <svg> node

renderPreviewMermaidBlock
+   header: MERMAID · Source · Copy
+   IntersectionObserver → lazy load + render
+   active CSS theme variables → re-render on theme change
+   error / sanitizer unavailable → source fallback
  • Vendored Mermaid 11.17.2 (MIT, about 950 KB gzip) and DOMPurify 3.5.0 (MPL-2.0/Apache-2.0, about 11 KB gzip), loaded on the first diagram only; users who leave the setting off download neither.
  • Untrusted diagram source cannot downgrade securityLevel or re-enable HTML labels: %%{...}%% directives and YAML config frontmatter are stripped before rendering.
  • The generated SVG is sanitized with DOMPurify, including its foreignObject labels, before any node is imported. If the sanitizer cannot load, the viewer falls back to Source instead of rendering.
  • The Reader setting sits under Article preview and is disabled while preview is off; stored under feedreader.mermaidEnabled; Source toggles the fence and Copy copies it.

Evidence

  • Before: Mermaid blocks rendered as code text with no diagram.
    After: Local and deployed previews of vercel-labs/openreview and the Polymarket HN article rendered their diagrams — 10 foreignObject labels preserved, theme switching re-rendered with the active colors, Source toggled to the fence, Copy returned Copied.
  • Security probes: payloads using %%{init: {"securityLevel":"loose"}}%%, YAML config: securityLevel: loose, iframe/img onerror labels, and click ... "javascript:..." produced no handler execution, no <script>/<iframe>, and no javascript: URLs after sanitization.
  • Tests: npm test — 30 files, 239 tests passed. npm run typecheck — clean. New assertions cover <pre lang="mermaid">, class="mermaid" wrappers, plain lang="en" (not treated as code), and Markdown Mermaid fences.

Merge Danger

Door: two-way — reverting removes the feature and the vendored assets.

Blast Radius: preview rendering and static assets

The assets add about 3.4 MB (roughly 960 KB gzip) to web-static; they are lazily fetched and cached by the service worker after first use. Previews cached before deploy lack the mermaid language hint until they refresh (30-minute TTL). The setting is off by default, so existing users see no change until they opt in.

🤖 Generated with Claude Code

- Detect Mermaid fences from GitHub's <pre lang="mermaid">, class="mermaid"
  wrappers, and Markdown fence info.
- Add a Reader setting (off by default) that lazy-loads a self-hosted
  Mermaid 11.17.2 bundle when a diagram approaches the viewport.
- Render with strict security mode, theme variables from the active CSS
  theme, a Source toggle, Copy, and a source fallback on failure.
- Re-render diagrams when the theme changes; bump app.js to v114 and the
  PWA shell cache to v148.

Co-Authored-By: Claude Code <noreply@anthropic.com>
@github-actions

github-actions Bot commented Nov 24, 2026 •

Copy link
Copy Markdown

🔎 Cloudflare preview: https://c57026f2-feedreader.phatpham9.workers.dev

Uploaded from 18976d6711dbee1971696273f8a870a65cf7dbda. This is a Worker version preview — it shares the production D1 database, so it reads/writes real data; it does not receive cron-triggered refreshes.

phatpham9 and others added 3 commits November 24, 2026 21:13
- Strip %%{...}%% directives and YAML config frontmatter from untrusted
  diagram source so it cannot downgrade securityLevel or enable HTML
  labels.
- Sanitize the generated SVG and its foreignObject labels with vendored
  DOMPurify 3.5.0 before importing any node into the DOM; fail closed to
  the Source view if the sanitizer cannot load.
- Bump app.js to v115 and the PWA shell cache to v149.

Co-Authored-By: Claude Code <noreply@anthropic.com>
- Move the Mermaid option into the Article preview section and show it
  only while preview is enabled.
- Bump app.js to v116 and the PWA shell cache to v150.

Co-Authored-By: Claude Code <noreply@anthropic.com>
- Show the Mermaid option whenever Article preview is shown; disable its
  checkbox while preview is off instead of hiding the option.
- Align the option with the preview option and dim the disabled state.
- Bump app.js to v117 and the PWA shell cache to v151.

Co-Authored-By: Claude Code <noreply@anthropic.com>
@phatpham9
phatpham9 merged commit 04783b5 into main Nov 24, 2026
3 checks passed
@phatpham9
phatpham9 deleted the feat/preview-mermaid branch November 24, 2026 14:32
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.

1 participant