diff options
| author | Mohamed Bassem <me@mbassem.com> | 2024-11-03 19:49:04 +0000 |
|---|---|---|
| committer | Mohamed Bassem <me@mbassem.com> | 2024-11-03 19:49:04 +0000 |
| commit | 7ec5746192ac59b38f7666643bad5e68ef4b46d7 (patch) | |
| tree | 3ff09565bb688c1b389af6dadbcc778560763de5 /apps/web/lib/importBookmarkParser.ts | |
| parent | 7042f26b3d0af33ef86451b68b74683da78f1552 (diff) | |
| download | karakeep-7ec5746192ac59b38f7666643bad5e68ef4b46d7.tar.zst | |
feature: Add support for importing bookmarks from Omnivore. Fixes #602
Diffstat (limited to '')
| -rw-r--r-- | apps/web/lib/importBookmarkParser.ts | 31 |
1 files changed, 31 insertions, 0 deletions
diff --git a/apps/web/lib/importBookmarkParser.ts b/apps/web/lib/importBookmarkParser.ts index 3262b170..f3819e79 100644 --- a/apps/web/lib/importBookmarkParser.ts +++ b/apps/web/lib/importBookmarkParser.ts @@ -1,6 +1,7 @@ // Copied from https://gist.github.com/devster31/4e8c6548fd16ffb75c02e6f24e27f9b9 import * as cheerio from "cheerio"; import { parse } from "csv-parse/sync"; +import { z } from "zod"; import { BookmarkTypes } from "@hoarder/shared/types/bookmarks"; @@ -109,3 +110,33 @@ export async function parseHoarderBookmarkFile( }; }); } + +export async function parseOmnivoreBookmarkFile( + file: File, +): Promise<ParsedBookmark[]> { + const textContent = await file.text(); + const zOmnivoreExportSchema = z.array( + z.object({ + title: z.string(), + url: z.string(), + labels: z.array(z.string()), + savedAt: z.coerce.date(), + }), + ); + + const parsed = zOmnivoreExportSchema.safeParse(JSON.parse(textContent)); + if (!parsed.success) { + throw new Error( + `The uploaded JSON file contains an invalid omnivore bookmark file: ${parsed.error.toString()}`, + ); + } + + return parsed.data.map((bookmark) => { + return { + title: bookmark.title ?? "", + content: { type: BookmarkTypes.LINK as const, url: bookmark.url }, + tags: bookmark.labels, + addDate: bookmark.savedAt.getTime() / 1000, + }; + }); +} |
