diff options
Diffstat (limited to 'apps/web/lib/importBookmarkParser.ts')
| -rw-r--r-- | apps/web/lib/importBookmarkParser.ts | 29 |
1 files changed, 29 insertions, 0 deletions
diff --git a/apps/web/lib/importBookmarkParser.ts b/apps/web/lib/importBookmarkParser.ts index 1f80e5f4..3134af55 100644 --- a/apps/web/lib/importBookmarkParser.ts +++ b/apps/web/lib/importBookmarkParser.ts @@ -1,11 +1,16 @@ // Copied from https://gist.github.com/devster31/4e8c6548fd16ffb75c02e6f24e27f9b9 import * as cheerio from "cheerio"; +import { BookmarkTypes } from "@hoarder/shared/types/bookmarks"; + +import { zExportSchema } from "./exportBookmarks"; + export interface ParsedBookmark { title: string; url?: string; tags: string[]; addDate?: number; + notes?: string; } export async function parseNetscapeBookmarkFile( @@ -68,3 +73,27 @@ export async function parsePocketBookmarkFile( }) .get(); } + +export async function parseHoarderBookmarkFile( + file: File, +): Promise<ParsedBookmark[]> { + const textContent = await file.text(); + + const parsed = zExportSchema.safeParse(JSON.parse(textContent)); + if (!parsed.success) { + throw new Error( + `The uploaded JSON file contains an invalid bookmark file: ${parsed.error.toString()}`, + ); + } + + 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, + })); +} |
