Skip to content

fix: allow <br> line breaks in Mermaid node labels#289

Open
koyo922 wants to merge 1 commit intosimov:mainfrom
koyo922:fix/mermaid-br-linebreak
Open

fix: allow <br> line breaks in Mermaid node labels#289
koyo922 wants to merge 1 commit intosimov:mainfrom
koyo922:fix/mermaid-br-linebreak

Conversation

@koyo922
Copy link
Copy Markdown

@koyo922 koyo922 commented Apr 2, 2026

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:

.markdown-body code br { display: none }

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 SVG viewBox is pre-calculated for single-line node heights, causing the top of the diagram to be clipped.

Fix

  1. CSS: Add .markdown-body code.mermaid br { display: inline !important } to override the theme rule specifically for Mermaid elements
  2. JS: Recalculate SVG viewBox using getBBox() after Mermaid finishes rendering
  3. JS: Set securityLevel: 'loose' in mermaid.initialize() so Mermaid preserves <br> HTML tags during rendering

Demo

Before (single line):

A["Line 1<br>Line 2<br>Line 3"]
→ renders as: Line 1Line 2Line 3

After (proper line breaks):

A["Line 1<br>Line 2<br>Line 3"]
→ renders as:
  Line 1
  Line 2
  Line 3

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

@koyo922 koyo922 force-pushed the fix/mermaid-br-linebreak branch from 4e160f2 to 47f5f2f Compare April 2, 2026 09:13
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
@koyo922 koyo922 force-pushed the fix/mermaid-br-linebreak branch from 47f5f2f to 3586407 Compare April 2, 2026 09:30
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.

multi line Markdown not rendering multiple lines

1 participant