rcgit

/ karakeep

Commit 43ed6989

SHA 43ed698930531631116f0fc22234a9ef68c122e6
Author MohamedBassem <me at mbassem dot com>
Author Date 2024-10-13 12:22 +0000
Committer MohamedBassem <me at mbassem dot com>
Commit Date 2024-10-13 12:22 +0000
Parent(s) cdd0088a7f1d (diff)
Tree 2010f230e2bc

patch snapshot

fix: Add support for importing notes from the hoarder export
File + - Graph
M apps/web/components/dashboard/settings/ImportExport.tsx +13 -7
M apps/web/lib/importBookmarkParser.ts +28 -13
2 file(s) changed, 41 insertions(+), 20 deletions(-)

apps/web/components/dashboard/settings/ImportExport.tsx

diff --git a/apps/web/components/dashboard/settings/ImportExport.tsx b/apps/web/components/dashboard/settings/ImportExport.tsx
index f3ef13ef..1145a42d 100644
--- a/apps/web/components/dashboard/settings/ImportExport.tsx
+++ b/apps/web/components/dashboard/settings/ImportExport.tsx
@@ -64,14 +64,20 @@ export function ImportExportRow() {
       listId: string;
     }) => {
       const bookmark = toImport.bookmark;
-      if (bookmark.url === undefined) {
-        throw new Error("URL is undefined");
+      if (bookmark.content === undefined) {
+        throw new Error("Content is undefined");
       }
-      new URL(bookmark.url);
-      const created = await createBookmark({
-        type: BookmarkTypes.LINK,
-        url: bookmark.url,
-      });
+      const created = await createBookmark(
+        bookmark.content.type === BookmarkTypes.LINK
+          ? {
+              type: BookmarkTypes.LINK,
+              url: bookmark.content.url,
+            }
+          : {
+              type: BookmarkTypes.TEXT,
+              text: bookmark.content.text,
+            },
+      );
 
       await Promise.all([
         // Update title and createdAt if they're set

apps/web/lib/importBookmarkParser.ts

diff --git a/apps/web/lib/importBookmarkParser.ts b/apps/web/lib/importBookmarkParser.ts
index 3134af55..45be3004 100644
--- a/apps/web/lib/importBookmarkParser.ts
+++ b/apps/web/lib/importBookmarkParser.ts
@@ -7,7 +7,9 @@ import { zExportSchema } from "./exportBookmarks";
 
 export interface ParsedBookmark {
   title: string;
-  url?: string;
+  content?:
+    | { type: BookmarkTypes.LINK; url: string }
+    | { type: BookmarkTypes.TEXT; text: string };
   tags: string[];
   addDate?: number;
   notes?: string;
@@ -36,9 +38,10 @@ export async function parseNetscapeBookmarkFile(
       } catch (e) {
         /* empty */
       }
+      const url = $a.attr("href");
       return {
         title: $a.text(),
-        url: $a.attr("href"),
+        content: url ? { type: BookmarkTypes.LINK as const, url } : undefined,
         tags,
         addDate: typeof addDate === "undefined" ? undefined : parseInt(addDate),
       };
@@ -64,9 +67,10 @@ export async function parsePocketBookmarkFile(
       } catch (e) {
         /* empty */
       }
+      const url = $a.attr("href");
       return {
         title: $a.text(),
-        url: $a.attr("href"),
+        content: url ? { type: BookmarkTypes.LINK as const, url } : undefined,
         tags,
         addDate: typeof addDate === "undefined" ? undefined : parseInt(addDate),
       };
@@ -86,14 +90,25 @@ export async function parseHoarderBookmarkFile(
     );
   }
 
-  return parsed.data.bookmarks.map((bookmark) => ({
-    title: bookmark.title ?? "",
-    url:
-      bookmark.content?.type == BookmarkTypes.LINK
-        ? bookmark.content.url
-        : undefined,
-    tags: bookmark.tags,
-    addDate: bookmark.createdAt,
-    notes: bookmark.note ?? undefined,
-  }));
+  return parsed.data.bookmarks.map((bookmark) => {
+    let content = undefined;
+    if (bookmark.content?.type == BookmarkTypes.LINK) {
+      content = {
+        type: BookmarkTypes.LINK as const,
+        url: bookmark.content.url,
+      };
+    } else if (bookmark.content?.type == BookmarkTypes.TEXT) {
+      content = {
+        type: BookmarkTypes.TEXT as const,
+        text: bookmark.content.text,
+      };
+    }
+    return {
+      title: bookmark.title ?? "",
+      content,
+      tags: bookmark.tags,
+      addDate: bookmark.createdAt,
+      notes: bookmark.note ?? undefined,
+    };
+  });
 }