Skip to content

Commit 7ff7199

Browse files
committed
refac: citations
1 parent a1dc266 commit 7ff7199

File tree

3 files changed

+21
-17
lines changed

3 files changed

+21
-17
lines changed

src/lib/components/chat/Messages/Citations.svelte

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,7 @@
100100
<div class="flex text-xs font-medium flex-wrap">
101101
{#each citations as citation, idx}
102102
<button
103-
id={`source-${citation.source.name}`}
103+
id={`source-${idx}`}
104104
class="no-toggle outline-none flex dark:text-gray-300 p-1 bg-white dark:bg-gray-900 rounded-xl max-w-96"
105105
on:click={() => {
106106
showCitationModal = true;
@@ -179,7 +179,7 @@
179179
<div class="flex text-xs font-medium flex-wrap">
180180
{#each citations as citation, idx}
181181
<button
182-
id={`source-${citation.source.name}`}
182+
id={`source-${idx}`}
183183
class="no-toggle outline-none flex dark:text-gray-300 p-1 bg-gray-50 hover:bg-gray-100 dark:bg-gray-900 dark:hover:bg-gray-850 transition rounded-xl max-w-96"
184184
on:click={() => {
185185
showCitationModal = true;

src/lib/components/chat/Messages/Markdown/Source.svelte

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,24 +2,31 @@
22
export let token;
33
export let onClick: Function = () => {};
44
5-
let id = '';
6-
function extractDataAttribute(input) {
7-
// Use a regular expression to extract the value of the `data` attribute
8-
const match = input.match(/data="([^"]*)"/);
9-
// Check if a match was found and return the first captured group
10-
return match ? match[1] : null;
5+
let attributes: Record<string, string> = {};
6+
7+
function extractAttributes(input: string): Record<string, string> {
8+
const regex = /(\w+)="([^"]*)"/g;
9+
let match;
10+
let attrs: Record<string, string> = {};
11+
12+
// Loop through all matches and populate the attributes object
13+
while ((match = regex.exec(input)) !== null) {
14+
attrs[match[1]] = match[2];
15+
}
16+
17+
return attrs;
1118
}
1219
13-
$: id = extractDataAttribute(token.text);
20+
$: attributes = extractAttributes(token.text);
1421
</script>
1522

1623
<button
1724
class="text-xs font-medium w-fit translate-y-[2px] px-2 py-0.5 dark:bg-white/5 dark:text-white/60 dark:hover:text-white bg-gray-50 text-black/60 hover:text-black transition rounded-lg"
1825
on:click={() => {
19-
onClick(id);
26+
onClick(attributes.data);
2027
}}
2128
>
2229
<span class="line-clamp-1">
23-
{id}
30+
{attributes.title}
2431
</span>
2532
</button>

src/lib/utils/index.ts

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -55,15 +55,12 @@ export const replaceTokens = (content, sourceIds, char, user) => {
5555

5656
// Remove sourceIds from the content and replace them with <source_id>...</source_id>
5757
if (Array.isArray(sourceIds)) {
58-
sourceIds.forEach((sourceId) => {
59-
// Escape special characters in the sourceId
60-
const escapedSourceId = escapeRegExp(sourceId);
61-
58+
sourceIds.forEach((sourceId, idx) => {
6259
// Create a token based on the exact `[sourceId]` string
63-
const sourceToken = `\\[${escapedSourceId}\\]`; // Escape special characters for RegExp
60+
const sourceToken = `\\[${idx}\\]`; // Escape special characters for RegExp
6461
const sourceRegex = new RegExp(sourceToken, 'g'); // Match all occurrences of [sourceId]
6562

66-
content = content.replace(sourceRegex, `<source_id data="${sourceId}" />`);
63+
content = content.replace(sourceRegex, `<source_id data="${idx}" title="${sourceId}" />`);
6764
});
6865
}
6966

0 commit comments

Comments
 (0)