Files
tkmind_go/ui/desktop/src/utils/urlUtils.ts
T
Bradley Axen 1c9a7c0b05 feat: V1.0 (#734)
Co-authored-by: Michael Neale <michael.neale@gmail.com>
Co-authored-by: Wendy Tang <wendytang@squareup.com>
Co-authored-by: Jarrod Sibbison <72240382+jsibbison-square@users.noreply.github.com>
Co-authored-by: Alex Hancock <alex.hancock@example.com>
Co-authored-by: Alex Hancock <alexhancock@block.xyz>
Co-authored-by: Lifei Zhou <lifei@squareup.com>
Co-authored-by: Wes <141185334+wesrblock@users.noreply.github.com>
Co-authored-by: Max Novich <maksymstepanenko1990@gmail.com>
Co-authored-by: Zaki Ali <zaki@squareup.com>
Co-authored-by: Salman Mohammed <smohammed@squareup.com>
Co-authored-by: Kalvin C <kalvinnchau@users.noreply.github.com>
Co-authored-by: Alec Thomas <alec@swapoff.org>
Co-authored-by: lily-de <119957291+lily-de@users.noreply.github.com>
Co-authored-by: kalvinnchau <kalvin@block.xyz>
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
Co-authored-by: Rizel Scarlett <rizel@squareup.com>
Co-authored-by: bwrage <bwrage@squareup.com>
Co-authored-by: Kalvin Chau <kalvin@squareup.com>
Co-authored-by: Alice Hau <110418948+ahau-square@users.noreply.github.com>
Co-authored-by: Alistair Gray <ajgray@stripe.com>
Co-authored-by: Nahiyan Khan <nahiyan.khan@gmail.com>
Co-authored-by: Alex Hancock <alexhancock@squareup.com>
Co-authored-by: Nahiyan Khan <nahiyan@squareup.com>
Co-authored-by: marcelle <1852848+laanak08@users.noreply.github.com>
Co-authored-by: Yingjie He <yingjiehe@block.xyz>
Co-authored-by: Yingjie He <yingjiehe@squareup.com>
Co-authored-by: Lily Delalande <ldelalande@block.xyz>
Co-authored-by: Adewale Abati <acekyd01@gmail.com>
Co-authored-by: Ebony Louis <ebony774@gmail.com>
Co-authored-by: Angie Jones <jones.angie@gmail.com>
Co-authored-by: Ebony Louis <55366651+EbonyLouis@users.noreply.github.com>
2025-01-24 13:04:43 -08:00

66 lines
2.3 KiB
TypeScript

// Helper to normalize URLs for comparison
function normalizeUrl(url: string): string {
try {
const parsed = new URL(url.toLowerCase());
// Remove trailing slashes and normalize protocol
return `${parsed.protocol}//${parsed.host}${parsed.pathname.replace(/\/$/, '')}${parsed.search}${parsed.hash}`;
} catch {
// If URL parsing fails, just lowercase it
return url.toLowerCase();
}
}
// Helper to determine if a link should be included in results
function linkIsEligible(url: string): boolean {
try {
const parsed = new URL(url.toLowerCase());
const ineligibleHosts = ['localhost', '127.0.0.1'];
return !ineligibleHosts.some((host) => parsed.hostname.includes(host));
} catch {
return false;
}
}
export function extractUrls(content: string, previousUrls: string[] = []): string[] {
let remainingContent = content;
const extractedUrls: string[] = [];
// First, extract markdown links
const markdownLinkRegex = /\[([^\]]+)\]\((https?:\/\/[^)]+)\)/g;
const markdownMatches = Array.from(content.matchAll(markdownLinkRegex));
// Add markdown URLs to our results and remove them from the content
markdownMatches.forEach((match) => {
extractedUrls.push(match[2]);
// Replace the entire markdown link with whitespace to preserve string indices
remainingContent = remainingContent.replace(match[0], ' '.repeat(match[0].length));
});
// Now look for standalone URLs in the remaining content
const urlRegex = /(https?:\/\/[^\s<>"']+)/g;
const urlMatches = Array.from(remainingContent.matchAll(urlRegex));
const standardUrls = urlMatches.map((match) => match[1]);
extractedUrls.push(...standardUrls);
// Remove duplicates
const uniqueCurrentUrls = [...new Set(extractedUrls)];
// Filter out ineligible URLs
const eligibleUrls = uniqueCurrentUrls.filter(linkIsEligible);
// Normalize all URLs for comparison
const normalizedPreviousUrls = previousUrls.map(normalizeUrl);
const normalizedCurrentUrls = eligibleUrls.map(normalizeUrl);
// Filter out duplicates from previous URLs
const uniqueUrls = eligibleUrls.filter((url, index) => {
const normalized = normalizedCurrentUrls[index];
const isDuplicate = normalizedPreviousUrls.some(
(prevUrl) => normalizeUrl(prevUrl) === normalized
);
return !isDuplicate;
});
return uniqueUrls;
}