mermaid 样式调整

This commit is contained in:
tangly1024
2022-12-07 15:12:29 +08:00
parent 249416980b
commit 1a6f7e3cb0
3 changed files with 23 additions and 34 deletions

View File

@@ -9,17 +9,15 @@ import 'prismjs/plugins/line-numbers/prism-line-numbers.css'
// mermaid图 // mermaid图
import mermaid from 'mermaid' import mermaid from 'mermaid'
import { useGlobal } from '@/lib/global'
/** /**
* @author https://github.com/txs/ * @author https://github.com/txs/
* @returns * @returns
*/ */
const PrismMac = () => { const PrismMac = () => {
const { theme } = useGlobal()
React.useEffect(() => { React.useEffect(() => {
renderPrismMac() renderPrismMac()
}, [theme]) }, [])
return <></> return <></>
} }
@@ -27,14 +25,6 @@ function renderPrismMac() {
const container = document?.getElementById('container-inner') const container = document?.getElementById('container-inner')
const codeToolBars = container?.getElementsByClassName('code-toolbar') const codeToolBars = container?.getElementsByClassName('code-toolbar')
if (codeToolBars) {
Array.from(codeToolBars).forEach(item => {
const codeBlocks = item.getElementsByTagName('pre')
if (codeBlocks.length === 0) {
item.remove()
}
})
}
// Add line numbers // Add line numbers
const codeBlocks = container?.getElementsByTagName('pre') const codeBlocks = container?.getElementsByTagName('pre')
if (codeBlocks) { if (codeBlocks) {
@@ -47,36 +37,31 @@ function renderPrismMac() {
} }
// 支持 Mermaid // 支持 Mermaid
const mermaids = document.querySelectorAll('.notion-code .language-mermaid') const mermaidPres = document.querySelectorAll('pre.notion-code.language-mermaid')
console.log('检查 mermaids', mermaids) if (mermaidPres) {
if (mermaids) { for (const e of mermaidPres) {
for (const e of mermaids) { const chart = e.querySelector('code').textContent
const chart = e.firstChild.textContent console.log(e.parentElement)
if (e.firstElementChild) { if (chart && !e.querySelector('.mermaid')) {
e.parentElement.remove() const m = document.createElement('div')
continue m.className = 'mermaid'
m.innerHTML = chart
e.appendChild(m)
} }
if (chart) {
e.parentElement.innerHTML = `<div class="mermaid">${chart}</div>`
}
}
}
// mermaid 样式
const mermaidsCodeBar = document.querySelectorAll('.language-mermaid')
if (mermaidsCodeBar) {
for (const e of mermaidsCodeBar) {
e.parentElement.classList.replace('code-toolbar', 'w-full')
} }
} }
const mermaidsSvg = document.querySelectorAll('.mermaid') const mermaidsSvg = document.querySelectorAll('.mermaid')
if (mermaidsSvg) { if (mermaidsSvg) {
let needLoad = false
for (const e of mermaidsSvg) { for (const e of mermaidsSvg) {
if (e?.firstChild?.nodeName !== 'svg') { if (e?.firstChild?.nodeName !== 'svg') {
mermaid.contentLoaded() needLoad = true
} }
} }
if (needLoad) {
mermaid.contentLoaded()
}
} }
// 重新渲染之前检查所有的多余text // 重新渲染之前检查所有的多余text

View File

@@ -15,7 +15,7 @@ const WalineComponent = (props) => {
const router = useRouter() const router = useRouter()
const updateWaline = url => { const updateWaline = url => {
if (url !== path) { if (url !== path && waline) {
waline.update(props) waline.update(props)
} }
} }

View File

@@ -60,6 +60,10 @@ pre[class*='language-'] {
} }
pre[class*="language-mermaid"] { pre[class*="language-mermaid"] {
background: transparent !important; @apply bg-gray-50 dark:bg-gray-200 !important;
@apply dark:bg-gray-200 !important; }
/* mermaid 原文隐藏 */
code.language-mermaid {
display:none
} }