Skip to content

Commit 43ed698

Browse files
committed
fix: Add support for importing notes from the hoarder export
1 parent cdd0088 commit 43ed698

File tree

2 files changed

+41
-20
lines changed

2 files changed

+41
-20
lines changed

apps/web/components/dashboard/settings/ImportExport.tsx

+13-7
Original file line numberDiff line numberDiff line change
@@ -64,14 +64,20 @@ export function ImportExportRow() {
6464
listId: string;
6565
}) => {
6666
const bookmark = toImport.bookmark;
67-
if (bookmark.url === undefined) {
68-
throw new Error("URL is undefined");
67+
if (bookmark.content === undefined) {
68+
throw new Error("Content is undefined");
6969
}
70-
new URL(bookmark.url);
71-
const created = await createBookmark({
72-
type: BookmarkTypes.LINK,
73-
url: bookmark.url,
74-
});
70+
const created = await createBookmark(
71+
bookmark.content.type === BookmarkTypes.LINK
72+
? {
73+
type: BookmarkTypes.LINK,
74+
url: bookmark.content.url,
75+
}
76+
: {
77+
type: BookmarkTypes.TEXT,
78+
text: bookmark.content.text,
79+
},
80+
);
7581

7682
await Promise.all([
7783
// Update title and createdAt if they're set

apps/web/lib/importBookmarkParser.ts

+28-13
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,9 @@ import { zExportSchema } from "./exportBookmarks";
77

88
export interface ParsedBookmark {
99
title: string;
10-
url?: string;
10+
content?:
11+
| { type: BookmarkTypes.LINK; url: string }
12+
| { type: BookmarkTypes.TEXT; text: string };
1113
tags: string[];
1214
addDate?: number;
1315
notes?: string;
@@ -36,9 +38,10 @@ export async function parseNetscapeBookmarkFile(
3638
} catch (e) {
3739
/* empty */
3840
}
41+
const url = $a.attr("href");
3942
return {
4043
title: $a.text(),
41-
url: $a.attr("href"),
44+
content: url ? { type: BookmarkTypes.LINK as const, url } : undefined,
4245
tags,
4346
addDate: typeof addDate === "undefined" ? undefined : parseInt(addDate),
4447
};
@@ -64,9 +67,10 @@ export async function parsePocketBookmarkFile(
6467
} catch (e) {
6568
/* empty */
6669
}
70+
const url = $a.attr("href");
6771
return {
6872
title: $a.text(),
69-
url: $a.attr("href"),
73+
content: url ? { type: BookmarkTypes.LINK as const, url } : undefined,
7074
tags,
7175
addDate: typeof addDate === "undefined" ? undefined : parseInt(addDate),
7276
};
@@ -86,14 +90,25 @@ export async function parseHoarderBookmarkFile(
8690
);
8791
}
8892

89-
return parsed.data.bookmarks.map((bookmark) => ({
90-
title: bookmark.title ?? "",
91-
url:
92-
bookmark.content?.type == BookmarkTypes.LINK
93-
? bookmark.content.url
94-
: undefined,
95-
tags: bookmark.tags,
96-
addDate: bookmark.createdAt,
97-
notes: bookmark.note ?? undefined,
98-
}));
93+
return parsed.data.bookmarks.map((bookmark) => {
94+
let content = undefined;
95+
if (bookmark.content?.type == BookmarkTypes.LINK) {
96+
content = {
97+
type: BookmarkTypes.LINK as const,
98+
url: bookmark.content.url,
99+
};
100+
} else if (bookmark.content?.type == BookmarkTypes.TEXT) {
101+
content = {
102+
type: BookmarkTypes.TEXT as const,
103+
text: bookmark.content.text,
104+
};
105+
}
106+
return {
107+
title: bookmark.title ?? "",
108+
content,
109+
tags: bookmark.tags,
110+
addDate: bookmark.createdAt,
111+
notes: bookmark.note ?? undefined,
112+
};
113+
});
99114
}

0 commit comments

Comments
 (0)