Skip to content

A self-hostable bookmark-everything app (links, notes and images) with AI-based automatic tagging and full text search

fix(import): parse linkwarden collections. fixes #2586

Mohamed Bassem722fd8c0cf32
diff --git a/packages/shared/import-export/fixtures/linkwarden-export.json b/packages/shared/import-export/fixtures/linkwarden-export.json
new file mode 100644
index 00000000..ba19ab75
--- /dev/null
+++ b/packages/shared/import-export/fixtures/linkwarden-export.json
@@ -0,0 +1,82 @@
+{
+ "collections": [
+ {
+ "id": 1,
+ "name": "Tech",
+ "parentId": null,
+ "links": [
+ {
+ "name": "GitHub",
+ "url": "https://github.com",
+ "tags": [{ "name": "dev" }],
+ "createdAt": "2025-01-01T00:00:00.000Z"
+ }
+ ]
+ },
+ {
+ "id": 2,
+ "name": "Frontend",
+ "parentId": 1,
+ "links": [
+ {
+ "name": "React",
+ "url": "https://react.dev",
+ "tags": [{ "name": "js" }, { "name": "ui" }],
+ "createdAt": "2025-02-15T12:00:00.000Z"
+ }
+ ]
+ },
+ {
+ "id": 3,
+ "name": "Frameworks",
+ "parentId": 2,
+ "links": [
+ {
+ "name": "Next.js",
+ "url": "https://nextjs.org",
+ "tags": [],
+ "createdAt": "2025-03-10T08:30:00.000Z"
+ }
+ ]
+ },
+ {
+ "id": 4,
+ "name": "News",
+ "parentId": null,
+ "links": [
+ {
+ "name": "Hacker News",
+ "url": "https://news.ycombinator.com",
+ "tags": [{ "name": "news" }],
+ "createdAt": "2025-01-20T00:00:00.000Z"
+ }
+ ]
+ },
+ {
+ "id": 5,
+ "name": "Work",
+ "parentId": null,
+ "links": [
+ {
+ "name": "Example",
+ "url": "https://example.com",
+ "tags": [{ "name": "work" }],
+ "createdAt": "2025-01-01T00:00:00.000Z"
+ }
+ ]
+ },
+ {
+ "id": 6,
+ "name": "Personal",
+ "parentId": null,
+ "links": [
+ {
+ "name": "Example",
+ "url": "https://example.com",
+ "tags": [{ "name": "personal" }],
+ "createdAt": "2025-02-01T00:00:00.000Z"
+ }
+ ]
+ }
+ ]
+}
diff --git a/packages/shared/import-export/parsers.test.ts b/packages/shared/import-export/parsers.test.ts
index a35d7bb8..199abd54 100644
--- a/packages/shared/import-export/parsers.test.ts
+++ b/packages/shared/import-export/parsers.test.ts
@@ -1,3 +1,5 @@
+import fs from "node:fs";
+import path from "node:path";
import { describe, expect, it } from "vitest";
import { parseImportFile } from "./parsers";
@@ -324,6 +326,69 @@ describe("parseNetscapeBookmarkFile", () => {
});
});
+describe("parseLinkwardenBookmarkFile", () => {
+ const fixture = fs.readFileSync(
+ path.join(__dirname, "fixtures/linkwarden-export.json"),
+ "utf-8",
+ );
+
+ it("parses collections with nested hierarchy into paths", () => {
+ const result = parseImportFile("linkwarden", fixture);
+
+ // 6 links total, but example.com appears in two collections so deduped to 5
+ expect(result.bookmarks).toHaveLength(5);
+
+ // Root-level collection
+ expect(result.bookmarks[0]).toMatchObject({
+ title: "GitHub",
+ content: { type: "link", url: "https://github.com" },
+ tags: ["dev"],
+ addDate: new Date("2025-01-01T00:00:00.000Z").getTime() / 1000,
+ paths: [["Tech"]],
+ });
+
+ // Nested one level deep
+ expect(result.bookmarks[1]).toMatchObject({
+ title: "React",
+ content: { type: "link", url: "https://react.dev" },
+ tags: ["js", "ui"],
+ paths: [["Tech", "Frontend"]],
+ });
+
+ // Nested two levels deep
+ expect(result.bookmarks[2]).toMatchObject({
+ title: "Next.js",
+ content: { type: "link", url: "https://nextjs.org" },
+ tags: [],
+ paths: [["Tech", "Frontend", "Frameworks"]],
+ });
+
+ // Another root-level collection
+ expect(result.bookmarks[3]).toMatchObject({
+ title: "Hacker News",
+ content: { type: "link", url: "https://news.ycombinator.com" },
+ tags: ["news"],
+ paths: [["News"]],
+ });
+ });
+
+ it("deduplicates bookmarks across collections and merges metadata", () => {
+ const result = parseImportFile("linkwarden", fixture);
+
+ // example.com appears in both "Work" and "Personal" collections
+ const example = result.bookmarks.find(
+ (b) =>
+ b.content?.type === "link" && b.content.url === "https://example.com",
+ )!;
+ expect(example.tags).toEqual(["work", "personal"]);
+ expect(example.paths).toEqual([["Work"], ["Personal"]]);
+ // Should keep the earlier date
+ expect(example.addDate).toEqual(
+ new Date("2025-01-01T00:00:00.000Z").getTime() / 1000,
+ );
+ });
+});
+
describe("parseKarakeepBookmarkFile", () => {
it("keeps distinct identities for duplicate sibling list names", () => {
const json = JSON.stringify({
diff --git a/packages/shared/import-export/parsers.ts b/packages/shared/import-export/parsers.ts
index b46d0e65..6ea70cf2 100644
--- a/packages/shared/import-export/parsers.ts
+++ b/packages/shared/import-export/parsers.ts
@@ -269,6 +269,9 @@ function parseLinkwardenBookmarkFile(textContent: string): ParsedBookmark[] {
const zLinkwardenExportSchema = z.object({
collections: z.array(
z.object({
+ id: z.number(),
+ name: z.string(),
+ parentId: z.number().nullable(),
links: z.array(
z.object({
name: z.string(),
@@ -288,13 +291,32 @@ function parseLinkwardenBookmarkFile(textContent: string): ParsedBookmark[] {
);
}
+ // Build a map of collection id -> collection for path resolution
+ const collectionsById = new Map(
+ parsed.data.collections.map((c) => [c.id, c]),
+ );
+
+ // Resolve the full path for a collection by walking up the parent chain
+ function getCollectionPath(collectionId: number): string[] {
+ const path: string[] = [];
+ let currentId: number | null = collectionId;
+ while (currentId !== null) {
+ const collection = collectionsById.get(currentId);
+ if (!collection) break;
+ path.unshift(collection.name);
+ currentId = collection.parentId;
+ }
+ return path;
+ }
+
return parsed.data.collections.flatMap((collection) => {
+ const collectionPath = getCollectionPath(collection.id);
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,
- paths: [], // TODO
+ paths: [collectionPath],
}));
});
}