mirror of https://github.com/go-gitea/gitea
Git with a cup of tea, painless self-hosted git service
Mirror for internal git.with.parts use
https://git.with.parts
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
75 lines
2.6 KiB
75 lines
2.6 KiB
4 months ago
|
import {isDarkTheme} from '../utils.ts';
|
||
|
import {makeCodeCopyButton} from './codecopy.ts';
|
||
|
import {displayError} from './common.ts';
|
||
2 years ago
|
|
||
3 years ago
|
const {mermaidMaxSourceCharacters} = window.config;
|
||
4 years ago
|
|
||
1 year ago
|
// margin removal is for https://github.com/mermaid-js/mermaid/issues/4907
|
||
2 years ago
|
const iframeCss = `:root {color-scheme: normal}
|
||
|
body {margin: 0; padding: 0; overflow: hidden}
|
||
1 year ago
|
#mermaid {display: block; margin: 0 auto}
|
||
|
blockquote, dd, dl, figure, h1, h2, h3, h4, h5, h6, hr, p, pre {margin: 0}`;
|
||
4 years ago
|
|
||
3 years ago
|
export async function renderMermaid() {
|
||
|
const els = document.querySelectorAll('.markup code.language-mermaid');
|
||
|
if (!els.length) return;
|
||
4 years ago
|
|
||
3 years ago
|
const {default: mermaid} = await import(/* webpackChunkName: "mermaid" */'mermaid');
|
||
4 years ago
|
|
||
4 years ago
|
mermaid.initialize({
|
||
3 years ago
|
startOnLoad: false,
|
||
|
theme: isDarkTheme() ? 'dark' : 'neutral',
|
||
4 years ago
|
securityLevel: 'strict',
|
||
|
});
|
||
|
|
||
|
for (const el of els) {
|
||
2 years ago
|
const pre = el.closest('pre');
|
||
|
if (pre.hasAttribute('data-render-done')) continue;
|
||
3 years ago
|
|
||
2 years ago
|
const source = el.textContent;
|
||
3 years ago
|
if (mermaidMaxSourceCharacters >= 0 && source.length > mermaidMaxSourceCharacters) {
|
||
2 years ago
|
displayError(pre, new Error(`Mermaid source of ${source.length} characters exceeds the maximum allowed length of ${mermaidMaxSourceCharacters}.`));
|
||
4 years ago
|
continue;
|
||
|
}
|
||
|
|
||
|
try {
|
||
2 years ago
|
await mermaid.parse(source);
|
||
4 years ago
|
} catch (err) {
|
||
2 years ago
|
displayError(pre, err);
|
||
4 years ago
|
continue;
|
||
|
}
|
||
|
|
||
|
try {
|
||
3 years ago
|
// 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
|
||
2 years ago
|
const {svg} = await mermaid.render('mermaid', source);
|
||
|
|
||
|
const iframe = document.createElement('iframe');
|
||
9 months ago
|
iframe.classList.add('markup-render', 'tw-invisible');
|
||
2 years ago
|
iframe.srcdoc = `<html><head><style>${iframeCss}</style></head><body>${svg}</body></html>`;
|
||
|
|
||
|
const mermaidBlock = document.createElement('div');
|
||
8 months ago
|
mermaidBlock.classList.add('mermaid-block', 'is-loading', 'tw-hidden');
|
||
2 years ago
|
mermaidBlock.append(iframe);
|
||
|
|
||
|
const btn = makeCodeCopyButton();
|
||
|
btn.setAttribute('data-clipboard-text', source);
|
||
|
mermaidBlock.append(btn);
|
||
2 years ago
|
|
||
|
iframe.addEventListener('load', () => {
|
||
|
pre.replaceWith(mermaidBlock);
|
||
8 months ago
|
mermaidBlock.classList.remove('tw-hidden');
|
||
2 years ago
|
iframe.style.height = `${iframe.contentWindow.document.body.clientHeight}px`;
|
||
|
setTimeout(() => { // avoid flash of iframe background
|
||
|
mermaidBlock.classList.remove('is-loading');
|
||
9 months ago
|
iframe.classList.remove('tw-invisible');
|
||
2 years ago
|
}, 0);
|
||
|
});
|
||
|
|
||
|
document.body.append(mermaidBlock);
|
||
4 years ago
|
} catch (err) {
|
||
2 years ago
|
displayError(pre, err);
|
||
4 years ago
|
}
|
||
4 years ago
|
}
|
||
|
}
|