Skip to content

Commit

Permalink
fix: Add support for importing notes from the hoarder export
Browse files Browse the repository at this point in the history
  • Loading branch information
MohamedBassem committed Oct 13, 2024
1 parent cdd0088 commit 43ed698
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 20 deletions.
20 changes: 13 additions & 7 deletions apps/web/components/dashboard/settings/ImportExport.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -64,14 +64,20 @@ export function ImportExportRow() {
listId: string;
}) => {
const bookmark = toImport.bookmark;
if (bookmark.url === undefined) {
throw new Error("URL is undefined");
if (bookmark.content === undefined) {
throw new Error("Content is undefined");
}
new URL(bookmark.url);
const created = await createBookmark({
type: BookmarkTypes.LINK,
url: bookmark.url,
});
const created = await createBookmark(
bookmark.content.type === BookmarkTypes.LINK
? {
type: BookmarkTypes.LINK,
url: bookmark.content.url,
}
: {
type: BookmarkTypes.TEXT,
text: bookmark.content.text,
},
);

await Promise.all([
// Update title and createdAt if they're set
Expand Down
41 changes: 28 additions & 13 deletions apps/web/lib/importBookmarkParser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,9 @@ import { zExportSchema } from "./exportBookmarks";

export interface ParsedBookmark {
title: string;
url?: string;
content?:
| { type: BookmarkTypes.LINK; url: string }
| { type: BookmarkTypes.TEXT; text: string };
tags: string[];
addDate?: number;
notes?: string;
Expand Down Expand Up @@ -36,9 +38,10 @@ export async function parseNetscapeBookmarkFile(
} catch (e) {
/* empty */
}
const url = $a.attr("href");
return {
title: $a.text(),
url: $a.attr("href"),
content: url ? { type: BookmarkTypes.LINK as const, url } : undefined,
tags,
addDate: typeof addDate === "undefined" ? undefined : parseInt(addDate),
};
Expand All @@ -64,9 +67,10 @@ export async function parsePocketBookmarkFile(
} catch (e) {
/* empty */
}
const url = $a.attr("href");
return {
title: $a.text(),
url: $a.attr("href"),
content: url ? { type: BookmarkTypes.LINK as const, url } : undefined,
tags,
addDate: typeof addDate === "undefined" ? undefined : parseInt(addDate),
};
Expand All @@ -86,14 +90,25 @@ export async function parseHoarderBookmarkFile(
);
}

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,
}));
return parsed.data.bookmarks.map((bookmark) => {
let content = undefined;
if (bookmark.content?.type == BookmarkTypes.LINK) {
content = {
type: BookmarkTypes.LINK as const,
url: bookmark.content.url,
};
} else if (bookmark.content?.type == BookmarkTypes.TEXT) {
content = {
type: BookmarkTypes.TEXT as const,
text: bookmark.content.text,
};
}
return {
title: bookmark.title ?? "",
content,
tags: bookmark.tags,
addDate: bookmark.createdAt,
notes: bookmark.note ?? undefined,
};
});
}

0 comments on commit 43ed698

Please sign in to comment.