Skip to content

Commit

Permalink
Merge pull request #1194 from tangly1024/fix/mermaid
Browse files Browse the repository at this point in the history
fix-mermaid
  • Loading branch information
tangly1024 committed Jun 23, 2023
2 parents 346b975 + 7f10e05 commit c5ddc8c
Showing 1 changed file with 27 additions and 27 deletions.
54 changes: 27 additions & 27 deletions components/PrismMac.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,36 +41,36 @@ const PrismMac = () => {
* 将mermaid语言 渲染成图片
*/
const renderMermaid = async() => {
// 支持 Mermaid
const mermaidPres = document.querySelectorAll('pre.notion-code.language-mermaid')
if (mermaidPres) {
for (const e of mermaidPres) {
const chart = e.querySelector('code').textContent
if (chart && !e.querySelector('.mermaid')) {
const m = document.createElement('div')
m.className = 'mermaid'
m.innerHTML = chart
e.appendChild(m)
}
}
}
const observer = new MutationObserver(async mutationsList => {
for (const m of mutationsList) {
if (m.target.className === 'notion-code language-mermaid') {
const chart = m.target.querySelector('code').textContent
if (chart && !m.target.querySelector('.mermaid')) {
const mermaidChart = document.createElement('div')
mermaidChart.className = 'mermaid'
mermaidChart.innerHTML = chart
m.target.appendChild(mermaidChart)
}

const mermaidsSvg = document.querySelectorAll('.mermaid')
if (mermaidsSvg) {
let needLoad = false
for (const e of mermaidsSvg) {
if (e?.firstChild?.nodeName !== 'svg') {
needLoad = true
const mermaidsSvg = document.querySelectorAll('.mermaid')
if (mermaidsSvg) {
let needLoad = false
for (const e of mermaidsSvg) {
if (e?.firstChild?.nodeName !== 'svg') {
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')
const url = await loadExternalResource(BLOG.MERMAID_CDN, 'js')
const mermaid = window.mermaid
console.log('mermaid加载成功', url, mermaid)
mermaid.contentLoaded()
}
}
})
observer.observe(document.querySelector('#container'), { attributes: true, subtree: true })
}

function renderPrismMac() {
Expand Down

0 comments on commit c5ddc8c

Please sign in to comment.