mirror of
https://github.com/danny-avila/LibreChat.git
synced 2025-12-30 15:18:50 +01:00
feat: cites text with links
This commit is contained in:
parent
5e57deab5f
commit
a451574760
6 changed files with 31 additions and 25 deletions
|
|
@ -1,27 +1,29 @@
|
|||
/*
|
||||
// example
|
||||
const ex = "Fetch API[^1^], Axios[^3^], or XMLHttpRequest[^2^]. Each of these...";
|
||||
const links = [
|
||||
'https://www.freecodecamp.org/news/here-is-the-most-popular-ways-to-make-an-http-request-in-javascript-954ce8c95aaa/',
|
||||
'https://stackoverflow.com/questions/247483/http-get-request-in-javascript',
|
||||
'https://livecodestream.dev/post/5-ways-to-make-http-requests-in-javascript/'
|
||||
];
|
||||
|
||||
const regex = /\[\^\d+?\^]/g;
|
||||
|
||||
const citations = Array.from(new Set(ex.match(regex)));
|
||||
const linkMap = {};
|
||||
citations.forEach(citation => {
|
||||
const digit = citation.match(/\d+?/g)[0];
|
||||
linkMap[citation] = links[digit - 1];
|
||||
});
|
||||
*/
|
||||
const citationRegex = /\[\^\d+?\^]/g;
|
||||
|
||||
const citeText = (res) => {
|
||||
const citeText = (res, noLinks = false) => {
|
||||
let result = res.text || res;
|
||||
const citations = Array.from(new Set(result.match(citationRegex)));
|
||||
if (citations?.length === 0) return result;
|
||||
|
||||
if (noLinks) {
|
||||
citations.forEach((citation) => {
|
||||
const digit = citation.match(/\d+?/g)[0];
|
||||
result = result.replaceAll(citation, `<sup>[${digit}](#)</sup>`);
|
||||
});
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
let sources = res.details.sourceAttributions;
|
||||
if (!sources) return res.response;
|
||||
if (sources?.length === 0) return result;
|
||||
sources = sources.map((source) => source.seeMoreUrl);
|
||||
|
||||
citations.forEach((citation) => {
|
||||
const digit = citation.match(/\d+?/g)[0];
|
||||
result = result.replaceAll(citation, `<sup>[${digit}](${sources[digit - 1]})</sup>`);
|
||||
});
|
||||
|
||||
return result;
|
||||
};
|
||||
|
||||
module.exports = citeText;
|
||||
module.exports = citeText;
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue