From de9cf0a45227da9d33feabe9c51a71845dad6763 Mon Sep 17 00:00:00 2001 From: MohamedBassem Date: Sun, 13 Oct 2024 01:49:13 +0000 Subject: feature: Allow importing hoarder's own bookmark file. Fixes #527 --- apps/web/lib/exportBookmarks.ts | 60 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 60 insertions(+) create mode 100644 apps/web/lib/exportBookmarks.ts (limited to 'apps/web/lib/exportBookmarks.ts') diff --git a/apps/web/lib/exportBookmarks.ts b/apps/web/lib/exportBookmarks.ts new file mode 100644 index 00000000..dd1913fb --- /dev/null +++ b/apps/web/lib/exportBookmarks.ts @@ -0,0 +1,60 @@ +import { z } from "zod"; + +import { BookmarkTypes, ZBookmark } from "@hoarder/shared/types/bookmarks"; + +export const zExportBookmarkSchema = z.object({ + createdAt: z.number(), + title: z.string().nullable(), + tags: z.array(z.string()), + content: z + .discriminatedUnion("type", [ + z.object({ + type: z.literal(BookmarkTypes.LINK), + url: z.string(), + }), + z.object({ + type: z.literal(BookmarkTypes.TEXT), + text: z.string(), + }), + ]) + .nullable(), + note: z.string().nullable(), +}); + +export const zExportSchema = z.object({ + bookmarks: z.array(zExportBookmarkSchema), +}); + +export function toExportFormat( + bookmark: ZBookmark, +): z.infer { + let content = null; + switch (bookmark.content.type) { + case BookmarkTypes.LINK: { + content = { + type: bookmark.content.type, + url: bookmark.content.url, + }; + break; + } + case BookmarkTypes.TEXT: { + content = { + type: bookmark.content.type, + text: bookmark.content.text, + }; + break; + } + // Exclude asset types for now + } + return { + createdAt: Math.floor(bookmark.createdAt.getTime() / 1000), + title: + bookmark.title ?? + (bookmark.content.type === BookmarkTypes.LINK + ? bookmark.content.title ?? null + : null), + tags: bookmark.tags.map((t) => t.name), + content, + note: bookmark.note ?? null, + }; +} -- cgit v1.2.3-70-g09d2