@@ -78,11 +78,15 @@ export const mermaidPlugin = (md, options = {}) => {
7878 if ( cache . has ( hash ) ) {
7979 svg = cache . get ( hash )
8080 } else {
81- // const { isValid } = await verifyMermaid(content)
8281
83- // if (!isValid) {
84- // return
85- // }
82+ const { isValid } = await verifyMermaid ( content )
83+
84+ if ( ! isValid ) {
85+ cache . set ( hash , content )
86+ container . dataset . mermaidStatus = 'error'
87+ container . innerHTML = `<pre>渲染失败:\n${ content } \n</pre>`
88+ return
89+ }
8690
8791 // 使用唯一 ID 渲染(避免图表冲突)
8892 const { svg : renderedSvg } = await mermaid . render ( `mermaid-${ hash } ` , content )
@@ -97,8 +101,9 @@ export const mermaidPlugin = (md, options = {}) => {
97101
98102 container . replaceWith ( fragment )
99103 } catch ( err ) {
100- // console.error('Mermaid 渲染失败:', err)
104+ console . error ( 'Mermaid 渲染失败:' , err )
101105 container . dataset . mermaidStatus = 'error'
106+ container . innerHTML = `<pre>渲染失败:\n${ content } \n</pre>`
102107 } finally {
103108 pendingQueue . delete ( hash )
104109 }
@@ -107,8 +112,7 @@ export const mermaidPlugin = (md, options = {}) => {
107112 // 全局渲染控制器
108113 const processContainers = ( ) => {
109114 const containers = document . querySelectorAll ( `
110- div[data-mermaid-status="pending"],
111- div[data-mermaid-hash]:not([data-mermaid-status])
115+ div[data-mermaid-status="pending"]
112116 ` ) as NodeListOf < HTMLElement >
113117
114118
@@ -120,9 +124,7 @@ export const mermaidPlugin = (md, options = {}) => {
120124 if ( container . dataset . mermaidStatus !== 'pending' ) {
121125 container . dataset . mermaidStatus = 'pending'
122126 }
123- nextTick ( ( ) => {
124- renderMermaid ( container )
125- } )
127+ renderMermaid ( container )
126128 } )
127129 }
128130
@@ -137,7 +139,7 @@ export const mermaidPlugin = (md, options = {}) => {
137139
138140 // 触发 Mermaid 图表渲染 export
139141 processMermaid . fn = ( ) => {
140- processContainers ( )
142+ requestAnimationFrame ( processContainers )
141143 }
142144}
143145
0 commit comments