fix: allow <br> line breaks in Mermaid node labels#289
Open
koyo922 wants to merge 1 commit intosimov:mainfrom
Open
fix: allow <br> line breaks in Mermaid node labels#289koyo922 wants to merge 1 commit intosimov:mainfrom
koyo922 wants to merge 1 commit intosimov:mainfrom
Conversation
4e160f2 to
47f5f2f
Compare
The GitHub theme CSS sets 'code br { display: none }' which hides <br>
elements inside Mermaid <code> blocks, preventing multi-line node labels.
This fix:
1. Adds a CSS override for code.mermaid br to restore display
2. Recalculates SVG viewBox after rendering to prevent clipping
3. Sets securityLevel to 'loose' so Mermaid preserves <br> tags
Fixes simov#263
47f5f2f to
3586407
Compare
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.
Problem
When using
<br>for line breaks inside Mermaid flowchart node labels (e.g.A["Line 1<br>Line 2"]), the text renders on a single line instead of wrapping.Root Cause
The GitHub theme CSS contains the rule:
Since Mermaid diagrams are rendered inside
<code class="mermaid">elements,<br>tags in node labels are hidden by this CSS rule.Additionally, even after making
<br>visible, the SVGviewBoxis pre-calculated for single-line node heights, causing the top of the diagram to be clipped.Fix
.markdown-body code.mermaid br { display: inline !important }to override the theme rule specifically for Mermaid elementsviewBoxusinggetBBox()after Mermaid finishes renderingsecurityLevel: 'loose'inmermaid.initialize()so Mermaid preserves<br>HTML tags during renderingDemo
Before (single line):
After (proper line breaks):
Workaround
Users can work around this by adding
%%{init: {"flowchart": {"htmlLabels": false}} }%%at the top of their Mermaid block, which uses SVG text rendering instead of HTML foreignObject. However, this requires modifying every diagram and loses some formatting features.Fixes #263