aboutsummaryrefslogtreecommitdiffstats
path: root/apps/web/lib/netscapeBookmarkParser.ts
diff options
context:
space:
mode:
authorkamtschatka <sschatka@gmail.com>2024-05-25 23:20:17 +0200
committerGitHub <noreply@github.com>2024-05-25 22:20:17 +0100
commit033e8a2d26bb0ecaa8301609960d35d3467a88f4 (patch)
tree8c899a081b1cdd31eb24ab851b7a5c9d1dec858e /apps/web/lib/netscapeBookmarkParser.ts
parentbb431be02850ca37f89cc40ce958226f64fe5ad5 (diff)
downloadkarakeep-033e8a2d26bb0ecaa8301609960d35d3467a88f4.tar.zst
feature: Allow import Netscape HTML format (#163)
* [Feature request] Netscape HTML format import/export #96 added the possibility to add exported bookmarks via the webUI for ease of use * [Feature request] Netscape HTML format import/export #96 updated the documentation * Extract the parser into its own file and reuse the existing bookmark upload logic --------- Co-authored-by: kamtschatka <simon.schatka@gmx.at> Co-authored-by: MohamedBassem <me@mbassem.com>
Diffstat (limited to 'apps/web/lib/netscapeBookmarkParser.ts')
-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));
+}