fix: enforce Vancouver citation clusters

This commit is contained in:
Daniel 2026-05-06 23:07:43 +02:00
parent dd4e3bad61
commit 8735cae625
2 changed files with 13 additions and 9 deletions

View file

@ -244,11 +244,14 @@
} else {
html = fallbackMarkdown(text);
}
html = html.replace(/\[(\d+)\]/g, function (_, n) {
var source = sources[Number(n) - 1];
if (!source) return '[' + n + ']';
var title = source ? source.title || source.resource || 'Source' : 'Source';
return '<a class="assistant-cite" href="#assistant-source-' + n + '" title="' + escapeHtml(title) + '">[' + n + ']</a>';
html = html.replace(/\[((?:\d+\s*,\s*)*\d+)\]/g, function (match, cluster) {
var nums = cluster.split(',').map(function (n) { return Number(n.trim()); }).filter(function (n) { return Number.isInteger(n) && n > 0; });
if (!nums.length || nums.some(function (n) { return !sources[n - 1]; })) return match;
return '[' + nums.map(function (n) {
var source = sources[n - 1];
var title = source ? source.title || source.resource || 'Source' : 'Source';
return '<a class="assistant-cite" href="#assistant-source-' + n + '" title="' + escapeHtml(title) + '">' + n + '</a>';
}).join(', ') + ']';
});
html = html.replace(/@@CODEBLOCK_(\d+)@@/g, function (_, idx) {
var block = codeBlocks[Number(idx)] || { lang: '', code: '' };

View file

@ -446,17 +446,18 @@ function normalizeCitationOrder(answer, sources) {
});
var rewritten = answer.replace(/\[((?:\d+\s*,\s*)*\d+)\]/g, function(match, cluster) {
var nums = parseCitationCluster(cluster).map(function(oldNum) { return oldToNew[oldNum]; }).filter(Boolean);
if (!nums.length) return match;
var oldNums = parseCitationCluster(cluster);
var nums = oldNums.map(function(oldNum) { return oldToNew[oldNum]; });
if (!nums.length || nums.some(function(n) { return !n; })) return match;
nums = Array.from(new Set(nums)).sort(function(a, b) { return a - b; });
return '[' + nums.join(',') + ']';
return '[' + nums.join(', ') + ']';
});
return { answer: rewritten, sources: reordered };
}
function parseCitationCluster(cluster) {
return String(cluster || '').split(',').map(function(n) { return Number(n.trim()); }).filter(function(n) { return Number.isFinite(n); });
return String(cluster || '').split(',').map(function(n) { return Number(n.trim()); }).filter(function(n) { return Number.isInteger(n) && n > 0; });
}
async function getAvailableExamples() {