perf(mermaid): lazy load mermaid

This commit is contained in:
kangfenmao 2024-07-24 18:19:43 +08:00
parent d0280186bc
commit f81b38a362
2 changed files with 12 additions and 8 deletions

View File

@ -21,13 +21,16 @@ async function initSentry() {
} }
} }
async function initMermaid() { export async function initMermaid() {
if (!window.mermaid) {
await loadScript('https://unpkg.com/mermaid@10.9.1/dist/mermaid.min.js') await loadScript('https://unpkg.com/mermaid@10.9.1/dist/mermaid.min.js')
window.mermaid.initialize({ window.mermaid.initialize({
startOnLoad: true, startOnLoad: true,
theme: 'dark', theme: 'dark',
securityLevel: 'loose' securityLevel: 'loose'
}) })
window.mermaid.contentLoaded()
}
} }
function init() { function init() {
@ -43,7 +46,6 @@ function init() {
window.keyv.init() window.keyv.init()
initSentry() initSentry()
initMermaid()
} }
init() init()

View File

@ -5,6 +5,7 @@ import styled from 'styled-components'
import { CopyOutlined } from '@ant-design/icons' import { CopyOutlined } from '@ant-design/icons'
import { useTranslation } from 'react-i18next' import { useTranslation } from 'react-i18next'
import Mermaid from './Mermaid' import Mermaid from './Mermaid'
import { initMermaid } from '@renderer/init'
interface CodeBlockProps { interface CodeBlockProps {
children: string children: string
@ -23,6 +24,7 @@ const CodeBlock: React.FC<CodeBlockProps> = ({ children, className, ...rest }) =
} }
if (match && match[1] === 'mermaid') { if (match && match[1] === 'mermaid') {
initMermaid()
return <Mermaid chart={children} /> return <Mermaid chart={children} />
} }