update to mermaid v10 (#23178)
fix #23153 --------- Co-authored-by: silverwind <me@silverwind.io>
This commit is contained in:
parent
787a05a3a2
commit
af4b00c283
4 changed files with 233 additions and 68 deletions
|
@ -37,14 +37,10 @@ export async function renderMermaid() {
|
|||
continue;
|
||||
}
|
||||
|
||||
let valid;
|
||||
try {
|
||||
valid = mermaid.parse(source);
|
||||
await mermaid.parse(source);
|
||||
} catch (err) {
|
||||
displayError(el, err);
|
||||
}
|
||||
|
||||
if (!valid) {
|
||||
el.closest('pre').classList.remove('is-loading');
|
||||
continue;
|
||||
}
|
||||
|
@ -52,22 +48,25 @@ export async function renderMermaid() {
|
|||
try {
|
||||
// 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
|
||||
mermaid.mermaidAPI.render('mermaid', source, (svgStr) => {
|
||||
const heightStr = (svgStr.match(/viewBox="(.+?)"/) || ['', ''])[1].split(/\s+/)[3];
|
||||
if (!heightStr) return displayError(el, new Error('Could not determine chart height'));
|
||||
const iframe = document.createElement('iframe');
|
||||
iframe.classList.add('markup-render');
|
||||
iframe.sandbox = 'allow-scripts';
|
||||
iframe.style.height = `${Math.ceil(parseFloat(heightStr))}px`;
|
||||
iframe.srcdoc = `<html><head><style>${iframeCss}</style></head><body>${svgStr}</body></html>`;
|
||||
const mermaidBlock = document.createElement('div');
|
||||
mermaidBlock.classList.add('mermaid-block');
|
||||
mermaidBlock.append(iframe);
|
||||
const btn = makeCodeCopyButton();
|
||||
btn.setAttribute('data-clipboard-text', source);
|
||||
mermaidBlock.append(btn);
|
||||
el.closest('pre').replaceWith(mermaidBlock);
|
||||
});
|
||||
const {svg} = await mermaid.render('mermaid', source);
|
||||
const heightStr = (svg.match(/viewBox="(.+?)"/) || ['', ''])[1].split(/\s+/)[3];
|
||||
if (!heightStr) return displayError(el, new Error('Could not determine chart height'));
|
||||
|
||||
const iframe = document.createElement('iframe');
|
||||
iframe.classList.add('markup-render');
|
||||
iframe.sandbox = 'allow-scripts';
|
||||
iframe.style.height = `${Math.ceil(parseFloat(heightStr))}px`;
|
||||
iframe.srcdoc = `<html><head><style>${iframeCss}</style></head><body>${svg}</body></html>`;
|
||||
|
||||
const mermaidBlock = document.createElement('div');
|
||||
mermaidBlock.classList.add('mermaid-block');
|
||||
mermaidBlock.append(iframe);
|
||||
|
||||
const btn = makeCodeCopyButton();
|
||||
btn.setAttribute('data-clipboard-text', source);
|
||||
|
||||
mermaidBlock.append(btn);
|
||||
el.closest('pre').replaceWith(mermaidBlock);
|
||||
} catch (err) {
|
||||
displayError(el, err);
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue