aboutsummaryrefslogtreecommitdiffstats
path: root/apps/web/lib
diff options
context:
space:
mode:
Diffstat (limited to 'apps/web/lib')
-rw-r--r--apps/web/lib/netscapeBookmarkParser.ts20
1 files changed, 20 insertions, 0 deletions
diff --git a/apps/web/lib/netscapeBookmarkParser.ts b/apps/web/lib/netscapeBookmarkParser.ts
new file mode 100644
index 00000000..ac5f3ec2
--- /dev/null
+++ b/apps/web/lib/netscapeBookmarkParser.ts
@@ -0,0 +1,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));
+}