Skip to content

Add Mermaid diagram rendering for markdown pages#415

Open
mprokopov wants to merge 5 commits intobasecamp:mainfrom
it-premium:main
Open

Add Mermaid diagram rendering for markdown pages#415
mprokopov wants to merge 5 commits intobasecamp:mainfrom
it-premium:main

Conversation

@mprokopov
Copy link
Copy Markdown

Summary

  • Renders fenced ```mermaid code blocks as interactive diagrams using Mermaid.js
  • Respects light/dark theme via prefers-color-scheme
  • Works with Turbo navigation (re-renders on page transitions)

Motivation

Writebook supports fenced code blocks with syntax highlighting via Rouge, but there's no way to render diagrams. Mermaid is the de facto standard for text-based diagrams in markdown (flowcharts, sequence diagrams, ER diagrams, etc.) and is supported by GitHub, GitLab, Notion, and Obsidian.

Since Rouge doesn't recognize mermaid as a language, fenced mermaid blocks fall through to plaintext. The implementation handles both cases: explicit language-mermaid classes and plaintext blocks whose content starts with a known Mermaid keyword.

Screenshot

Mermaid flowchart rendered in Writebook

Implementation

  • config/importmap.rb — pins Mermaid v11 ESM build from jsDelivr CDN
  • app/javascript/application.js — initializes Mermaid once at startup, then on each turbo:load event finds matching code blocks, replaces them with <div class="mermaid">, and calls mermaid.run()

No changes to models, controllers, views, or the HTML sanitizer.

Test plan

  • Create a page with a ```mermaid fenced code block containing a flowchart TD diagram
  • Verify it renders as an SVG diagram, not a code block
  • Toggle OS dark mode — diagram uses correct theme on page load
  • Navigate between pages via Turbo — diagrams render on each page
  • Visit a page with no mermaid blocks — no errors in console

mprokopov and others added 4 commits April 3, 2026 08:43
Load Mermaid.js from CDN to render fenced mermaid code blocks
as interactive diagrams, with dark mode and Turbo navigation support.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
Rouge doesn't recognize mermaid as a language, so fenced mermaid
blocks render as plaintext. Detect them by matching mermaid keywords
in the code block content.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
Pin mermaid in importmap.rb and initialize in application.js
instead of an inline script in the layout.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
Copilot AI review requested due to automatic review settings April 3, 2026 08:39
Copy link
Copy Markdown

@claude claude bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Claude Code Review

This pull request is from a fork — automated review is disabled. A repository maintainer can comment @claude review to run a one-time review.

Copy link
Copy Markdown

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Adds client-side Mermaid.js rendering so fenced Mermaid code blocks in Writebook markdown pages are converted into interactive diagrams, including support for Turbo page navigation and light/dark theming.

Changes:

  • Pin Mermaid.js in the Rails importmap for ESM loading.
  • Initialize Mermaid and render detected Mermaid code blocks on each turbo:load.
  • Add a plaintext fallback heuristic to detect Mermaid content when Rouge highlights as plaintext.

Reviewed changes

Copilot reviewed 2 out of 3 changed files in this pull request and generated 4 comments.

File Description
config/importmap.rb Adds a Mermaid pin so the ESM build can be imported by application.js.
app/javascript/application.js Initializes Mermaid and replaces matching <pre><code> blocks with .mermaid containers on Turbo navigation.

Tip

If you aren't ready for review, convert to a draft PR.
Click "Convert to draft" or run gh pr ready --undo.
Click "Ready for review" or run gh pr ready to reengage.


💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

- Pin exact mermaid version (11.4.1) instead of major-only
- Recompute theme on each turbo:load for dark mode switching
- Tighten graph keyword to require direction token (TD/LR/etc)
- Guard closest("pre") to prevent runtime error on edge cases

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
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