Skip to content

Commit

Permalink
fix: Adopt pocket's new export format. Fixes #570
Browse files Browse the repository at this point in the history
  • Loading branch information
MohamedBassem committed Nov 3, 2024
1 parent 306567b commit 7042f26
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 22 deletions.
2 changes: 1 addition & 1 deletion apps/web/components/settings/ImportExport.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -213,7 +213,7 @@ export function ImportExportRow() {

<FilePickerButton
loading={false}
accept=".html"
accept=".csv"
multiple={false}
className="flex items-center gap-2"
onFileSelect={(file) =>
Expand Down
39 changes: 18 additions & 21 deletions apps/web/lib/importBookmarkParser.ts
Original file line number Diff line number Diff line change
@@ -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";

Expand Down Expand Up @@ -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(
Expand Down
2 changes: 2 additions & 0 deletions apps/web/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@
"cheerio": "^1.0.0",
"class-variance-authority": "^0.7.0",
"clsx": "^2.1.0",
"csv-parse": "^5.5.6",
"dayjs": "^1.11.10",
"drizzle-orm": "^0.33.0",
"fastest-levenshtein": "^1.0.16",
Expand Down Expand Up @@ -80,6 +81,7 @@
"@hoarder/prettier-config": "workspace:^0.1.0",
"@hoarder/tailwind-config": "workspace:^0.1.0",
"@hoarder/tsconfig": "workspace:^0.1.0",
"@types/csv-parse": "^1.2.5",
"@types/emoji-mart": "^3.0.14",
"@types/react": "^18.2.55",
"@types/react-dom": "^18.2.19",
Expand Down
20 changes: 20 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 7042f26

Please sign in to comment.