Skip to content

Commit f501325

Browse files
committed
Various Mermaid improvments
- Render into iframe for improved security - Use built-in dark theme instead of color inversion - Remove flexbox attributes, resulting in more consistent size rendering - Update API usage and update to latest version
1 parent 609c916 commit f501325

File tree

8 files changed

+44
-39
lines changed

8 files changed

+44
-39
lines changed

package-lock.json

Lines changed: 15 additions & 15 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
"less": "4.1.2",
2424
"less-loader": "10.2.0",
2525
"license-checker-webpack-plugin": "0.2.1",
26-
"mermaid": "8.13.10",
26+
"mermaid": "8.14.0",
2727
"mini-css-extract-plugin": "2.5.3",
2828
"monaco-editor": "0.32.1",
2929
"monaco-editor-webpack-plugin": "7.0.1",

web_src/js/markup/mermaid.js

Lines changed: 18 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import {isDarkTheme} from '../utils.js';
12
const {mermaidMaxSourceCharacters} = window.config;
23

34
function displayError(el, err) {
@@ -15,14 +16,8 @@ export async function renderMermaid() {
1516
const {default: mermaid} = await import(/* webpackChunkName: "mermaid" */'mermaid');
1617

1718
mermaid.initialize({
18-
mermaid: {
19-
startOnLoad: false,
20-
},
21-
flowchart: {
22-
useMaxWidth: true,
23-
htmlLabels: false,
24-
},
25-
theme: 'neutral',
19+
startOnLoad: false,
20+
theme: isDarkTheme() ? 'dark' : 'neutral',
2621
securityLevel: 'strict',
2722
});
2823

@@ -48,7 +43,21 @@ export async function renderMermaid() {
4843
mermaid.init(undefined, el, (id) => {
4944
const svg = document.getElementById(id);
5045
svg.classList.add('mermaid-chart');
51-
svg.closest('pre').replaceWith(svg);
46+
const iframe = document.createElement('iframe');
47+
iframe.classList.add('markup-render');
48+
// allow-same-origin is to set inline style below
49+
iframe.sandbox = 'allow-scripts allow-same-origin';
50+
iframe.srcdoc = svg.outerHTML;
51+
iframe.addEventListener('load', () => {
52+
const style = document.createElement('style');
53+
style.appendChild(document.createTextNode(`
54+
body {margin: 0; padding: 0; overflow: hidden}
55+
.mermaid-chart {display: block; margin: 0 auto}
56+
`));
57+
iframe.contentWindow.document.head.appendChild(style);
58+
iframe.style.height = `${iframe.contentWindow.document.body.scrollHeight}px`;
59+
});
60+
svg.closest('pre').replaceWith(iframe);
5261
});
5362
} catch (err) {
5463
displayError(el, err);

web_src/less/_base.less

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
/* other variables */
77
--border-radius: .28571429rem;
88
--opacity-disabled: .55;
9+
--height-loading: 12rem;
910
--color-primary: #4183c4;
1011
--color-primary-dark-1: #3876b3;
1112
--color-primary-dark-2: #31699f;

web_src/less/animations.less

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@
3030

3131
.markup pre.is-loading,
3232
.editor-loading.is-loading {
33-
height: 12rem;
33+
height: var(--height-loading);
3434
}
3535

3636
@keyframes fadein {

web_src/less/markup/content.less

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -536,6 +536,14 @@
536536
}
537537
}
538538

539+
.markup-render {
540+
display: block;
541+
border: none;
542+
width: 100%;
543+
height: var(--height-loading); // actual height is set in JS after loading
544+
overflow: hidden;
545+
}
546+
539547
.markup-block-error {
540548
margin-bottom: 0 !important;
541549
border-bottom-left-radius: 0 !important;

web_src/less/markup/mermaid.less

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,3 @@
1-
.mermaid-chart {
2-
display: flex;
3-
justify-content: center;
4-
align-items: center;
5-
padding: 1rem;
6-
margin: 1rem auto;
7-
height: auto;
8-
}
9-
101
/* mermaid's errorRenderer seems to unavoidably spew stuff into <body>, hide it */
112
body > div[id*="mermaid-"] {
123
display: none !important;

web_src/less/themes/theme-arc-green.less

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -455,10 +455,6 @@ img[src$="/img/matrix.svg"] {
455455
filter: invert(80%);
456456
}
457457

458-
.mermaid-chart {
459-
filter: invert(84%) hue-rotate(180deg);
460-
}
461-
462458
.is-loading::after {
463459
border-color: #4a4c58 #4a4c58 #d7d7da #d7d7da;
464460
}

0 commit comments

Comments
 (0)