From 5e0f4de1625957c6ce057ae272aa301fc459d31b Mon Sep 17 00:00:00 2001 From: Jorge Barnaby Date: Wed, 16 Apr 2025 11:41:03 -0400 Subject: 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 --- apps/web/lib/i18n/locales/en/translation.json | 1 + apps/web/lib/i18n/locales/es/translation.json | 3 +- apps/web/lib/importBookmarkParser.ts | 40 +++++++++++++++++++++++++++ 3 files changed, 43 insertions(+), 1 deletion(-) (limited to 'apps/web/lib') diff --git a/apps/web/lib/i18n/locales/en/translation.json b/apps/web/lib/i18n/locales/en/translation.json index 3398045e..4e622df5 100644 --- a/apps/web/lib/i18n/locales/en/translation.json +++ b/apps/web/lib/i18n/locales/en/translation.json @@ -142,6 +142,7 @@ "import_bookmarks_from_omnivore_export": "Import Bookmarks from Omnivore export", "import_bookmarks_from_linkwarden_export": "Import Bookmarks from Linkwarden export", "import_bookmarks_from_hoarder_export": "Import Bookmarks from Hoarder export", + "import_bookmarks_from_tab_session_manager_export": "Import Bookmarks from Tab Session Manager", "export_links_and_notes": "Export Links and Notes", "imported_bookmarks": "Imported Bookmarks" }, diff --git a/apps/web/lib/i18n/locales/es/translation.json b/apps/web/lib/i18n/locales/es/translation.json index 9ee40bd2..89af3229 100644 --- a/apps/web/lib/i18n/locales/es/translation.json +++ b/apps/web/lib/i18n/locales/es/translation.json @@ -73,7 +73,8 @@ "import_bookmarks_from_hoarder_export": "Importar marcadores desde exportación de Hoarder", "import_bookmarks_from_html_file": "Importar marcadores desde archivo HTML", "import_bookmarks_from_omnivore_export": "Importar marcadores desde exportación de Omnivore", - "import_bookmarks_from_linkwarden_export": "Importar marcadores desde Linkwarden" + "import_bookmarks_from_linkwarden_export": "Importar marcadores desde Linkwarden", + "import_bookmarks_from_tab_session_manager_export": "Importar marcadores desde Tab Session Manager" }, "api_keys": { "api_keys": "Claves APIs", 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 { + 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[] { -- cgit v1.2.3-70-g09d2