1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
|
import { MAX_LIST_NAME_LENGTH } from "../types/lists";
import { ImportSource, ParsedBookmark, parseImportFile } from "./parsers";
export interface ImportCounts {
successes: number;
failures: number;
alreadyExisted: number;
total: number;
}
export interface StagedBookmark {
type: "link" | "text" | "asset";
url?: string;
title?: string;
content?: string;
note?: string;
tags: string[];
listIds: string[];
sourceAddedAt?: Date;
}
export interface ImportDeps {
createList: (input: {
name: string;
icon: string;
parentId?: string;
}) => Promise<{ id: string }>;
stageImportedBookmarks: (input: {
importSessionId: string;
bookmarks: StagedBookmark[];
}) => Promise<void>;
createImportSession: (input: {
name: string;
rootListId: string;
}) => Promise<{ id: string }>;
finalizeImportStaging: (sessionId: string) => Promise<void>;
}
export interface ImportOptions {
concurrencyLimit?: number;
parsers?: Partial<
Record<ImportSource, (textContent: string) => ParsedBookmark[]>
>;
}
export interface ImportResult {
counts: ImportCounts;
rootListId: string | null;
importSessionId: string | null;
}
export async function importBookmarksFromFile(
{
file,
source,
rootListName,
deps,
onProgress,
}: {
file: { text: () => Promise<string> };
source: ImportSource;
rootListName: string;
deps: ImportDeps;
onProgress?: (done: number, total: number) => void;
},
options: ImportOptions = {},
): Promise<ImportResult> {
const { parsers } = options;
const textContent = await file.text();
const parsedBookmarks = parsers?.[source]
? parsers[source]!(textContent)
: parseImportFile(source, textContent);
if (parsedBookmarks.length === 0) {
return {
counts: { successes: 0, failures: 0, alreadyExisted: 0, total: 0 },
rootListId: null,
importSessionId: null,
};
}
const rootList = await deps.createList({ name: rootListName, icon: "⬆️" });
const session = await deps.createImportSession({
name: `${source.charAt(0).toUpperCase() + source.slice(1)} Import - ${new Date().toLocaleDateString()}`,
rootListId: rootList.id,
});
onProgress?.(0, parsedBookmarks.length);
const PATH_DELIMITER = "$$__$$";
// Build required paths
const allRequiredPaths = new Set<string>();
for (const bookmark of parsedBookmarks) {
for (const path of bookmark.paths) {
if (path && path.length > 0) {
for (let i = 1; i <= path.length; i++) {
const subPath = path.slice(0, i);
const pathKey = subPath.join(PATH_DELIMITER);
allRequiredPaths.add(pathKey);
}
}
}
}
const allRequiredPathsArray = Array.from(allRequiredPaths).sort(
(a, b) => a.split(PATH_DELIMITER).length - b.split(PATH_DELIMITER).length,
);
const pathMap: Record<string, string> = { "": rootList.id };
for (const pathKey of allRequiredPathsArray) {
const parts = pathKey.split(PATH_DELIMITER);
const parentKey = parts.slice(0, -1).join(PATH_DELIMITER);
const parentId = pathMap[parentKey] || rootList.id;
const folderName = parts[parts.length - 1];
const folderList = await deps.createList({
name: folderName.substring(0, MAX_LIST_NAME_LENGTH),
parentId,
icon: "📁",
});
pathMap[pathKey] = folderList.id;
}
// Prepare all bookmarks for staging
const bookmarksToStage: StagedBookmark[] = parsedBookmarks.map((bookmark) => {
// Convert paths to list IDs using pathMap
// If no paths, assign to root list
const listIds =
bookmark.paths.length === 0
? [rootList.id]
: bookmark.paths
.map((path) => {
if (path.length === 0) {
return rootList.id;
}
const pathKey = path.join(PATH_DELIMITER);
return pathMap[pathKey] || rootList.id;
})
.filter((id, index, arr) => arr.indexOf(id) === index); // dedupe
// Determine type and extract content appropriately
let type: "link" | "text" | "asset" = "link";
let url: string | undefined;
let textContent: string | undefined;
if (bookmark.content) {
if (bookmark.content.type === "link") {
type = "link";
url = bookmark.content.url;
} else if (bookmark.content.type === "text") {
type = "text";
textContent = bookmark.content.text;
}
}
return {
type,
url,
title: bookmark.title,
content: textContent,
note: bookmark.notes,
tags: bookmark.tags ?? [],
listIds,
sourceAddedAt: bookmark.addDate
? new Date(bookmark.addDate * 1000)
: undefined,
};
});
// Stage bookmarks in batches of 50
const BATCH_SIZE = 50;
let staged = 0;
for (let i = 0; i < bookmarksToStage.length; i += BATCH_SIZE) {
const batch = bookmarksToStage.slice(i, i + BATCH_SIZE);
await deps.stageImportedBookmarks({
importSessionId: session.id,
bookmarks: batch,
});
staged += batch.length;
onProgress?.(staged, parsedBookmarks.length);
}
// Finalize staging - marks session as "pending" for worker pickup
await deps.finalizeImportStaging(session.id);
return {
counts: {
successes: 0,
failures: 0,
alreadyExisted: 0,
total: parsedBookmarks.length,
},
rootListId: rootList.id,
importSessionId: session.id,
};
}
|