From 7042f26b3d0af33ef86451b68b74683da78f1552 Mon Sep 17 00:00:00 2001 From: Mohamed Bassem Date: Sun, 3 Nov 2024 19:08:00 +0000 Subject: fix: Adopt pocket's new export format. Fixes #570 --- apps/web/lib/importBookmarkParser.ts | 39 +++++++++++++++++------------------- 1 file changed, 18 insertions(+), 21 deletions(-) (limited to 'apps/web/lib/importBookmarkParser.ts') diff --git a/apps/web/lib/importBookmarkParser.ts b/apps/web/lib/importBookmarkParser.ts index 45be3004..3262b170 100644 --- a/apps/web/lib/importBookmarkParser.ts +++ b/apps/web/lib/importBookmarkParser.ts @@ -1,5 +1,6 @@ // Copied from https://gist.github.com/devster31/4e8c6548fd16ffb75c02e6f24e27f9b9 import * as cheerio from "cheerio"; +import { parse } from "csv-parse/sync"; import { BookmarkTypes } from "@hoarder/shared/types/bookmarks"; @@ -54,28 +55,24 @@ export async function parsePocketBookmarkFile( ): Promise { const textContent = await file.text(); - const $ = cheerio.load(textContent); + const records = parse(textContent, { + columns: true, + skip_empty_lines: true, + }) as { + title: string; + url: string; + time_added: string; + tags: string; + }[]; - return $("a") - .map(function (_index, a) { - const $a = $(a); - const addDate = $a.attr("time_added"); - let tags: string[] = []; - const tagsStr = $a.attr("tags"); - try { - tags = tagsStr && tagsStr.length > 0 ? tagsStr.split(",") : []; - } catch (e) { - /* empty */ - } - const url = $a.attr("href"); - return { - title: $a.text(), - content: url ? { type: BookmarkTypes.LINK as const, url } : undefined, - tags, - addDate: typeof addDate === "undefined" ? undefined : parseInt(addDate), - }; - }) - .get(); + return records.map((record) => { + return { + title: record.title, + content: { type: BookmarkTypes.LINK as const, url: record.url }, + tags: record.tags.length > 0 ? record.tags.split("|") : [], + addDate: parseInt(record.time_added), + }; + }); } export async function parseHoarderBookmarkFile( -- cgit v1.2.3-70-g09d2