feat: add smart URL parser for bulk import

This commit is contained in:
SnapOtter
2026-05-11 21:18:45 +08:00
parent 488ed95686
commit a2be47bd68
2 changed files with 115 additions and 0 deletions
+43
View File
@@ -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)];
}