aboutsummaryrefslogtreecommitdiffstats
path: root/apps/web/lib/netscapeBookmarkParser.ts
blob: ac5f3ec2de217d5b9e96976e10b3ac10b5d228a5 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
function extractUrls(html: string): string[] {
  const regex = /<a\s+(?:[^>]*?\s+)?href="(http[^"]*)"/gi;
  let match;
  const urls = [];

  while ((match = regex.exec(html)) !== null) {
    urls.push(match[1]);
  }

  return urls;
}

export async function parseNetscapeBookmarkFile(file: File) {
  const textContent = await file.text();
  if (!textContent.startsWith("<!DOCTYPE NETSCAPE-Bookmark-file-1>")) {
    throw Error("The uploaded html file does not seem to be a bookmark file");
  }

  return extractUrls(textContent).map((url) => new URL(url));
}