From 033e8a2d26bb0ecaa8301609960d35d3467a88f4 Mon Sep 17 00:00:00 2001 From: kamtschatka Date: Sat, 25 May 2024 23:20:17 +0200 Subject: 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 Co-authored-by: MohamedBassem --- apps/web/lib/netscapeBookmarkParser.ts | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) create mode 100644 apps/web/lib/netscapeBookmarkParser.ts (limited to 'apps/web/lib') 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 = /]*?\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("")) { + throw Error("The uploaded html file does not seem to be a bookmark file"); + } + + return extractUrls(textContent).map((url) => new URL(url)); +} -- cgit v1.2.3-70-g09d2