File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change @@ -1347,6 +1347,8 @@ export class CodeBlockView implements NodeView {
13471347
13481348 // 排队执行,避免多个 mermaid 块同时 render 互相干扰
13491349 mermaidRenderQueue = mermaidRenderQueue . then ( async ( ) => {
1350+ // renderId 需要在 try 外声明,以便 catch 中清理 mermaid 遗留的临时元素
1351+ const renderId = `mermaid-${ ++ mermaidIdCounter } ` ;
13501352 try {
13511353 const mermaid = await import ( "mermaid" ) ;
13521354 const isDark = detectDarkTheme ( ) ;
@@ -1358,12 +1360,17 @@ export class CodeBlockView implements NodeView {
13581360 themeVariables : getMermaidThemeVariables ( ) ,
13591361 } ) ;
13601362
1361- const renderId = `mermaid-${ ++ mermaidIdCounter } ` ;
13621363 const { svg } = await mermaid . default . render ( renderId , content ) ;
13631364 preview . innerHTML = svg ;
13641365 // 根据实际背景色修正文本颜色
13651366 fixMermaidTextContrast ( preview ) ;
13661367 } catch ( error ) {
1368+ // mermaid.render() 语法错误时会在 removeTempElements() 之前抛出异常,
1369+ // 导致临时 DOM 元素(<div id="d..."> / <svg> / <iframe>)遗留在 document.body 中。
1370+ // 这些孤儿元素不断累积会干扰页面布局,导致 tab 栏等 UI 元素显示异常。
1371+ document . getElementById ( `d${ renderId } ` ) ?. remove ( ) ;
1372+ document . getElementById ( renderId ) ?. remove ( ) ;
1373+ document . getElementById ( `i${ renderId } ` ) ?. remove ( ) ;
13671374 preview . innerHTML = `<div class="milkup-mermaid-error">Mermaid 渲染错误</div>` ;
13681375 }
13691376 } ) ;
You can’t perform that action at this time.
0 commit comments