@@ -2,7 +2,7 @@ import mermaid from 'mermaid'
22import CryptoJs from 'crypto-js'
33
44export const processMermaid = {
5- fn : ( ) => { }
5+ fn : ( callback ) => { }
66}
77
88const computeHash = ( str ) => {
@@ -43,7 +43,10 @@ export const mermaidPlugin = (md, options = {}) => {
4343
4444
4545 if ( cache . has ( hash ) ) {
46- return `<div data-mermaid-hash="${ hash } ">${ cache . get ( hash ) } </div>`
46+ return `
47+ ${ defaultFence ( tokens , idx , opts , env , self ) }
48+ <div data-mermaid-hash="${ hash } ">${ cache . get ( hash ) } </div>
49+ `
4750 }
4851
4952 return `
@@ -59,7 +62,7 @@ export const mermaidPlugin = (md, options = {}) => {
5962 }
6063
6164 // 后处理渲染 mermaid
62- const renderMermaid = async ( container ) => {
65+ const renderMermaid = async ( container , callback = ( ) => { } ) => {
6366 const encodedContent = container . dataset . mermaidContent
6467 const content = decodeURIComponent ( encodedContent )
6568 const hash = container . dataset . mermaidHash
@@ -93,6 +96,8 @@ export const mermaidPlugin = (md, options = {}) => {
9396 fragment . appendChild ( wrapper )
9497
9598 container . replaceWith ( fragment )
99+
100+ callback ( )
96101 } catch ( err ) {
97102 console . error ( 'Mermaid 渲染失败:' , err )
98103 container . dataset . mermaidStatus = 'error'
@@ -101,7 +106,7 @@ export const mermaidPlugin = (md, options = {}) => {
101106 }
102107
103108 // 全局渲染控制器
104- const processContainers = ( ) => {
109+ const processContainers = ( callback = ( ) => { } ) => {
105110 const containers = document . querySelectorAll ( `
106111 div[data-mermaid-status="pending"]
107112 ` ) as NodeListOf < HTMLElement >
@@ -116,7 +121,7 @@ export const mermaidPlugin = (md, options = {}) => {
116121 container . dataset . mermaidStatus = 'pending'
117122 }
118123 nextTick ( ( ) => {
119- renderMermaid ( container )
124+ renderMermaid ( container , callback )
120125 } )
121126 } )
122127 }
@@ -131,8 +136,8 @@ export const mermaidPlugin = (md, options = {}) => {
131136 } )
132137
133138 // 触发 Mermaid 图表渲染 export
134- processMermaid . fn = ( ) => {
135- processContainers ( )
139+ processMermaid . fn = ( callback = ( ) => { } ) => {
140+ processContainers ( callback )
136141 }
137142}
138143
0 commit comments