{idx !== chunks.length - 1 && (
)}
))}
);
}
const supportedSources = [
"link://",
"confluence://",
"github://",
"gitlab://",
"gitea://",
"drupalwiki://",
"youtube://",
"obsidian://",
"paperless-ngx://",
"gmail-thread://",
"gmail-attachment://",
"google-calendar://",
"outlook-thread://",
"outlook-attachment://",
];
/**
* Parses the chunk source to get the correct title and/or display text for citations
* which contain valid outbound links that can be clicked by the
* user when viewing a citation. Optionally allows various icons
* to show distinct types of sources.
* @param {{title: string, chunks: {text: string, chunkSource: string}[]}} options
* @returns {{isUrl: boolean, text: string, href: string, icon: string}}
*/
export function parseChunkSource({ title = "", chunks = [] }) {
const nullResponse = {
isUrl: false,
text: null,
href: null,
icon: "file",
};
if (
!chunks.length ||
!supportedSources.some((source) =>
chunks[0].chunkSource?.startsWith(source)
)
)
return nullResponse;
try {
const sourceID = supportedSources.find((source) =>
chunks[0].chunkSource?.startsWith(source)
);
let url, text, icon;
// Try to parse the URL from the chunk source
// If it fails, we'll use the title as the text and the link icon
// but the document will not be linkable
try {
url = new URL(chunks[0].chunkSource.split(sourceID)[1]);
} catch {}
switch (sourceID) {
case "link://":
text = url.host + url.pathname;
icon = "link";
break;
case "youtube://":
text = title;
icon = "youtube";
break;
case "github://":
text = title;
icon = "github";
break;
case "gitlab://":
text = title;
icon = "gitlab";
break;
case "gitea://":
text = title;
icon = "gitea";
break;
case "confluence://":
text = title;
icon = "confluence";
break;
case "drupalwiki://":
text = title;
icon = "drupalwiki";
break;
case "obsidian://":
text = title;
icon = "obsidian";
break;
case "paperless-ngx://":
text = title;
icon = "paperlessNgx";
break;
case "gmail-thread://":
text = title;
icon = "gmailThread";
break;
case "gmail-attachment://":
text = title;
icon = "gmailAttachment";
break;
case "google-calendar://":
text = title;
icon = "googleCalendar";
break;
case "outlook-thread://":
text = title;
icon = "outlookThread";
break;
case "outlook-attachment://":
text = title;
icon = "outlookAttachment";
break;
default:
text = url.host + url.pathname;
icon = "link";
break;
}
return {
isUrl: !!url,
href: url?.toString() ?? "#",
text,
icon,
};
} catch (err) {
console.warn(`Unsupported source identifier ${chunks[0].chunkSource}`, err);
}
return nullResponse;
}