Fix mermaid diagram rendering with YAML frontmatter#1913
Open
gjouret wants to merge 1 commit intoglushchenko:masterfrom
Open
Fix mermaid diagram rendering with YAML frontmatter#1913gjouret wants to merge 1 commit intoglushchenko:masterfrom
gjouret wants to merge 1 commit intoglushchenko:masterfrom
Conversation
Mermaid diagrams using YAML frontmatter (--- config blocks) failed to render in preview, showing as plain text code blocks instead of graphical diagrams. Three issues were identified and fixed: 1. Horizontal rule replacement destroyed mermaid frontmatter: getPrettifiedContent() globally replaced \n---\n with \n<hr>\n, including inside fenced code blocks. Added replaceHorizontalRulesOutsideCodeBlocks() to skip code fences. 2. cleanMetaData() stripped --- separators from content: When a note has YAML front matter, components(separatedBy: "---") splits on ALL occurrences, and list.joined() rejoined without the separator, destroying --- inside mermaid blocks. Fixed by using list.joined(separator: "---"). 3. Mermaid v11 API and rendering improvements: - Updated from deprecated mermaid.init() to mermaid.run() (async) - Bundled @mermaid-js/layout-elk plugin for ELK layout support - Added YAML frontmatter indentation normalization (tabs → spaces) - Changed useMaxWidth from false to true (diagrams fit preview width) - Added error handling with visible error messages on render failure - Removed async script loading to prevent race conditions Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Mermaid diagrams that use YAML frontmatter (
---config blocks) fail to render in preview — they display as plain text code blocks instead of graphical diagrams. This PR fixes three root causes:getPrettifiedContent()globally replaced\n---\nwith\n<hr>\n, including inside fenced code blocks, destroying mermaid YAML frontmatter delimiters. AddedreplaceHorizontalRulesOutsideCodeBlocks()that uses regex to skip fenced code blocks.cleanMetaData()stripped all---separators when a note has YAML front matter.components(separatedBy: "---")splits on every occurrence, andlist.joined()rejoined without the separator. Fixed withlist.joined(separator: "---").mermaid.init()to asyncmermaid.run(), bundled@mermaid-js/layout-elkplugin for ELK layout support, added YAML frontmatter indentation normalization (tabs/mixed whitespace → consistent spaces), changeduseMaxWidthtotrueso diagrams fit the preview width, and added visible error messages on render failure.Example mermaid that now renders correctly
Test plan
layout: elk) — verify it renders as a graphical diagram---\ntitle: ...\n---) AND a mermaid diagram with its own---frontmatter — verify both are handled correctly---are unaffected🤖 Generated with Claude Code