Merge pull request #1194 from tangly1024/fix/mermaid

fix-mermaid
This commit is contained in:
tangly1024
2023-06-23 20:24:17 +08:00
committed by GitHub

View File

@@ -41,36 +41,36 @@ const PrismMac = () => {
* 将mermaid语言 渲染成图片 * 将mermaid语言 渲染成图片
*/ */
const renderMermaid = async() => { const renderMermaid = async() => {
// 支持 Mermaid const observer = new MutationObserver(async mutationsList => {
const mermaidPres = document.querySelectorAll('pre.notion-code.language-mermaid') for (const m of mutationsList) {
if (mermaidPres) { if (m.target.className === 'notion-code language-mermaid') {
for (const e of mermaidPres) { const chart = m.target.querySelector('code').textContent
const chart = e.querySelector('code').textContent if (chart && !m.target.querySelector('.mermaid')) {
if (chart && !e.querySelector('.mermaid')) { const mermaidChart = document.createElement('div')
const m = document.createElement('div') mermaidChart.className = 'mermaid'
m.className = 'mermaid' mermaidChart.innerHTML = chart
m.innerHTML = chart m.target.appendChild(mermaidChart)
e.appendChild(m) }
}
}
}
const mermaidsSvg = document.querySelectorAll('.mermaid') const mermaidsSvg = document.querySelectorAll('.mermaid')
if (mermaidsSvg) { if (mermaidsSvg) {
let needLoad = false let needLoad = false
for (const e of mermaidsSvg) { for (const e of mermaidsSvg) {
if (e?.firstChild?.nodeName !== 'svg') { if (e?.firstChild?.nodeName !== 'svg') {
needLoad = true needLoad = true
}
}
if (needLoad) {
const url = await loadExternalResource(BLOG.MERMAID_CDN, 'js')
const mermaid = window.mermaid
console.log('mermaid加载成功', url, mermaid)
mermaid.contentLoaded()
}
}
} }
} }
if (needLoad) { })
// const asyncMermaid = await import('mermaid') observer.observe(document.querySelector('#container'), { attributes: true, subtree: true })
const url = await loadExternalResource(BLOG.MERMAID_CDN, 'js')
const mermaid = window.mermaid
console.log('mermaid加载成功', url, mermaid)
mermaid.contentLoaded()
}
}
} }
function renderPrismMac() { function renderPrismMac() {