From a176e1b01478dd7ecd2422df2c8278c68d54b5f5 Mon Sep 17 00:00:00 2001 From: Daniel Date: Sat, 9 May 2026 20:54:05 +0200 Subject: [PATCH] protect code blocks during citation repair --- public/js/assistant/citations.js | 4 ++-- test/assistant-citations.test.js | 7 +++++++ 2 files changed, 9 insertions(+), 2 deletions(-) diff --git a/public/js/assistant/citations.js b/public/js/assistant/citations.js index b5f2331..633c483 100644 --- a/public/js/assistant/citations.js +++ b/public/js/assistant/citations.js @@ -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 || []); diff --git a/test/assistant-citations.test.js b/test/assistant-citations.test.js index 1871c80..66c0076 100644 --- a/test/assistant-citations.test.js +++ b/test/assistant-citations.test.js @@ -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);