A self-hostable bookmark-everything app (links, notes and images) with AI-based automatic tagging and full text search
feat: add Linkwarden importer (#786)
Patrick Leonard27b3f9d17958
diff --git a/apps/web/components/settings/ImportExport.tsx b/apps/web/components/settings/ImportExport.tsxindex 34e069da..e38f629a 100644--- a/apps/web/components/settings/ImportExport.tsx+++ b/apps/web/components/settings/ImportExport.tsx@@ -11,6 +11,7 @@ import { useTranslation } from "@/lib/i18n/client";import {ParsedBookmark,parseHoarderBookmarkFile,+ parseLinkwardenBookmarkFile,parseNetscapeBookmarkFile,parseOmnivoreBookmarkFile,parsePocketBookmarkFile,@@ -121,7 +122,7 @@ export function ImportExportRow() {source,}: {file: File;- source: "html" | "pocket" | "omnivore" | "hoarder";+ source: "html" | "pocket" | "omnivore" | "hoarder" | "linkwarden";}) => {if (source === "html") {return await parseNetscapeBookmarkFile(file);@@ -131,6 +132,8 @@ export function ImportExportRow() {return await parseHoarderBookmarkFile(file);} else if (source === "omnivore") {return await parseOmnivoreBookmarkFile(file);+ } else if (source === "linkwarden") {+ return await parseLinkwardenBookmarkFile(file);} else {throw new Error("Unknown source");}@@ -230,6 +233,18 @@ export function ImportExportRow() {<Upload /><p>{t("settings.import.import_bookmarks_from_omnivore_export")}</p></FilePickerButton>+ <FilePickerButton+ loading={false}+ accept=".json"+ multiple={false}+ className="flex items-center gap-2"+ onFileSelect={(file) =>+ runUploadBookmarkFile({ file, source: "linkwarden" })+ }+ >+ <Upload />+ <p>{t("settings.import.import_bookmarks_from_linkwarden_export")}</p>+ </FilePickerButton><FilePickerButtonloading={false}accept=".json"diff --git a/apps/web/lib/i18n/locales/en/translation.json b/apps/web/lib/i18n/locales/en/translation.jsonindex 0236b1c3..92f6e956 100644--- a/apps/web/lib/i18n/locales/en/translation.json+++ b/apps/web/lib/i18n/locales/en/translation.json@@ -99,6 +99,7 @@"import_bookmarks_from_html_file": "Import Bookmarks from HTML file","import_bookmarks_from_pocket_export": "Import Bookmarks from Pocket export","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","export_links_and_notes": "Export Links and Notes","imported_bookmarks": "Imported Bookmarks"diff --git a/apps/web/lib/importBookmarkParser.ts b/apps/web/lib/importBookmarkParser.tsindex f3819e79..69b8a78c 100644--- a/apps/web/lib/importBookmarkParser.ts+++ b/apps/web/lib/importBookmarkParser.ts@@ -140,3 +140,39 @@ export async function parseOmnivoreBookmarkFile(};});}++export async function parseLinkwardenBookmarkFile(+ file: File,+): Promise<ParsedBookmark[]> {+ const textContent = await file.text();+ const zLinkwardenExportSchema = z.object({+ collections: z.array(+ z.object({+ links: z.array(+ z.object({+ name: z.string(),+ url: z.string(),+ tags: z.array(z.object({ name: z.string() })),+ createdAt: z.coerce.date(),+ }),+ ),+ }),+ ),+ });++ const parsed = zLinkwardenExportSchema.safeParse(JSON.parse(textContent));+ if (!parsed.success) {+ throw new Error(+ `The uploaded JSON file contains an invalid Linkwarden bookmark file: ${parsed.error.toString()}`,+ );+ }++ return parsed.data.collections.flatMap((collection) => {+ return collection.links.map((bookmark) => ({+ title: bookmark.name ?? "",+ content: { type: BookmarkTypes.LINK as const, url: bookmark.url },+ tags: bookmark.tags.map((tag) => tag.name),+ addDate: bookmark.createdAt.getTime() / 1000,+ }));+ });+}