aboutsummaryrefslogtreecommitdiffstats
path: root/apps/web/lib/importBookmarkParser.ts
diff options
context:
space:
mode:
authorMohamed Bassem <me@mbassem.com>2024-11-03 19:08:00 +0000
committerMohamed Bassem <me@mbassem.com>2024-11-03 19:09:56 +0000
commit7042f26b3d0af33ef86451b68b74683da78f1552 (patch)
treeaaf90adb5467a1c7b72c0e6cc7b9a35f3b5c9ff2 /apps/web/lib/importBookmarkParser.ts
parent306567b95478d5847eaea052a86e8797a3e03f3f (diff)
downloadkarakeep-7042f26b3d0af33ef86451b68b74683da78f1552.tar.zst
fix: Adopt pocket's new export format. Fixes #570
Diffstat (limited to 'apps/web/lib/importBookmarkParser.ts')
-rw-r--r--apps/web/lib/importBookmarkParser.ts39
1 files changed, 18 insertions, 21 deletions
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<ParsedBookmark[]> {
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(