1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
|
// Copied from https://gist.github.com/devster31/4e8c6548fd16ffb75c02e6f24e27f9b9
import * as cheerio from "cheerio";
import { BookmarkTypes } from "@hoarder/shared/types/bookmarks";
import { zExportSchema } from "./exportBookmarks";
export interface ParsedBookmark {
title: string;
url?: string;
tags: string[];
addDate?: number;
notes?: string;
}
export async function parseNetscapeBookmarkFile(
file: File,
): Promise<ParsedBookmark[]> {
const textContent = await file.text();
if (!textContent.startsWith("<!DOCTYPE NETSCAPE-Bookmark-file-1>")) {
throw Error("The uploaded html file does not seem to be a bookmark file");
}
const $ = cheerio.load(textContent);
return $("a")
.map(function (_index, a) {
const $a = $(a);
const addDate = $a.attr("add_date");
let tags: string[] = [];
const tagsStr = $a.attr("tags");
try {
tags = tagsStr && tagsStr.length > 0 ? tagsStr.split(",") : [];
} catch (e) {
/* empty */
}
return {
title: $a.text(),
url: $a.attr("href"),
tags,
addDate: typeof addDate === "undefined" ? undefined : parseInt(addDate),
};
})
.get();
}
export async function parsePocketBookmarkFile(
file: File,
): Promise<ParsedBookmark[]> {
const textContent = await file.text();
const $ = cheerio.load(textContent);
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 */
}
return {
title: $a.text(),
url: $a.attr("href"),
tags,
addDate: typeof addDate === "undefined" ? undefined : parseInt(addDate),
};
})
.get();
}
export async function parseHoarderBookmarkFile(
file: File,
): Promise<ParsedBookmark[]> {
const textContent = await file.text();
const parsed = zExportSchema.safeParse(JSON.parse(textContent));
if (!parsed.success) {
throw new Error(
`The uploaded JSON file contains an invalid bookmark file: ${parsed.error.toString()}`,
);
}
return parsed.data.bookmarks.map((bookmark) => ({
title: bookmark.title ?? "",
url:
bookmark.content?.type == BookmarkTypes.LINK
? bookmark.content.url
: undefined,
tags: bookmark.tags,
addDate: bookmark.createdAt,
notes: bookmark.note ?? undefined,
}));
}
|