diff --git a/components/PrismMac.js b/components/PrismMac.js index 22fc3709..956e9f73 100644 --- a/components/PrismMac.js +++ b/components/PrismMac.js @@ -8,7 +8,6 @@ import 'prismjs/plugins/line-numbers/prism-line-numbers.css' // 所有语言的prismjs 使用autoloader引入 import 'prismjs/plugins/autoloader/prism-autoloader' // mermaid图 -import mermaid from 'mermaid' import BLOG from '@/blog.config' /** @@ -18,6 +17,7 @@ import BLOG from '@/blog.config' const PrismMac = () => { React.useEffect(() => { renderPrismMac() + renderMermaid() // 折叠代码行号bug const observer = new MutationObserver(mutationsList => { @@ -35,21 +35,10 @@ const PrismMac = () => { return <> } -function renderPrismMac() { - const container = document?.getElementById('container-inner') - const codeToolBars = container?.getElementsByClassName('code-toolbar') - - // Add line numbers - const codeBlocks = container?.getElementsByTagName('pre') - if (codeBlocks) { - Array.from(codeBlocks).forEach(item => { - if (!item.classList.contains('line-numbers')) { - item.classList.add('line-numbers') - item.style.whiteSpace = 'pre-wrap' - } - }) - } - +/** + * 将mermaid语言 渲染成图片 + */ +const renderMermaid = async() => { // 支持 Mermaid const mermaidPres = document.querySelectorAll('pre.notion-code.language-mermaid') if (mermaidPres) { @@ -73,9 +62,26 @@ function renderPrismMac() { } } if (needLoad) { - mermaid.contentLoaded() + const asyncMermaid = await import('mermaid') + asyncMermaid.default.contentLoaded() } } +} + +function renderPrismMac() { + const container = document?.getElementById('container-inner') + const codeToolBars = container?.getElementsByClassName('code-toolbar') + + // Add line numbers + const codeBlocks = container?.getElementsByTagName('pre') + if (codeBlocks) { + Array.from(codeBlocks).forEach(item => { + if (!item.classList.contains('line-numbers')) { + item.classList.add('line-numbers') + item.style.whiteSpace = 'pre-wrap' + } + }) + } // 重新渲染之前检查所有的多余text diff --git a/lib/busuanzi.js b/lib/busuanzi.js index 9255485a..439aa4b7 100644 --- a/lib/busuanzi.js +++ b/lib/busuanzi.js @@ -94,11 +94,6 @@ bszTag = { } } -// 修复Node同构代码的问题 -if (typeof document !== 'undefined') { - fetch() -} - module.exports = { fetch } diff --git a/themes/next/components/Toc.js b/themes/next/components/Toc.js index 5dbbad5f..6d98ee4a 100644 --- a/themes/next/components/Toc.js +++ b/themes/next/components/Toc.js @@ -11,10 +11,6 @@ import Progress from './Progress' * @constructor */ const Toc = ({ toc }) => { - // 无目录就直接返回空 - if (!toc || toc.length < 1) { - return <> - } // 监听滚动事件 React.useEffect(() => { window.addEventListener('scroll', actionSectionScrollSpy) @@ -58,6 +54,11 @@ const Toc = ({ toc }) => { tRef?.current?.scrollTo({ top: 28 * index, behavior: 'smooth' }) }, throttleMs)) + // 无目录就直接返回空 + if (!toc || toc.length < 1) { + return <> + } + return