From 3c47e49cf021d040c1863a365f4d7817d83a329c Mon Sep 17 00:00:00 2001 From: Timothy Jaeryang Baek Date: Tue, 7 Oct 2025 12:46:18 -0500 Subject: [PATCH] refac: source parsing --- src/lib/utils/index.ts | 26 ++++++++++++++++++++------ 1 file changed, 20 insertions(+), 6 deletions(-) diff --git a/src/lib/utils/index.ts b/src/lib/utils/index.ts index 85000b4448..c1ed987766 100644 --- a/src/lib/utils/index.ts +++ b/src/lib/utils/index.ts @@ -68,12 +68,26 @@ export const replaceTokens = (content, sourceIds, char, user) => { }); if (Array.isArray(sourceIds)) { - sourceIds.forEach((sourceId, idx) => { - const regex = new RegExp(`\\[${idx + 1}\\]`, 'g'); - segment = segment.replace( - regex, - `` - ); + // Match both [1], [2], and [1,2,3] forms + const multiRefRegex = /\[([\d,\s]+)\]/g; + segment = segment.replace(multiRefRegex, (match, group) => { + // Extract numbers like 1,2,3 + const indices = group + .split(',') + .map((n) => parseInt(n.trim(), 10)) + .filter((n) => !isNaN(n)); + + // Replace each index with a tag + const sources = indices + .map((idx) => { + const sourceId = sourceIds[idx - 1]; + return sourceId + ? `` + : match; + }) + .join(''); + + return sources; }); }