🐛 fix(ai): 修正 SQL 代码块 Markdown 换行渲染

- 为 AI markdown 渲染补充 fenced code block 预处理
- 修正 opening/closing fence 缺少换行时的代码块解析失败
- 补充回归测试并更新 issue backlog 记录

Fixes #369
This commit is contained in:
Syngnat
2026-04-17 14:37:36 +08:00
parent 7cb46f9f69
commit f3193f0933
4 changed files with 34 additions and 1 deletions

View File

@@ -0,0 +1,11 @@
import { describe, expect, it } from 'vitest';
import { normalizeAiMarkdown } from './aiMarkdown';
describe('normalizeAiMarkdown', () => {
it('inserts a missing newline after the fenced code language marker', () => {
expect(normalizeAiMarkdown('```sqlSELECT COUNT(*) AS order_count\nFROM customer_order;\n```')).toBe(
'```sql\nSELECT COUNT(*) AS order_count\nFROM customer_order;\n```',
);
});
});

View File

@@ -0,0 +1,13 @@
export const normalizeAiMarkdown = (content: string): string => {
let text = String(content || '').replace(/\r\n/g, '\n');
const knownFenceLanguages = [
'sql', 'mermaid', 'json', 'javascript', 'typescript', 'ts', 'js', 'tsx', 'jsx',
'bash', 'sh', 'shell', 'python', 'py', 'go', 'java', 'yaml', 'yml', 'html', 'css',
'xml', 'markdown', 'md', 'text', 'plaintext', 'vue', 'php', 'ruby', 'rust', 'toml',
'ini', 'diff',
];
const fencePattern = new RegExp(`(^|\\n)\`\`\`(${knownFenceLanguages.join('|')})([^\\n])`, 'gi');
text = text.replace(fencePattern, '$1```$2\n$3');
text = text.replace(/([^\n])```(?=\n|$)/g, '$1\n```');
return text;
};