From 7ec5746192ac59b38f7666643bad5e68ef4b46d7 Mon Sep 17 00:00:00 2001 From: Mohamed Bassem Date: Sun, 3 Nov 2024 19:49:04 +0000 Subject: feature: Add support for importing bookmarks from Omnivore. Fixes #602 --- apps/web/lib/importBookmarkParser.ts | 31 +++++++++++++++++++++++++++++++ 1 file changed, 31 insertions(+) (limited to 'apps/web/lib') 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 { + 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, + }; + }); +} -- cgit v1.2.3-70-g09d2