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

View File

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