gitea源码

mermaid.ts 3.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. import {isDarkTheme} from '../utils.ts';
  2. import {makeCodeCopyButton} from './codecopy.ts';
  3. import {displayError} from './common.ts';
  4. import {queryElems} from '../utils/dom.ts';
  5. import {html, htmlRaw} from '../utils/html.ts';
  6. const {mermaidMaxSourceCharacters} = window.config;
  7. // margin removal is for https://github.com/mermaid-js/mermaid/issues/4907
  8. const iframeCss = `:root {color-scheme: normal}
  9. body {margin: 0; padding: 0; overflow: hidden}
  10. #mermaid {display: block; margin: 0 auto}
  11. blockquote, dd, dl, figure, h1, h2, h3, h4, h5, h6, hr, p, pre {margin: 0}`;
  12. export async function initMarkupCodeMermaid(elMarkup: HTMLElement): Promise<void> {
  13. // .markup code.language-mermaid
  14. queryElems(elMarkup, 'code.language-mermaid', async (el) => {
  15. const {default: mermaid} = await import(/* webpackChunkName: "mermaid" */'mermaid');
  16. mermaid.initialize({
  17. startOnLoad: false,
  18. theme: isDarkTheme() ? 'dark' : 'neutral',
  19. securityLevel: 'strict',
  20. suppressErrorRendering: true,
  21. });
  22. const pre = el.closest('pre');
  23. if (pre.hasAttribute('data-render-done')) return;
  24. const source = el.textContent;
  25. if (mermaidMaxSourceCharacters >= 0 && source.length > mermaidMaxSourceCharacters) {
  26. displayError(pre, new Error(`Mermaid source of ${source.length} characters exceeds the maximum allowed length of ${mermaidMaxSourceCharacters}.`));
  27. return;
  28. }
  29. try {
  30. await mermaid.parse(source);
  31. } catch (err) {
  32. displayError(pre, err);
  33. return;
  34. }
  35. try {
  36. // can't use bindFunctions here because we can't cross the iframe boundary. This
  37. // means js-based interactions won't work but they aren't intended to work either
  38. const {svg} = await mermaid.render('mermaid', source);
  39. const iframe = document.createElement('iframe');
  40. iframe.classList.add('markup-content-iframe', 'tw-invisible');
  41. iframe.srcdoc = html`<html><head><style>${htmlRaw(iframeCss)}</style></head><body>${htmlRaw(svg)}</body></html>`;
  42. const mermaidBlock = document.createElement('div');
  43. mermaidBlock.classList.add('mermaid-block', 'is-loading', 'tw-hidden');
  44. mermaidBlock.append(iframe);
  45. const btn = makeCodeCopyButton();
  46. btn.setAttribute('data-clipboard-text', source);
  47. mermaidBlock.append(btn);
  48. const updateIframeHeight = () => {
  49. const body = iframe.contentWindow?.document?.body;
  50. if (body) {
  51. iframe.style.height = `${body.clientHeight}px`;
  52. }
  53. };
  54. iframe.addEventListener('load', () => {
  55. pre.replaceWith(mermaidBlock);
  56. mermaidBlock.classList.remove('tw-hidden');
  57. updateIframeHeight();
  58. setTimeout(() => { // avoid flash of iframe background
  59. mermaidBlock.classList.remove('is-loading');
  60. iframe.classList.remove('tw-invisible');
  61. }, 0);
  62. // update height when element's visibility state changes, for example when the diagram is inside
  63. // a <details> + <summary> block and the <details> block becomes visible upon user interaction, it
  64. // would initially set a incorrect height and the correct height is set during this callback.
  65. (new IntersectionObserver(() => {
  66. updateIframeHeight();
  67. }, {root: document.documentElement})).observe(iframe);
  68. });
  69. document.body.append(mermaidBlock);
  70. } catch (err) {
  71. displayError(pre, err);
  72. }
  73. });
  74. }