From 34334eabed151cf5fa881d6b34f75107a2d3871e Mon Sep 17 00:00:00 2001 From: Jacky Zhao Date: Mon, 5 Feb 2024 20:36:31 -0800 Subject: [PATCH] perf: don't load mermaid if its not on the page --- quartz/plugins/transformers/ofm.ts | 25 +++++++++++++++---------- 1 file changed, 15 insertions(+), 10 deletions(-) diff --git a/quartz/plugins/transformers/ofm.ts b/quartz/plugins/transformers/ofm.ts index 908c23d0..f8a28c48 100644 --- a/quartz/plugins/transformers/ofm.ts +++ b/quartz/plugins/transformers/ofm.ts @@ -600,17 +600,22 @@ export const ObsidianFlavoredMarkdown: QuartzTransformerPlugin if (opts.mermaid) { js.push({ script: ` - import mermaid from 'https://cdn.jsdelivr.net/npm/mermaid/dist/mermaid.esm.min.mjs'; - const darkMode = document.documentElement.getAttribute('saved-theme') === 'dark' - mermaid.initialize({ - startOnLoad: false, - securityLevel: 'loose', - theme: darkMode ? 'dark' : 'default' - }); + let mermaidImport = undefined document.addEventListener('nav', async () => { - await mermaid.run({ - querySelector: '.mermaid' - }) + if (document.querySelector("code.mermaid")) { + mermaidImport ||= await import('https://cdn.jsdelivr.net/npm/mermaid/dist/mermaid.esm.min.mjs') + const mermaid = mermaidImport.default + const darkMode = document.documentElement.getAttribute('saved-theme') === 'dark' + mermaid.initialize({ + startOnLoad: false, + securityLevel: 'loose', + theme: darkMode ? 'dark' : 'default' + }) + + await mermaid.run({ + querySelector: '.mermaid' + }) + } }); `, loadTime: "afterDOMReady",