Skip to content

fix(mermaid): prevent diagrams from breaking on theme change#151

Merged
ThisIs-Developer merged 7 commits into
mainfrom
fix/mermaid-theme-change
Jun 2, 2026
Merged

fix(mermaid): prevent diagrams from breaking on theme change#151
ThisIs-Developer merged 7 commits into
mainfrom
fix/mermaid-theme-change

Conversation

@ThisIs-Developer
Copy link
Copy Markdown
Owner

This PR resolves a critical bug where Mermaid diagrams fail to render and display syntax parser errors (e.g., "Syntax error in text" or "Maximum text size in diagram exceeded") after switching the application theme.

Root Cause

During initial rendering, Mermaid parses the raw text inside .mermaid containers and replaces the text content with the rendered SVG element. On a theme switch, the application re-triggers Mermaid rendering on the existing .mermaid DOM nodes. Because the raw text has been replaced with SVG elements, Mermaid is forced to parse the massive SVG code as if it were diagram source code, leading to syntax/parser errors.

Solution

  1. Preserve Original Code: Stored the URL-encoded raw Mermaid source code inside a data-original-code attribute on each .mermaid element during initial markdown parsing.
  2. Configure DOMPurify: Updated the three DOMPurify.sanitize configurations in script.js to allow the data-original-code attribute, ensuring it is not stripped.
  3. Clean Restoration & Re-render:
    • In the themeToggle click handler, read the raw Mermaid code from data-original-code for each .mermaid node, decoded it, and restored it as the node's innerHTML content.
    • Removed the data-processed attribute from each node.
    • Removed old .mermaid-toolbar elements to enable fresh recreation of the toolbars with the new SVG elements.
    • Triggered mermaid.init to draw the diagrams with correct theme colors.
  4. Desktop Support: Ran the Neutralino build/preparation script to completely synchronize the desktop-app resources folder with these core updates.

Copilot AI review requested due to automatic review settings June 2, 2026 06:14
@vercel
Copy link
Copy Markdown

vercel Bot commented Jun 2, 2026

The latest updates on your projects. Learn more about Vercel for GitHub.

Project Deployment Actions Updated (UTC)
markdown-viwer Ready Ready Preview, Comment Jun 2, 2026 6:27am

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

Fixes a Mermaid rendering bug where toggling the theme caused diagrams to error out because the theme-change handler re-parsed the already-rendered SVG instead of the original Mermaid source. The fix preserves the source code in a data-original-code attribute on each .mermaid node and restores it before re-running mermaid.init. The desktop-app resources are synchronized via the Neutralino prepare script (carrying both this fix and previously merged unrelated UI changes).

Changes:

  • Add data-original-code (URL-encoded) to .mermaid nodes when highlighting fenced mermaid blocks; whitelist this attribute in the three DOMPurify.sanitize configs.
  • In the theme toggle handler, restore each Mermaid node's source from data-original-code, drop data-processed, remove stale .mermaid-toolbar, and re-init Mermaid.
  • Sync the desktop-app resources/ (index.html, styles.css, js/script.js) with current root assets.

Reviewed changes

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

File Description
script.js Persist Mermaid source in data-original-code; restore and re-render on theme toggle; permit attribute in DOMPurify configs.
desktop-app/resources/js/script.js Mirror of root script.js including the Mermaid fix plus previously merged unrelated changes (custom undo/redo, mobile direction toggle removal, etc.).
desktop-app/resources/index.html Sync of root index.html: removes mobile-direction-toggle, retitles the clear-formatting modal and toolbar action.
desktop-app/resources/styles.css Sync of root styles.css: adds disabled state for .markdown-tool-btn.

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

@cloudflare-workers-and-pages
Copy link
Copy Markdown

cloudflare-workers-and-pages Bot commented Jun 2, 2026

Deploying markdown-viewer with  Cloudflare Pages  Cloudflare Pages

Latest commit: 533e03b
Status: ✅  Deploy successful!
Preview URL: https://a9caa321.markdown-viewer.pages.dev
Branch Preview URL: https://fix-mermaid-theme-change.markdown-viewer.pages.dev

View logs

@cloudflare-workers-and-pages
Copy link
Copy Markdown

cloudflare-workers-and-pages Bot commented Jun 2, 2026

Deploying markdownviewer with  Cloudflare Pages  Cloudflare Pages

Latest commit: 533e03b
Status: ✅  Deploy successful!
Preview URL: https://65c37cb1.markdownviewer.pages.dev
Branch Preview URL: https://fix-mermaid-theme-change.markdownviewer.pages.dev

View logs

@ThisIs-Developer ThisIs-Developer merged commit a611966 into main Jun 2, 2026
6 checks passed
@ThisIs-Developer ThisIs-Developer deleted the fix/mermaid-theme-change branch June 2, 2026 06:27
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