Skip to content

Commit 7852e1f

Browse files
committed
Fix multiple markdown rendering bugs
1 parent 58b36cc commit 7852e1f

File tree

5 files changed

+31
-32
lines changed

5 files changed

+31
-32
lines changed

web_src/js/markup/asciicast.js

Lines changed: 1 addition & 1 deletion
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

Lines changed: 8 additions & 0 deletions
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('div');
4+
errorNode.setAttribute('class', 'ui message error markup-block-error gt-mono');
5+
errorNode.textContent = err.str || err.message || String(err);
6+
el.before(errorNode);
7+
el.setAttribute('data-done', 'true');
8+
}

web_src/js/markup/content.js

Lines changed: 2 additions & 2 deletions
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

Lines changed: 7 additions & 11 deletions
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-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

Lines changed: 13 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
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

@@ -9,14 +10,6 @@ const iframeCss = `
910
#mermaid {display: block; margin: 0 auto}
1011
`;
1112

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-
}
19-
2013
export async function renderMermaid() {
2114
const els = document.querySelectorAll('.markup code.language-mermaid');
2215
if (!els.length) return;
@@ -30,33 +23,35 @@ export async function renderMermaid() {
3023
});
3124

3225
for (const el of els) {
33-
const source = el.textContent;
26+
const pre = el.closest('pre');
27+
if (pre.hasAttribute('data-done')) continue;
3428

29+
const source = el.textContent;
3530
if (mermaidMaxSourceCharacters >= 0 && source.length > mermaidMaxSourceCharacters) {
36-
displayError(el, new Error(`Mermaid source of ${source.length} characters exceeds the maximum allowed length of ${mermaidMaxSourceCharacters}.`));
31+
displayError(pre, new Error(`Mermaid source of ${source.length} characters exceeds the maximum allowed length of ${mermaidMaxSourceCharacters}.`));
3732
continue;
3833
}
3934

4035
try {
4136
await mermaid.parse(source);
4237
} catch (err) {
43-
displayError(el, err);
44-
el.closest('pre').classList.remove('is-loading');
38+
displayError(pre, err);
4539
continue;
4640
}
4741

4842
try {
4943
// can't use bindFunctions here because we can't cross the iframe boundary. This
5044
// means js-based interactions won't work but they aren't intended to work either
5145
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'));
5446

5547
const iframe = document.createElement('iframe');
5648
iframe.classList.add('markup-render');
57-
iframe.sandbox = 'allow-scripts';
58-
iframe.style.height = `${Math.ceil(parseFloat(heightStr))}px`;
49+
iframe.sandbox = 'allow-scripts allow-same-origin';
5950
iframe.srcdoc = `<html><head><style>${iframeCss}</style></head><body>${svg}</body></html>`;
51+
iframe.addEventListener('load', (e) => {
52+
const height = e.target.contentWindow.document.body.clientHeight;
53+
iframe.style.height = `${height}px`;
54+
});
6055

6156
const mermaidBlock = document.createElement('div');
6257
mermaidBlock.classList.add('mermaid-block');
@@ -66,9 +61,9 @@ export async function renderMermaid() {
6661
btn.setAttribute('data-clipboard-text', source);
6762

6863
mermaidBlock.append(btn);
69-
el.closest('pre').replaceWith(mermaidBlock);
64+
pre.replaceWith(mermaidBlock);
7065
} catch (err) {
71-
displayError(el, err);
66+
displayError(pre, err);
7267
}
7368
}
7469
}

0 commit comments

Comments
 (0)