- 在 js/core/app-shell.js:1275 引入 window.mermaid.parse(code) 语法校验,若捕获异常则抛出带有“Mermaid

语法错误”描述的错误,阻断后续渲染流程并避免生成无效图表。
  - 在 js/core/app-shell.js:1244 的渲染异常分支中统一处理错误信息,既在查看区域展示,又在输入框末尾追加
    (避免重复追加),并自动调整输入框高度,方便用户基于错误提示修改 Mermaid 代码。
This commit is contained in:
史悦
2025-10-28 09:19:07 +08:00
parent 298e421f7d
commit 7bcfadde59

View File

@@ -1238,11 +1238,22 @@
} catch (error) { } catch (error) {
this.destroyMermaidPanZoom(); this.destroyMermaidPanZoom();
console.error('Mermaid 渲染失败:', error); console.error('Mermaid 渲染失败:', error);
const errorMessage = error.message || '未知错误';
this.el.viewer.innerHTML = ` this.el.viewer.innerHTML = `
<div class="p-4 text-center text-red-500 font-bold"> <div class="p-4 text-center text-red-500 font-bold">
Mermaid 渲染失败:${Utils.escapeHtml(error.message || '未知错误')} Mermaid 渲染失败:${Utils.escapeHtml(errorMessage)}
</div> </div>
`; `;
if (this.el.chatInput) {
const existingValue = this.el.chatInput.value || '';
const appendedValue = existingValue.includes(errorMessage)
? existingValue
: existingValue
? `${existingValue}\n${errorMessage}`
: errorMessage;
this.el.chatInput.value = appendedValue;
Utils.autoResizeTextarea(this.el.chatInput);
}
} }
} }
@@ -1256,6 +1267,15 @@
if (!code.trim()) { if (!code.trim()) {
throw new Error('缺少 Mermaid 代码,无法渲染'); throw new Error('缺少 Mermaid 代码,无法渲染');
} }
try {
window.mermaid.parse(code);
} catch (parseError) {
const syntaxMessage =
parseError?.str || parseError?.message || '未知错误';
const error = new Error(`Mermaid 语法错误:${syntaxMessage}`);
error.isMermaidSyntaxError = true;
throw error;
}
const { svg } = await window.mermaid.render("mermaidSvg", code); const { svg } = await window.mermaid.render("mermaidSvg", code);
const updatedArtifact = { const updatedArtifact = {
...artifact, ...artifact,