mirror of
https://github.com/ItzCrazyKns/Perplexica.git
synced 2025-06-07 18:38:42 +00:00
feat(message-box): change styles, lint & beautify
This commit is contained in:
@ -68,45 +68,34 @@ const MessageBox = ({
|
|||||||
) {
|
) {
|
||||||
setParsedMessage(
|
setParsedMessage(
|
||||||
processedMessage.replace(
|
processedMessage.replace(
|
||||||
citationRegex, // Use the updated regex
|
citationRegex,
|
||||||
(match, capturedContent) => { // match is the full "[1,2,3]", capturedContent is "1,2,3"
|
(_, capturedContent: string) => {
|
||||||
// Split the captured content by comma, trim whitespace, and filter out non-digits
|
|
||||||
const numbers = capturedContent
|
const numbers = capturedContent
|
||||||
.split(',')
|
.split(',')
|
||||||
.map(numStr => numStr.trim())
|
.map((numStr) => numStr.trim())
|
||||||
.filter(numStr => /^\d+$/.test(numStr)); // Ensure it's only digits
|
|
||||||
|
|
||||||
// If no valid numbers found after split/filter (e.g., "[]" or "[abc]"), return original match
|
const linksHtml = numbers
|
||||||
if (numbers.length === 0) {
|
.map((numStr) => {
|
||||||
return match;
|
const number = parseInt(numStr);
|
||||||
}
|
|
||||||
|
|
||||||
// Generate an HTML link for each valid number found
|
if (isNaN(number) || number <= 0) {
|
||||||
const linksHtml = numbers.map(numStr => {
|
return `[${numStr}]`;
|
||||||
const number = parseInt(numStr, 10); // Convert string to integer for array indexing
|
}
|
||||||
|
|
||||||
// Basic validation: Ensure it's a positive number
|
const source = message.sources?.[number - 1];
|
||||||
if (isNaN(number) || number <= 0) {
|
const url = source?.metadata?.url;
|
||||||
// Return the original number part as text if invalid for lookup
|
|
||||||
return `[${numStr}]`;
|
|
||||||
}
|
|
||||||
|
|
||||||
// Get the corresponding source, adjusting for 0-based index
|
if (url) {
|
||||||
const source = message.sources?.[number - 1];
|
return `<a href="${url}" target="_blank" className="bg-light-secondary dark:bg-dark-secondary px-1 rounded ml-1 no-underline text-xs text-black/70 dark:text-white/70 relative">${numStr}</a>`;
|
||||||
const url = source?.metadata?.url;
|
} else {
|
||||||
|
return `[${numStr}]`;
|
||||||
// If URL exists, create the link
|
}
|
||||||
if (url) {
|
})
|
||||||
return `<a href="${url}" target="_blank" className="bg-light-secondary dark:bg-dark-secondary px-1 rounded ml-1 no-underline text-xs text-black/70 dark:text-white/70 relative">${numStr}</a>`;
|
.join('');
|
||||||
} else {
|
|
||||||
// If no URL found for this number, return the number styled similarly but red
|
|
||||||
return `<span className="bg-light-secondary dark:bg-dark-secondary px-1 rounded ml-1 text-xs relative font-medium text-red-600 dark:text-red-400">${numStr}</span>`;
|
|
||||||
}
|
|
||||||
}).join(''); // Join the generated links (or fallback text) together without separators
|
|
||||||
|
|
||||||
return linksHtml;
|
return linksHtml;
|
||||||
}
|
},
|
||||||
)
|
),
|
||||||
);
|
);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user