Skip to content

Commit dcde470

Browse files
silverwindGiteaBot
andauthored
Fix math and mermaid rendering bugs (#24049)
1. Fix multiple error display for math and mermaid: ![err](https://user-images.githubusercontent.com/115237/231126411-8a21a777-cd53-4b7e-ac67-5332623106e8.gif) 2. Fix height calculation of certain mermaid diagrams by reading the iframe inner height from it's document instead of parsing it from SVG: Before: <img width="866" alt="Screenshot 2023-04-11 at 11 56 27" src="https://user-images.githubusercontent.com/115237/231126480-b194e02b-ea8c-4ddf-8c79-50c525815d92.png"> After: <img width="855" alt="Screenshot 2023-04-11 at 11 56 35" src="https://user-images.githubusercontent.com/115237/231126494-5fe86a48-8d21-455a-8b95-79b6ee27a16f.png"> 3. Refactor error handling to a common function 4. Rename to `renderAsciicast` for consistency 5. Improve mermaid loading sequence Note: I did try `securityLevel: 'sandbox'` to make mermaid output a iframe directly, but that showed a bug in mermaid where the iframe style height was set incorrectly. Opened mermaid-js/mermaid#4289 for this. --------- Co-authored-by: Giteabot <teabot@gitea.io>
1 parent 7681d58 commit dcde470

File tree

8 files changed

+46
-43
lines changed

8 files changed

+46
-43
lines changed

web_src/css/base.css

+1-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
/* non-color variables */
1010
--border-radius: 0.28571429rem;
1111
--opacity-disabled: 0.55;
12-
--height-loading: 12rem;
12+
--height-loading: 16rem;
1313
/* base colors */
1414
--color-primary: #4183c4;
1515
--color-primary-contrast: #ffffff;

web_src/css/helpers.css

+2-1
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,8 @@
2525
.gt-overflow-x-scroll { overflow-x: scroll !important; }
2626
.gt-cursor-default { cursor: default !important; }
2727
.gt-items-start { align-items: flex-start !important; }
28-
.gt-whitespace-pre { white-space: pre !important }
28+
.gt-whitespace-pre { white-space: pre !important; }
29+
.gt-invisible { visibility: hidden !important; }
2930

3031
.gt-mono {
3132
font-family: var(--fonts-monospace) !important;

web_src/css/markup/content.css

+1-1
Original file line numberDiff line numberDiff line change
@@ -542,7 +542,7 @@
542542

543543
.markup-block-error {
544544
display: block !important; /* override fomantic .ui.form .error.message {display: none} */
545-
border: 1px solid var(--color-error-border) !important;
545+
border: none !important;
546546
margin-bottom: 0 !important;
547547
border-bottom-left-radius: 0 !important;
548548
border-bottom-right-radius: 0 !important;

web_src/js/markup/asciicast.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
export async function renderAsciinemaPlayer() {
1+
export async function renderAsciicast() {
22
const els = document.querySelectorAll('.asciinema-player-container');
33
if (!els.length) return;
44

web_src/js/markup/common.js

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
export function displayError(el, err) {
2+
el.classList.remove('is-loading');
3+
const errorNode = document.createElement('pre');
4+
errorNode.setAttribute('class', 'ui message error markup-block-error');
5+
errorNode.textContent = err.str || err.message || String(err);
6+
el.before(errorNode);
7+
el.setAttribute('data-render-done', 'true');
8+
}

web_src/js/markup/content.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
11
import {renderMermaid} from './mermaid.js';
22
import {renderMath} from './math.js';
33
import {renderCodeCopy} from './codecopy.js';
4-
import {renderAsciinemaPlayer} from './asciicast.js';
4+
import {renderAsciicast} from './asciicast.js';
55
import {initMarkupTasklist} from './tasklist.js';
66

77
// code that runs for all markup content
88
export function initMarkupContent() {
99
renderMermaid();
1010
renderMath();
1111
renderCodeCopy();
12-
renderAsciinemaPlayer();
12+
renderAsciicast();
1313
}
1414

1515
// code that only runs for comments

web_src/js/markup/math.js

+7-11
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,8 @@
1-
function displayError(el, err) {
2-
const target = targetElement(el);
3-
target.classList.remove('is-loading');
4-
const errorNode = document.createElement('div');
5-
errorNode.setAttribute('class', 'ui message error markup-block-error gt-mono');
6-
errorNode.textContent = err.str || err.message || String(err);
7-
target.before(errorNode);
8-
}
1+
import {displayError} from './common.js';
92

103
function targetElement(el) {
11-
// The target element is either the current element if it has the `is-loading` class or the pre that contains it
4+
// The target element is either the current element if it has the
5+
// `is-loading` class or the pre that contains it
126
return el.classList.contains('is-loading') ? el : el.closest('pre');
137
}
148

@@ -22,6 +16,8 @@ export async function renderMath() {
2216
]);
2317

2418
for (const el of els) {
19+
const target = targetElement(el);
20+
if (target.hasAttribute('data-render-done')) continue;
2521
const source = el.textContent;
2622
const displayMode = el.classList.contains('display');
2723
const nodeName = displayMode ? 'p' : 'span';
@@ -33,9 +29,9 @@ export async function renderMath() {
3329
maxExpand: 50,
3430
displayMode,
3531
});
36-
targetElement(el).replaceWith(tempEl);
32+
target.replaceWith(tempEl);
3733
} catch (error) {
38-
displayError(el, error);
34+
displayError(target, error);
3935
}
4036
}
4137
}

web_src/js/markup/mermaid.js

+24-26
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,12 @@
11
import {isDarkTheme} from '../utils.js';
22
import {makeCodeCopyButton} from './codecopy.js';
3+
import {displayError} from './common.js';
34

45
const {mermaidMaxSourceCharacters} = window.config;
56

6-
const iframeCss = `
7-
:root {color-scheme: normal}
8-
body {margin: 0; padding: 0; overflow: hidden}
9-
#mermaid {display: block; margin: 0 auto}
10-
`;
11-
12-
function displayError(el, err) {
13-
el.closest('pre').classList.remove('is-loading');
14-
const errorNode = document.createElement('div');
15-
errorNode.setAttribute('class', 'ui message error markup-block-error gt-mono');
16-
errorNode.textContent = err.str || err.message || String(err);
17-
el.closest('pre').before(errorNode);
18-
}
7+
const iframeCss = `:root {color-scheme: normal}
8+
body {margin: 0; padding: 0; overflow: hidden}
9+
#mermaid {display: block; margin: 0 auto}`;
1910

2011
export async function renderMermaid() {
2112
const els = document.querySelectorAll('.markup code.language-mermaid');
@@ -30,45 +21,52 @@ export async function renderMermaid() {
3021
});
3122

3223
for (const el of els) {
33-
const source = el.textContent;
24+
const pre = el.closest('pre');
25+
if (pre.hasAttribute('data-render-done')) continue;
3426

27+
const source = el.textContent;
3528
if (mermaidMaxSourceCharacters >= 0 && source.length > mermaidMaxSourceCharacters) {
36-
displayError(el, new Error(`Mermaid source of ${source.length} characters exceeds the maximum allowed length of ${mermaidMaxSourceCharacters}.`));
29+
displayError(pre, new Error(`Mermaid source of ${source.length} characters exceeds the maximum allowed length of ${mermaidMaxSourceCharacters}.`));
3730
continue;
3831
}
3932

4033
try {
4134
await mermaid.parse(source);
4235
} catch (err) {
43-
displayError(el, err);
44-
el.closest('pre').classList.remove('is-loading');
36+
displayError(pre, err);
4537
continue;
4638
}
4739

4840
try {
4941
// can't use bindFunctions here because we can't cross the iframe boundary. This
5042
// means js-based interactions won't work but they aren't intended to work either
5143
const {svg} = await mermaid.render('mermaid', source);
52-
const heightStr = (svg.match(/viewBox="(.+?)"/) || ['', ''])[1].split(/\s+/)[3];
53-
if (!heightStr) return displayError(el, new Error('Could not determine chart height'));
5444

5545
const iframe = document.createElement('iframe');
56-
iframe.classList.add('markup-render');
57-
iframe.sandbox = 'allow-scripts';
58-
iframe.style.height = `${Math.ceil(parseFloat(heightStr))}px`;
46+
iframe.classList.add('markup-render', 'gt-invisible');
5947
iframe.srcdoc = `<html><head><style>${iframeCss}</style></head><body>${svg}</body></html>`;
6048

6149
const mermaidBlock = document.createElement('div');
62-
mermaidBlock.classList.add('mermaid-block');
50+
mermaidBlock.classList.add('mermaid-block', 'is-loading', 'gt-hidden');
6351
mermaidBlock.append(iframe);
6452

6553
const btn = makeCodeCopyButton();
6654
btn.setAttribute('data-clipboard-text', source);
67-
6855
mermaidBlock.append(btn);
69-
el.closest('pre').replaceWith(mermaidBlock);
56+
57+
iframe.addEventListener('load', () => {
58+
pre.replaceWith(mermaidBlock);
59+
mermaidBlock.classList.remove('gt-hidden');
60+
iframe.style.height = `${iframe.contentWindow.document.body.clientHeight}px`;
61+
setTimeout(() => { // avoid flash of iframe background
62+
mermaidBlock.classList.remove('is-loading');
63+
iframe.classList.remove('gt-invisible');
64+
}, 0);
65+
});
66+
67+
document.body.append(mermaidBlock);
7068
} catch (err) {
71-
displayError(el, err);
69+
displayError(pre, err);
7270
}
7371
}
7472
}

0 commit comments

Comments
 (0)