2024-07-07 09:32:30 -06:00
|
|
|
import {isDarkTheme} from '../utils.ts';
|
|
|
|
import {makeCodeCopyButton} from './codecopy.ts';
|
|
|
|
import {displayError} from './common.ts';
|
2022-12-25 10:17:48 -07:00
|
|
|
|
2021-10-21 01:37:43 -06:00
|
|
|
const {mermaidMaxSourceCharacters} = window.config;
|
2020-08-04 13:56:37 -06:00
|
|
|
|
2023-10-07 21:20:12 -06:00
|
|
|
// margin removal is for https://github.com/mermaid-js/mermaid/issues/4907
|
2023-04-17 04:10:22 -06:00
|
|
|
const iframeCss = `:root {color-scheme: normal}
|
|
|
|
body {margin: 0; padding: 0; overflow: hidden}
|
2023-10-07 21:20:12 -06:00
|
|
|
#mermaid {display: block; margin: 0 auto}
|
|
|
|
blockquote, dd, dl, figure, h1, h2, h3, h4, h5, h6, hr, p, pre {margin: 0}`;
|
2020-07-27 00:24:09 -06:00
|
|
|
|
2024-11-11 04:13:57 -07:00
|
|
|
export async function renderMermaid(): Promise<void> {
|
2021-11-16 01:16:05 -07:00
|
|
|
const els = document.querySelectorAll('.markup code.language-mermaid');
|
|
|
|
if (!els.length) return;
|
2020-07-27 00:24:09 -06:00
|
|
|
|
2021-10-19 01:23:58 -06:00
|
|
|
const {default: mermaid} = await import(/* webpackChunkName: "mermaid" */'mermaid');
|
2020-07-27 00:24:09 -06:00
|
|
|
|
2020-08-04 13:56:37 -06:00
|
|
|
mermaid.initialize({
|
2022-02-15 20:28:29 -07:00
|
|
|
startOnLoad: false,
|
|
|
|
theme: isDarkTheme() ? 'dark' : 'neutral',
|
2020-07-27 00:24:09 -06:00
|
|
|
securityLevel: 'strict',
|
2024-08-25 11:23:13 -06:00
|
|
|
suppressErrorRendering: true,
|
2020-07-27 00:24:09 -06:00
|
|
|
});
|
|
|
|
|
|
|
|
for (const el of els) {
|
2023-04-17 04:10:22 -06:00
|
|
|
const pre = el.closest('pre');
|
|
|
|
if (pre.hasAttribute('data-render-done')) continue;
|
2022-02-15 20:28:29 -07:00
|
|
|
|
2023-04-17 04:10:22 -06:00
|
|
|
const source = el.textContent;
|
2022-02-15 20:28:29 -07:00
|
|
|
if (mermaidMaxSourceCharacters >= 0 && source.length > mermaidMaxSourceCharacters) {
|
2023-04-17 04:10:22 -06:00
|
|
|
displayError(pre, new Error(`Mermaid source of ${source.length} characters exceeds the maximum allowed length of ${mermaidMaxSourceCharacters}.`));
|
2020-08-04 13:56:37 -06:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
try {
|
2023-03-03 22:39:07 -07:00
|
|
|
await mermaid.parse(source);
|
2020-08-04 13:56:37 -06:00
|
|
|
} catch (err) {
|
2023-04-17 04:10:22 -06:00
|
|
|
displayError(pre, err);
|
2020-08-04 13:56:37 -06:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
try {
|
2022-02-15 20:28:29 -07:00
|
|
|
// can't use bindFunctions here because we can't cross the iframe boundary. This
|
|
|
|
// means js-based interactions won't work but they aren't intended to work either
|
2023-03-03 22:39:07 -07:00
|
|
|
const {svg} = await mermaid.render('mermaid', source);
|
|
|
|
|
|
|
|
const iframe = document.createElement('iframe');
|
2024-03-04 22:29:32 -07:00
|
|
|
iframe.classList.add('markup-render', 'tw-invisible');
|
2023-03-03 22:39:07 -07:00
|
|
|
iframe.srcdoc = `<html><head><style>${iframeCss}</style></head><body>${svg}</body></html>`;
|
|
|
|
|
|
|
|
const mermaidBlock = document.createElement('div');
|
2024-03-24 12:23:38 -06:00
|
|
|
mermaidBlock.classList.add('mermaid-block', 'is-loading', 'tw-hidden');
|
2023-03-03 22:39:07 -07:00
|
|
|
mermaidBlock.append(iframe);
|
|
|
|
|
|
|
|
const btn = makeCodeCopyButton();
|
|
|
|
btn.setAttribute('data-clipboard-text', source);
|
|
|
|
mermaidBlock.append(btn);
|
2023-04-17 04:10:22 -06:00
|
|
|
|
2024-11-09 11:03:55 -07:00
|
|
|
const updateIframeHeight = () => {
|
2024-11-25 23:37:24 -07:00
|
|
|
const body = iframe.contentWindow?.document?.body;
|
|
|
|
if (body) {
|
|
|
|
iframe.style.height = `${body.clientHeight}px`;
|
|
|
|
}
|
2024-11-09 11:03:55 -07:00
|
|
|
};
|
|
|
|
|
2023-04-17 04:10:22 -06:00
|
|
|
iframe.addEventListener('load', () => {
|
|
|
|
pre.replaceWith(mermaidBlock);
|
2024-03-24 12:23:38 -06:00
|
|
|
mermaidBlock.classList.remove('tw-hidden');
|
2024-11-09 11:03:55 -07:00
|
|
|
updateIframeHeight();
|
2023-04-17 04:10:22 -06:00
|
|
|
setTimeout(() => { // avoid flash of iframe background
|
|
|
|
mermaidBlock.classList.remove('is-loading');
|
2024-03-04 22:29:32 -07:00
|
|
|
iframe.classList.remove('tw-invisible');
|
2023-04-17 04:10:22 -06:00
|
|
|
}, 0);
|
2024-11-25 23:37:24 -07:00
|
|
|
|
|
|
|
// update height when element's visibility state changes, for example when the diagram is inside
|
|
|
|
// a <details> + <summary> block and the <details> block becomes visible upon user interaction, it
|
|
|
|
// would initially set a incorrect height and the correct height is set during this callback.
|
|
|
|
(new IntersectionObserver(() => {
|
|
|
|
updateIframeHeight();
|
|
|
|
}, {root: document.documentElement})).observe(iframe);
|
2023-04-17 04:10:22 -06:00
|
|
|
});
|
|
|
|
|
|
|
|
document.body.append(mermaidBlock);
|
2020-08-04 13:56:37 -06:00
|
|
|
} catch (err) {
|
2023-04-17 04:10:22 -06:00
|
|
|
displayError(pre, err);
|
2020-08-04 13:56:37 -06:00
|
|
|
}
|
2020-07-27 00:24:09 -06:00
|
|
|
}
|
|
|
|
}
|