Skip to content

Commit 4e160f2

Browse files
author
warren.q
committed
fix: allow <br> line breaks in Mermaid node labels
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 #263
1 parent ccf738f commit 4e160f2

2 files changed

Lines changed: 23 additions & 1 deletion

File tree

content/index.css

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -335,6 +335,15 @@ pre:has(> code.mermaid) {
335335
height: 100%;
336336
}
337337

338+
/*fix: allow <br> line breaks inside mermaid node labels
339+
The github theme sets `code br { display: none }` which hides <br> inside
340+
mermaid `<code>` elements, preventing multi-line node labels from rendering.
341+
See: https://github.com/simov/markdown-viewer/issues/263 */
342+
.markdown-body code.mermaid br,
343+
.markdown-theme code.mermaid br {
344+
display: inline !important;
345+
}
346+
338347
/*mermaid text bold effect*/
339348
svg[id^=mermaid] text {
340349
stroke: none !important;

content/mermaid.js

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,18 @@ var mmd = (() => {
55
var walk = (regex, string, result = [], match = regex.exec(string)) =>
66
!match ? result : walk(regex, string, result.concat(match[1]))
77

8+
var fixMermaidViewBox = () => {
9+
document.querySelectorAll('pre code.mermaid svg').forEach((svg) => {
10+
var bbox = svg.getBBox()
11+
if (bbox.height > 0) {
12+
var pad = 16
13+
svg.setAttribute('viewBox',
14+
(bbox.x - pad) + ' ' + (bbox.y - pad) + ' ' +
15+
(bbox.width + pad * 2) + ' ' + (bbox.height + pad * 2))
16+
}
17+
})
18+
}
19+
820
return {
921
render: () => {
1022
if (loaded) {
@@ -19,7 +31,7 @@ var mmd = (() => {
1931
state._themes[state.theme] === 'dark' ||
2032
(state._themes[state.theme] === 'auto' && window.matchMedia('(prefers-color-scheme: dark)').matches)
2133
? 'dark' : 'default'
22-
mermaid.initialize({theme})
34+
mermaid.initialize({theme, securityLevel: 'loose'})
2335
mermaid.init({theme}, 'code.mermaid')
2436
loaded = true
2537

@@ -28,6 +40,7 @@ var mmd = (() => {
2840
var svg = Array.from(document.querySelectorAll('pre code.mermaid svg'))
2941
if (diagrams.length === svg.length) {
3042
clearInterval(timeout)
43+
setTimeout(fixMermaidViewBox, 100)
3144
svg.forEach((diagram) => {
3245
var panzoom = Panzoom(diagram, {canvas: true})
3346
diagram.parentElement.parentElement.addEventListener('wheel', (e) => {

0 commit comments

Comments
 (0)