refac: sources and citations

This commit is contained in:
Timothy Jaeryang Baek committed 2025-11-23 18:27:57 -05:00
1 parent b0491886bc
commit ec45d77ce9
11 files changed
+201 -62

No files matched your search

+1 -25
View File
@@ -32,7 +32,7 @@ function escapeRegExp(string: string): string {
return string.replace(/[.*+?^${}()|[\]\\]/g, '\\$&');
}
export const replaceTokens = (content, sourceIds, char, user) => {
export const replaceTokens = (content, char, user) => {
const tokens = [
{ regex: /{{char}}/gi, replacement: char },
{ regex: /{{user}}/gi, replacement: user },
@@ -67,30 +67,6 @@ export const replaceTokens = (content, sourceIds, char, user) => {
}
});
if (Array.isArray(sourceIds)) {
// 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 <source_id> tag
const sources = indices
.map((idx) => {
const sourceId = sourceIds[idx - 1];
return sourceId
? `<source_id data="${idx}" title="${encodeURIComponent(sourceId)}" />`
: `[${idx}]`;
})
.join('');
return sources;
});
}
return segment;
});