Skip to content

Commit eafd354

Browse files
committed
fix: 修复聊天数学公式渲染错误
1 parent af8f1ba commit eafd354

File tree

1 file changed

+50
-2
lines changed

1 file changed

+50
-2
lines changed

src/chatPanel.ts

Lines changed: 50 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import { getCurrentOperationController, resetCurrentOperationController } from '
44
import path from 'path';
55
import * as fs from "fs";
66
import * as apiTools from './apiTools';
7+
import {getOutputChannel} from './extension';
78

89
// Webview 输出通道实现
910
class WebviewOutputChannel implements vscode.OutputChannel {
@@ -21,10 +22,12 @@ class WebviewOutputChannel implements vscode.OutputChannel {
2122

2223
append(value: string): void {
2324
this.webview.postMessage({ role: 'model', content: value });
25+
//getOutputChannel().append(value);
2426
}
2527

2628
appendLine(value: string): void {
2729
this.append(value + '\n');
30+
//getOutputChannel().appendLine(value);
2831
}
2932

3033
clear(): void {
@@ -284,6 +287,47 @@ export class ChatPanel {
284287
});
285288
}
286289
290+
/**
291+
* fnRenderDisplayMath
292+
* 把 container.innerHTML 里所有的 $$…$$ 块,
293+
* 用 katex.renderToString 直接渲染成 HTML
294+
*/
295+
function fnRenderDisplayMath(webviewDiv)
296+
{
297+
// 1) 获取原始 HTML
298+
const strRawHtml = webviewDiv.innerHTML;
299+
300+
// 2) 匹配所有 $$…$$(非贪婪)
301+
const rgxDisplayMath = /\$\$([\s\S]+?)\$\$/g;
302+
303+
// 3) 替换成 katex 渲染结果
304+
const strReplacedHtml = strRawHtml.replace(rgxDisplayMath
305+
,
306+
(strMatch, strInnerTex) =>
307+
{
308+
try
309+
{
310+
// trim 首尾空白,保持 display 模式
311+
const strTex = strInnerTex.replace(/^\s+|\s+$/g, '');
312+
return katex.renderToString(strTex
313+
,
314+
{
315+
displayMode: true,
316+
throwOnError: false
317+
});
318+
}
319+
catch (err)
320+
{
321+
console.error('KaTeX render error:', err);
322+
// 渲染失败就返回原始 $$…$$
323+
return strMatch;
324+
}
325+
});
326+
327+
// 4) 更新回 DOM
328+
webviewDiv.innerHTML = strReplacedHtml;
329+
}
330+
287331
// 渲染消息内容
288332
function renderMessage(role, content, index) {
289333
const lastChild = chat.lastElementChild;
@@ -301,11 +345,14 @@ export class ChatPanel {
301345
302346
if (role === 'model') {
303347
targetDiv.innerHTML = marked.parse(targetDiv.dataset.markdownContent, {
304-
breaks: true,
348+
breaks: false,
305349
mangle: false,
306350
headerIds: false,
307351
highlight: (code, lang) => hljs.highlight(hljs.getLanguage(lang) ? lang : 'plaintext', code).value
308352
});
353+
354+
fnRenderDisplayMath(targetDiv);
355+
309356
renderMathInElement(targetDiv, {
310357
delimiters: [
311358
{ left: '$$', right: '$$', display: true },
@@ -456,7 +503,8 @@ export class ChatPanel {
456503

457504
try {
458505
const tools = message.webSearch ? [apiTools.searchTool] : null;
459-
const systemPrompt = message.webSearch ? "每次回答问题前,一定要先上网搜索一下再回答。" : 'You are a helpful assistant. Always format answers with Markdown.'
506+
const nomalSystemPromot = "用markdown输出。";
507+
const systemPrompt = message.webSearch ? "每次回答问题前,一定要先上网搜索一下再回答。" + nomalSystemPromot : nomalSystemPromot;
460508
const response = await callDeepSeekApi(
461509
this.conversation.map(msg => ({ role: msg.role, content: msg.content })),
462510
systemPrompt,

0 commit comments

Comments
 (0)