mirror of
https://github.com/snapotter-hq/SnapOtter.git
synced 2026-08-03 07:46:42 +02:00
feat: add smart URL parser for bulk import
This commit is contained in:
@@ -0,0 +1,43 @@
|
||||
function isValidHttpUrl(str: string): boolean {
|
||||
try {
|
||||
const url = new URL(str);
|
||||
return url.protocol === "http:" || url.protocol === "https:";
|
||||
} catch {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
export function extractUrls(input: string): string[] {
|
||||
const urls: string[] = [];
|
||||
|
||||
for (const rawLine of input.split("\n")) {
|
||||
let line = rawLine.trim();
|
||||
if (!line) continue;
|
||||
|
||||
// Strip numbered list prefixes: "1. ", "2) ", "3 "
|
||||
line = line.replace(/^\d+[.)]?\s+/, "");
|
||||
// Strip bullet prefixes: "- ", "* ", "+ "
|
||||
line = line.replace(/^[-*+]\s+/, "");
|
||||
|
||||
// Extract from markdown links: [text](url)
|
||||
const mdMatch = line.match(/\[.*?]\((https?:\/\/[^)]+)\)/);
|
||||
if (mdMatch) {
|
||||
urls.push(mdMatch[1]);
|
||||
continue;
|
||||
}
|
||||
|
||||
// Extract from HTML img tags: <img src="url">
|
||||
const imgMatch = line.match(/<img[^>]+src=["'](https?:\/\/[^"']+)["']/i);
|
||||
if (imgMatch) {
|
||||
urls.push(imgMatch[1]);
|
||||
continue;
|
||||
}
|
||||
|
||||
line = line.trim();
|
||||
if (isValidHttpUrl(line)) {
|
||||
urls.push(line);
|
||||
}
|
||||
}
|
||||
|
||||
return [...new Set(urls)];
|
||||
}
|
||||
Reference in New Issue
Block a user