diff options
| author | Jorge Barnaby <jorge.barnaby@gmail.com> | 2025-04-16 11:41:03 -0400 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2025-04-16 16:41:03 +0100 |
| commit | 5e0f4de1625957c6ce057ae272aa301fc459d31b (patch) | |
| tree | 184c642573bc95b356b31b5ad3515edc97bb65f0 /apps/web/lib/importBookmarkParser.ts | |
| parent | 2328dc3444b8bcae9a134bc859858521d4c31f19 (diff) | |
| download | karakeep-5e0f4de1625957c6ce057ae272aa301fc459d31b.tar.zst | |
feat: Add import support for Tab Session Manager (#1246)
* feat: Add import support for Tab Session Manager
* drop unneeded schema fields
---------
Co-authored-by: Mohamed Bassem <me@mbassem.com>
Diffstat (limited to 'apps/web/lib/importBookmarkParser.ts')
| -rw-r--r-- | apps/web/lib/importBookmarkParser.ts | 40 |
1 files changed, 40 insertions, 0 deletions
diff --git a/apps/web/lib/importBookmarkParser.ts b/apps/web/lib/importBookmarkParser.ts index bea92da1..25415975 100644 --- a/apps/web/lib/importBookmarkParser.ts +++ b/apps/web/lib/importBookmarkParser.ts @@ -177,6 +177,46 @@ export async function parseLinkwardenBookmarkFile( }); } +export async function parseTabSessionManagerStateFile( + file: File, +): Promise<ParsedBookmark[]> { + const textContent = await file.text(); + + const zTab = z.object({ + url: z.string(), + title: z.string(), + lastAccessed: z.number(), + }); + + const zSession = z.object({ + windows: z.record(z.string(), z.record(z.string(), zTab)), + date: z.number(), + }); + + const zTabSessionManagerSchema = z.array(zSession); + + const parsed = zTabSessionManagerSchema.safeParse(JSON.parse(textContent)); + if (!parsed.success) { + throw new Error( + `The uploaded JSON file contains an invalid Tab Session Manager bookmark file: ${parsed.error.toString()}`, + ); + } + + // Get the object in data that has the most recent `date` + const { windows } = parsed.data.reduce((prev, curr) => + prev.date > curr.date ? prev : curr, + ); + + return Object.values(windows).flatMap((window) => + Object.values(window).map((tab) => ({ + title: tab.title, + content: { type: BookmarkTypes.LINK as const, url: tab.url }, + tags: [], + addDate: tab.lastAccessed, + })), + ); +} + export function deduplicateBookmarks( bookmarks: ParsedBookmark[], ): ParsedBookmark[] { |
