protect code blocks during citation repair

This commit is contained in:
Daniel 2026-05-09 20:54:05 +02:00
parent 83d9a77160
commit a176e1b014
2 changed files with 9 additions and 2 deletions

View file

@ -1,12 +1,12 @@
export function renderAssistantMarkdown(md, sources, options) {
var opts = options || {};
var text = stripOrphanMarkdownMarkers(normalizeMarkdownText(md));
var codeBlocks = [];
text = text.replace(/```(\w+)?\n([\s\S]*?)```/g, function (_, lang, code) {
var text = String(md || '').replace(/```(\w+)?\n([\s\S]*?)```/g, function (_, lang, code) {
var idx = codeBlocks.length;
codeBlocks.push({ lang: (lang || '').toLowerCase(), code: code });
return '\n@@CODEBLOCK_' + idx + '@@\n';
});
text = stripOrphanMarkdownMarkers(normalizeMarkdownText(text));
text = renderLatexText(text, opts.katex);
text = normalizeAdjacentCitationClusters(text, sources || []);

View file

@ -129,6 +129,13 @@ test('preserves code block contents without creating citation links inside code'
assert.doesNotMatch(html, /assistant-source-1/);
});
test('does not repair source tables inside code blocks', async () => {
const { renderAssistantMarkdown } = await loadCitationModule();
const html = renderAssistantMarkdown('```md\n| Feature | Source(s) |\n|---|---|\n| Example | 1, 2 |\n```', sources);
assert.match(html, /\| Example \| 1, 2 \|/);
assert.doesNotMatch(html, /assistant-source-1/);
});
test('does not repair model-split markdown tables', async () => {
const { renderAssistantMarkdown } = await loadCitationModule();
const html = renderAssistantMarkdown('Summary Table\n| Indication for Use | Dose (IV) | Max Dose\n\n| Infusion Time | Monitoring/Precautions |\n|-----------------------------------------|----------------------------|----------|--------------|---------------------------------------|\n| Severe exacerbation unresponsive to SABA/anticholinergic | 25-75 mg/kg | 2 g\n| 20 min | BP, reflexes, respiratory status[1, 2] |', sources);