AI-native knowledge base & Evernote/Obsidian alternative with instant sync & native MCP. Free on Cloudflare or self-hosted via Docker.
HTML ai-agentcloudflarecloudflare-d1cloudflare-r2cloudflare-workersdockerelectronevernoteevernote-alternativeknowledge-basemcpnote-takingobsidian-alternativeoffline-firstpersonal-knowledge-basepwaself-hostedsqlitetiptapweb-clipper
feat: import notes from Evernote, Obsidian and Joplin (#657)
sivdeadf562b2a53766
diff --git a/.gitattributes b/.gitattributesindex 3318ba07..6625e744 100644--- a/.gitattributes+++ b/.gitattributes@@ -2,3 +2,6 @@# GitHub Linguist overrides: exclude built bundles from language statisticsapps/ios/**/EditorBundle/** linguist-vendored++# Preserve the original PDF fixture bytes.+tests/fixtures/note-import/real-apps/assets/*.pdf binarydiff --git a/apps/api/src/backup-consistency.test.mjs b/apps/api/src/backup-consistency.test.mjsindex ab3ce5ef..9102dafb 100644--- a/apps/api/src/backup-consistency.test.mjs+++ b/apps/api/src/backup-consistency.test.mjs@@ -14,11 +14,11 @@ const cleanups = [];afterEach(() => cleanups.splice(0).forEach((cleanup) => cleanup()));const auth = { kind: "user", actorType: "user", actorId: "backup-test-user", username: "owner", displayName: "Owner", scopes: [], workspaceId: "ws_default", role: "owner" };-const installation = async () => {+const installation = async ({ includeExportIndex = true } = {}) => {const sqlite = new Database(":memory:");const migrations = globSync("migrations/*.sql").sort();// Start with the existing schema and data, then upgrade it in place.- for (const file of migrations.filter((file) => !file.endsWith("0080_backup_revision.sql"))) sqlite.exec(readFileSync(file, "utf8"));+ for (const file of migrations.filter((file) => !file.endsWith("0080_backup_revision.sql") && (includeExportIndex || !file.endsWith("0085_export_cursor_index.sql")))) sqlite.exec(readFileSync(file, "utf8"));const directory = mkdtempSync(join(tmpdir(), "edgeever-backup-protocol-"));cleanups.push(() => { sqlite.close(); rmSync(directory, { recursive: true, force: true }); });const storage = createSelfHostedStorageAdapter(sqlite, directory);@@ -40,6 +40,90 @@ const installation = async () => {const pageUrl = (state, kind = "memos", after = "") => `/api/v1/backups/data?${new URLSearchParams({ kind, identity: state.identity, revision: state.backupRevision, after, limit: "1" })}`;describe("consistent backup reads across a real schema upgrade", () => {+ test("upgrades existing data to indexed, stable browser-export continuation pages", async () => {+ const db = await installation({ includeExportIndex: false });+ for (let index = 0; index < 60; index++) await createMemoRecord(db.storage.db, "ws_default", {+ notebookId: db.inbox, title: `Cursor ${index}`, contentMarkdown: `Body ${index}`, tags: ["budget"],+ createdAt: index % 2 ? "2020-01-02T00:00:00.000Z" : "2020-01-01T00:00:00.000Z",+ }, auth, "owner");+ const snapshot = () => db.sqlite.query("SELECT m.*, c.content_markdown, c.content_json, c.content_hash, c.revision FROM memos m JOIN memo_contents c ON c.memo_id=m.id ORDER BY m.id").all();+ const before = snapshot();+ db.sqlite.exec(readFileSync("migrations/0085_export_cursor_index.sql", "utf8"));+ expect(snapshot()).toEqual(before);+ const expected = db.sqlite.query("SELECT id FROM memos WHERE workspace_id='ws_default' AND is_deleted=0 ORDER BY created_at,id").all().map(row => row.id);+ for (const endpoint of ["backups/json", "exports/markdown"]) {+ const queries = [], prepare = db.storage.db.prepare.bind(db.storage.db);+ db.storage.db.prepare = sql => {+ const statement = prepare(sql), bind = statement.bind.bind(statement);+ statement.bind = (...values) => {+ if (/SELECT m\.id, m\.notebook_id/.test(sql)) queries.push({ sql, values });+ return bind(...values);+ };+ return statement;+ };+ let cursor, offset = 0;+ const ids = [];+ do {+ const search = new URLSearchParams({ limit: "7", offset: String(offset) });+ if (cursor) search.set("cursor", cursor);+ const response = await db.request(`/api/v1/${endpoint}?${search}`);+ expect(response.status).toBe(200);+ const page = await response.json();+ expect(page.totalCount).toBe(cursor ? undefined : expected.length);+ ids.push(...page.memos.map(memo => memo.id));+ expect(page.nextOffset).toBe(page.nextCursor === null ? null : ids.length);+ expect(page.nextCursor === null || typeof page.nextCursor === "string").toBe(true);+ cursor = page.nextCursor; offset = page.nextOffset;+ } while (cursor);+ expect(ids).toEqual(expected);+ expect(new Set(ids).size).toBe(expected.length);+ for (const { sql, values } of queries.slice(1)) {+ expect(sql).not.toContain("OFFSET");+ const plan = db.sqlite.query(`EXPLAIN QUERY PLAN ${sql}`).all(...values).map(row => row.detail).join("\n");+ expect(plan).toContain("idx_memos_workspace_created_export");+ expect(plan).not.toContain("TEMP B-TREE");+ }+ // Old clients still receive their historical fields and creation-time order.+ const legacy = await (await db.request(`/api/v1/${endpoint}?limit=7&offset=7`)).json();+ expect(legacy.totalCount).toBe(expected.length);+ expect(legacy.memos.map(memo => memo.id)).toEqual(expected.slice(7,14));+ const selectedIds = expected.filter((_, index) => index % 3 === 1).slice(0, 12);+ const selected = [];+ cursor = undefined;+ do {+ const search = new URLSearchParams({ limit: "3", ids: selectedIds.join(",") });+ if (cursor) search.set("cursor", cursor);+ const response = await db.request(`/api/v1/${endpoint}?${search}`);+ const page = await response.json();+ expect(response.status).toBe(200);+ selected.push(...page.memos.map(memo => memo.id));+ expect(page.nextOffset).toBe(page.nextCursor === null ? null : selected.length);+ cursor = page.nextCursor;+ } while (cursor);+ expect(selected).toEqual(endpoint === "exports/markdown" ? selectedIds : expected);+ db.storage.db.prepare = prepare;+ }+ });++ test("validates only the new export cursor and supports explicitly requested continuation totals", async () => {+ const db = await installation();+ const total = db.sqlite.query("SELECT COUNT(*) AS n FROM memos WHERE workspace_id='ws_default' AND is_deleted=0").get().n;+ for (const endpoint of ["backups/json", "exports/markdown"]) {+ for (const cursor of ["", "not-json", "null", JSON.stringify({ id: "memo", createdAt: "date", offset: -1 })]) {+ const response = await db.request(`/api/v1/${endpoint}?${new URLSearchParams({ cursor })}`);+ expect(response.status).toBe(400);+ expect(await response.json()).toMatchObject({ error: { code: "invalid_export_cursor" } });+ }+ const first = await (await db.request(`/api/v1/${endpoint}?limit=1`)).json();+ expect(first.totalCount).toBe(total);+ expect(typeof first.nextCursor).toBe("string");+ const next = await (await db.request(`/api/v1/${endpoint}?${new URLSearchParams({ limit: "1", cursor: first.nextCursor, includeTotal: "1" })}`)).json();+ expect(next.totalCount).toBe(first.totalCount);+ expect(next.memos[0].id).not.toBe(first.memos[0].id);+ expect(next.nextOffset).toBe(total > 2 ? 2 : null);+ }+ });+test("keeps existing data and old export callers usable, with a separate revision", async () => {const db = await installation();expect(db.revision()).toBe("0");diff --git a/apps/api/src/backup-routes.test.mjs b/apps/api/src/backup-routes.test.mjsindex 9eefd991..0bf854de 100644--- a/apps/api/src/backup-routes.test.mjs+++ b/apps/api/src/backup-routes.test.mjs@@ -201,6 +201,7 @@ describe("backup route contracts", () => {resources: [],totalCount: 0,nextOffset: null,+ nextCursor: null,});});diff --git a/apps/api/src/backup-routes.ts b/apps/api/src/backup-routes.tsindex 7aada387..3f4688ad 100644--- a/apps/api/src/backup-routes.ts+++ b/apps/api/src/backup-routes.ts@@ -12,6 +12,7 @@ import {type TiptapDoc,} from "@edgeever/shared";import { zValidator } from "@hono/zod-validator";+import { z } from "zod";import type { Hono } from "hono";import type { AppEnv } from "./api-context";import { AppError } from "./app-error";@@ -183,19 +184,49 @@ const readBackupRelations = async (database: DatabaseAdapter, memoIds: string[])return { resources: resources.results.map(mapResource), revisions: revisions.results.map(mapJsonBackupRevision) };};+const exportCursorSchema = z.object({+ id: z.string().min(1),+ createdAt: z.string().min(1),+ offset: z.number().int().nonnegative(),+});+type ExportCursor = z.infer<typeof exportCursorSchema>;++const parseExportCursor = (value: string | undefined): ExportCursor | undefined | null => {+ if (value === undefined) return undefined;+ try {+ const parsed = exportCursorSchema.safeParse(JSON.parse(value));+ return parsed.success ? parsed.data : null;+ } catch {+ return null;+ }+};++const exportContinuation = (page: BackupMemoDetailRow[], offset: number, hasMore: boolean) => {+ const nextOffset = hasMore ? offset + page.length : null;+ const last = page.at(-1);+ return {+ nextOffset,+ nextCursor: nextOffset !== null && last+ ? JSON.stringify({ id: last.id, createdAt: last.created_at, offset: nextOffset } satisfies ExportCursor)+ : null,+ };+};+const listBackupPage = async (database: DatabaseAdapter,workspaceId: string,limit: number,offset: number,includeTotal = true,+ cursor?: ExportCursor,) => Promise.all([database.prepare(`${MEMO_EXPORT_SELECT_SQL}WHERE m.workspace_id = ? AND m.is_deleted = 0+ ${cursor ? "AND (m.created_at, m.id) > (?, ?)" : ""}ORDER BY m.created_at ASC, m.id ASC- LIMIT ? OFFSET ?`,- ).bind(workspaceId, includeTotal ? limit : limit + 1, offset).all<BackupMemoDetailRow>(),+ LIMIT ?${cursor ? "" : " OFFSET ?"}`,+ ).bind(workspaceId, ...(cursor ? [cursor.createdAt, cursor.id] : []), includeTotal ? limit : limit + 1, ...(cursor ? [] : [offset])).all<BackupMemoDetailRow>(),includeTotal ? database.prepare(`SELECT COUNT(*) AS count FROM memos WHERE workspace_id = ? AND is_deleted = 0`,).bind(workspaceId).first<{ count: number }>() : Promise.resolve(null),@@ -213,13 +244,14 @@ const listMarkdownExportPage = async (offset: number,memoIds: string[] | null,includeTotal = true,+ cursor?: ExportCursor,) => {if (memoIds && memoIds.length === 0) {return emptyMarkdownExportPage();}if (!memoIds) {- return listBackupPage(database, workspaceId, limit, offset, includeTotal);+ return listBackupPage(database, workspaceId, limit, offset, includeTotal, cursor);}// json_each keeps the D1 bound-parameter count fixed (workspace + JSON + limit/offset).@@ -229,9 +261,10 @@ const listMarkdownExportPage = async (`${MEMO_EXPORT_SELECT_SQL}WHERE m.workspace_id = ? AND m.is_deleted = 0AND m.id IN (SELECT value FROM json_each(?))+ ${cursor ? "AND (m.created_at, m.id) > (?, ?)" : ""}ORDER BY m.created_at ASC, m.id ASC- LIMIT ? OFFSET ?`,- ).bind(workspaceId, memoIdsJson, includeTotal ? limit : limit + 1, offset).all<BackupMemoDetailRow>(),+ LIMIT ?${cursor ? "" : " OFFSET ?"}`,+ ).bind(workspaceId, memoIdsJson, ...(cursor ? [cursor.createdAt, cursor.id] : []), includeTotal ? limit : limit + 1, ...(cursor ? [] : [offset])).all<BackupMemoDetailRow>(),includeTotal ? database.prepare(`SELECT COUNT(*) AS count FROM memosWHERE workspace_id = ? AND is_deleted = 0@@ -322,9 +355,11 @@ export const registerBackupRoutes = (if (denied) return denied;const limit = dependencies.clampNumber(Number(context.req.query("limit") ?? 50), 1, 100);- const offset = dependencies.clampNumber(Number(context.req.query("offset") ?? 0), 0, 1_000_000);+ const cursor = parseExportCursor(context.req.query("cursor"));+ if (cursor === null) return apiError(context, "invalid_export_cursor", "Invalid export cursor.", 400);+ const offset = cursor?.offset ?? dependencies.clampNumber(Number(context.req.query("offset") ?? 0), 0, 1_000_000);const parsedIds = parseMarkdownExportMemoIds(context.req.query("ids"));- const includeTotal = offset === 0 || context.req.query("includeTotal") !== "0";+ const includeTotal = cursor ? context.req.query("includeTotal") === "1" : offset === 0 || context.req.query("includeTotal") !== "0";if (parsedIds.error === "too_many") {return badRequest(context, `Export at most ${MAX_MARKDOWN_EXPORT_MEMO_IDS} notes at a time.`);}@@ -336,6 +371,7 @@ export const registerBackupRoutes = (offset,parsedIds.ids,includeTotal,+ cursor,);const page = memoRows.results.slice(0, limit);const memoIds = page.map((row) => row.id);@@ -358,9 +394,7 @@ export const registerBackupRoutes = (memos: page.map(dependencies.mapMemoDetail),resources,...(includeTotal ? { totalCount } : {}),- nextOffset: (includeTotal ? offset + page.length < totalCount : memoRows.results.length > limit)- ? offset + page.length- : null,+ ...exportContinuation(page, offset, includeTotal ? offset + page.length < totalCount : memoRows.results.length > limit),});});@@ -369,10 +403,12 @@ export const registerBackupRoutes = (if (denied) return denied;const limit = dependencies.clampNumber(Number(context.req.query("limit") ?? 25), 1, 50);- const offset = dependencies.clampNumber(Number(context.req.query("offset") ?? 0), 0, 1_000_000);+ const cursor = parseExportCursor(context.req.query("cursor"));+ if (cursor === null) return apiError(context, "invalid_export_cursor", "Invalid export cursor.", 400);+ const offset = cursor?.offset ?? dependencies.clampNumber(Number(context.req.query("offset") ?? 0), 0, 1_000_000);const workspaceId = getWorkspaceId(context);- const includeTotal = offset === 0 || context.req.query("includeTotal") !== "0";- const [memoRows, totalRow] = await listBackupPage(context.env.storage.db, workspaceId, limit, offset, includeTotal);+ const includeTotal = cursor ? context.req.query("includeTotal") === "1" : offset === 0 || context.req.query("includeTotal") !== "0";+ const [memoRows, totalRow] = await listBackupPage(context.env.storage.db, workspaceId, limit, offset, includeTotal, cursor);const page = memoRows.results.slice(0, limit);const memoIds = page.map((row) => row.id);let resources: Resource[] = [];@@ -406,9 +442,7 @@ export const registerBackupRoutes = (resources,revisions,...(includeTotal ? { totalCount } : {}),- nextOffset: (includeTotal ? offset + page.length < totalCount : memoRows.results.length > limit)- ? offset + page.length- : null,+ ...exportContinuation(page, offset, includeTotal ? offset + page.length < totalCount : memoRows.results.length > limit),});});diff --git a/apps/api/src/import-routes.ts b/apps/api/src/import-routes.tsnew file mode 100644index 00000000..ee77b04d--- /dev/null+++ b/apps/api/src/import-routes.ts@@ -0,0 +1,60 @@+import type { Hono } from "hono";+import { zValidator } from "@hono/zod-validator";+import { z } from "zod";+import type { AppContext, AppEnv } from "./api-context";+import { AppError } from "./app-error";+import { apiError } from "./http-errors";+import { importMemosRecord } from "./memo-service";+import { getActorLabel, getAuditActor, getWorkspaceId, requireScopes } from "./request-auth";++const importSchema = z.object({+ importId: z.uuid(),+ format: z.enum(["evernote", "obsidian", "joplin"]),+ notebookId: z.string().trim().min(1),+ items: z.array(z.unknown()).min(1).max(25),+ dryRun: z.boolean().default(false),+});++const importContext = (context: AppContext) => {+ const auth = context.get("auth");+ return JSON.stringify([auth.workspaceId, auth.actorType, auth.actorId]);+};++/** Opt-in guard for import requests, including ordinary folder/resource writes. */+export const registerImportContextGuard = (app: Hono<AppEnv>) => {+ app.use("/api/v1/*", async (context, next) => {+ const expected = context.req.header("x-edgeever-import-context");+ if (expected !== undefined && expected !== importContext(context)) {+ return apiError(context, "import_identity_changed", "The import account or workspace changed.", 409);+ }+ return next();+ });+};++/** Native import uses the same business service and permissions as MCP. */+export const registerImportRoutes = (app: Hono<AppEnv>) => {+ app.get("/api/v1/imports/capabilities", (context) => {+ const denied = requireScopes(context, "read:memos");+ if (denied) return denied;+ context.header("Cache-Control", "no-store");+ return context.json({ protocolVersion: 1, maxBatchSize: 25, formats: ["evernote", "obsidian", "joplin"], importContext: importContext(context), accountId: context.get("auth").actorId });+ });+ app.post("/api/v1/imports/memos", zValidator("json", importSchema), async (context) => {+ const denied = requireScopes(context, "write:memos");+ if (denied) return denied;+ const input = context.req.valid("json");+ try {+ return context.json(await importMemosRecord(context.env.storage.db, getWorkspaceId(context), {+ source: `third-party-${input.format}-${input.importId}`,+ notebookId: input.notebookId,+ items: input.items,+ dryRun: input.dryRun,+ actor: getAuditActor(context),+ actorLabel: getActorLabel(context),+ }));+ } catch (error) {+ if (error instanceof AppError) return apiError(context, error.code, error.message, error.status);+ throw error;+ }+ });+};diff --git a/apps/api/src/index.ts b/apps/api/src/index.tsindex 2e0bbe3a..13217e1a 100644--- a/apps/api/src/index.ts+++ b/apps/api/src/index.ts@@ -6,6 +6,7 @@ import {normalizeTags,} from "@edgeever/shared";import { Hono } from "hono";+import { registerImportContextGuard, registerImportRoutes } from "./import-routes";import { cors } from "hono/cors";import { resolveContainerImageSource } from "./container-image-source";import apiProbe from "../../../docs/openapi.json";@@ -164,7 +165,7 @@ app.use("/api/*",cors({origin: API_CORS_ORIGINS,- allowHeaders: ["Content-Type", "Authorization"],+ allowHeaders: ["Content-Type", "Authorization", "x-edgeever-import-context"],allowMethods: ["GET", "POST", "PUT", "PATCH", "DELETE", "OPTIONS"],credentials: true,})@@ -325,6 +326,8 @@ app.use("/api/v1/*", async (c, next) => {await next();});+registerImportContextGuard(app);+registerObjectStorageRoutes(app, {isDemoMode: (...args) => isDemoMode(...args),});@@ -348,6 +351,7 @@ registerSyncRoutes(app, {clampNumber: (...args) => clampNumber(...args),mapMemoDetail: (...args) => mapMemoDetail(...args),});+registerImportRoutes(app);registerTagRoutes(app);registerPosterRenderRoutes(app);registerPluginDistributionRoutes(app);diff --git a/apps/api/src/memo-duplicate-service.test.mjs b/apps/api/src/memo-duplicate-service.test.mjsindex c9567217..2917f610 100644--- a/apps/api/src/memo-duplicate-service.test.mjs+++ b/apps/api/src/memo-duplicate-service.test.mjs@@ -80,6 +80,16 @@ const resourceRows = (sqlite, memoId) => sqlite.all(memoId);describe("duplicate a note", () => {+ test("validates its destination without recounting the notebook's notes", async () => {+ const fixture = setup();+ const { memo } = await createSourceNote(fixture);+ const queries = [], prepare = fixture.storage.db.prepare.bind(fixture.storage.db);+ fixture.storage.db.prepare = sql => { queries.push(sql); return prepare(sql); };+ const copy = await duplicateMemoRecord(fixture.context, memo.id, {}, actor, "admin");+ expect(copy.notebookId).toBe(memo.notebookId);+ expect(queries.filter(sql => /COUNT\(m\.id\) AS memo_count/.test(sql))).toHaveLength(0);+ });+test("copies the title, content, tags and files into an independent note", async () => {const fixture = setup();const { sqlite, context, storage } = fixture;diff --git a/apps/api/src/memo-duplicate-service.ts b/apps/api/src/memo-duplicate-service.tsindex 06b5f83c..43722992 100644--- a/apps/api/src/memo-duplicate-service.ts+++ b/apps/api/src/memo-duplicate-service.ts@@ -6,7 +6,7 @@ import { createId, isoNow, parseJsonArray } from "./entity-utils";import { sha256 } from "./hash-utils";import { getMemoDetail, getMemoDetailRow } from "./memo-service";import { upsertMemoSearchDocumentStatement } from "./memo-search-index";-import { getNotebook } from "./notebook-service";+import { notebookExists } from "./notebook-service";import { resolveObjectStorage } from "./object-storage";import { inferImageExtension } from "./resource-service";import { getWorkspaceId } from "./request-auth";@@ -88,7 +88,7 @@ export const duplicateMemoRecord = async (if (!source) throw new AppError("not_found", "Memo not found", 404);const notebookId = input.notebookId ?? source.notebook_id;- if (!(await getNotebook(db, workspaceId, notebookId))) {+ if (!(await notebookExists(db, workspaceId, notebookId))) {throw new AppError("not_found", "Notebook not found", 404);}diff --git a/apps/api/src/memo-service.ts b/apps/api/src/memo-service.tsindex 6c43a584..a85e9cd1 100644--- a/apps/api/src/memo-service.ts+++ b/apps/api/src/memo-service.ts@@ -26,7 +26,7 @@ import { auditStatement } from "./audit";import type { AppContext, AuditActor, AuthContext, Bindings } from "./api-context";import { AppError } from "./app-error";import { clampNumber, createId, isoNow, parseJsonArray } from "./entity-utils";-import { workspaceInboxId } from "./notebook-service";+import { notebookExists, workspaceInboxId } from "./notebook-service";import { sha256 } from "./hash-utils";import { getRequiredString } from "./mcp-json-rpc";import {@@ -43,7 +43,6 @@ import {createMemoRevisionStatement as createMemoRevisionStatementService,shouldSnapshotMemoRevision as shouldSnapshotMemoRevisionService,} from "./memo-revision-service";-import { getNotebook } from "./notebook-service";import { deleteStoredObjects } from "./object-storage";import { getAuditActor, getWorkspaceId } from "./request-auth";import type { DatabaseAdapter, PreparedStatementAdapter } from "./storage-contract";@@ -795,7 +794,7 @@ export const moveMemosRecord = async (actor: AuditActor,actorLabel: string,) => {- if (!(await getNotebook(db, workspaceId, notebookId))) {+ if (!(await notebookExists(db, workspaceId, notebookId))) {throw new AppError("not_found", "Target notebook not found", 404);}return moveMemosToNotebook(db, workspaceId, memoIds, notebookId, actor, actorLabel);@@ -860,7 +859,7 @@ export const mergeMemosRecord = async (throw new AppError("missing_memos", "One or more memos cannot be merged.", 400);}- if (input.notebookId && !(await getNotebook(db, workspaceId, input.notebookId))) {+ if (input.notebookId && !(await notebookExists(db, workspaceId, input.notebookId))) {throw new AppError("not_found", "Target notebook not found", 404);}@@ -960,7 +959,7 @@ export const createMemoRecord = async (// Reject a missing notebook before the batch. The memo insert is a SELECT// from notebooks, so a bad id used to write orphan content and then fail// the read-back as a generic 500.- if (!(await getNotebook(db, workspaceId, input.notebookId))) {+ if (!(await notebookExists(db, workspaceId, input.notebookId))) {throw new AppError("not_found", "Notebook not found", 404);}const tags = normalizeTags(input.tags);@@ -1097,8 +1096,7 @@ export const importMemosRecord = async (if (!Array.isArray(input.items) || input.items.length === 0 || input.items.length > 25) {throw new AppError("invalid_import_items", "items must contain between 1 and 25 memos", 400);}- const notebook = await getNotebook(db, workspaceId, input.notebookId);- if (!notebook) {+ if (!(await notebookExists(db, workspaceId, input.notebookId))) {throw new AppError("not_found", "Import destination notebook not found in the authenticated user's workspace.", 404);}@@ -1130,7 +1128,7 @@ export const importMemosRecord = async (}const memo = await createMemoRecord(db, workspaceId, {- notebookId: notebook.id,+ notebookId: input.notebookId,title: item.title,contentMarkdown: item.contentMarkdown,tags: item.tags,@@ -1148,7 +1146,7 @@ export const importMemosRecord = async (auditStatement(db, input.actor.actorType, input.actor.actorId, "memo.import", "memo", memo.id, {source,externalId,- notebookId: notebook.id,+ notebookId: input.notebookId,}),]);results.push({ index, externalId, status: "created", memo });@@ -1182,7 +1180,7 @@ export const importMemosRecord = async (return {dryRun: input.dryRun,source,- notebookId: notebook.id,+ notebookId: input.notebookId,total: results.length,created: count("created"),skipped: count("skipped"),diff --git a/apps/api/src/notebook-service.ts b/apps/api/src/notebook-service.tsindex 5fce3d29..88a1cdcd 100644--- a/apps/api/src/notebook-service.ts+++ b/apps/api/src/notebook-service.ts@@ -162,6 +162,12 @@ export const getNotebook = async (db: DatabaseAdapter, workspaceId: string, id:return row ? mapNotebook(row) : null;};+/** Existence checks do not need the notebook's potentially large memo aggregate. */+export const notebookExists = async (db: DatabaseAdapter, workspaceId: string, id: string): Promise<boolean> =>+ Boolean(await db.prepare(+ "SELECT id FROM notebooks WHERE id = ? AND workspace_id = ? AND is_deleted = 0",+ ).bind(id, workspaceId).first<{ id: string }>());+export const createNotebookRecord = async (db: DatabaseAdapter,workspaceId: string,@@ -170,7 +176,7 @@ export const createNotebookRecord = async () => {const parentId = input.parentId ?? null;- if (parentId && !(await getNotebook(db, workspaceId, parentId))) {+ if (parentId && !(await notebookExists(db, workspaceId, parentId))) {throw new AppError("not_found", "Parent notebook not found", 404);}@@ -225,9 +231,7 @@ export const updateNotebookRecord = async (}if (nextParentId) {- const parent = await getNotebook(db, workspaceId, nextParentId);-- if (!parent) {+ if (!(await notebookExists(db, workspaceId, nextParentId))) {throw new AppError("not_found", "Parent notebook not found", 404);}diff --git a/apps/extension/src/turndown-plugin-gfm.d.ts b/apps/extension/src/turndown-plugin-gfm.d.tsindex 9828e4b2..b0c8f567 100644--- a/apps/extension/src/turndown-plugin-gfm.d.ts+++ b/apps/extension/src/turndown-plugin-gfm.d.ts@@ -1,4 +1,5 @@declare module "@joplin/turndown-plugin-gfm" {import type TurndownService from "turndown";export const tables: TurndownService.Plugin;+ export const taskListItems: TurndownService.Plugin;}diff --git a/apps/web/package.json b/apps/web/package.jsonindex 2ad119b0..fe6e1ae4 100644--- a/apps/web/package.json+++ b/apps/web/package.json@@ -14,7 +14,9 @@"@edgeever/client": "workspace:*","@floating-ui/react-dom": "^2.1.8","@fontsource-variable/noto-sans-sc": "^5.2.8",+ "@joplin/turndown-plugin-gfm": "1.0.68","@lezer/highlight": "^1.2.3",+ "@mixmark-io/domino": "2.2.0","@radix-ui/react-hover-card": "^1.1.23","@radix-ui/react-separator": "^1.1.15","@radix-ui/react-slot": "^1.3.3",@@ -30,6 +32,7 @@"@uiw/react-codemirror": "^4.25.11","ai": "7.0.137","beautiful-mermaid": "^1.1.3",+ "decode-named-character-reference": "1.3.0","dompurify": "3.4.17","docx-preview": "0.4.1","fabric": "7.4.0",diff --git a/apps/web/src/components/SettingsPane.tsx b/apps/web/src/components/SettingsPane.tsxindex 2b6cbd43..93db7454 100644--- a/apps/web/src/components/SettingsPane.tsx+++ b/apps/web/src/components/SettingsPane.tsx@@ -32,7 +32,8 @@ import { DataExportCard } from "./settings/DataExportCard";import { DesktopLocalDataCard } from "./settings/DesktopLocalDataCard";import { LoginDevicesCard } from "./settings/LoginDevicesCard";import { ShareManagementCard } from "./settings/ShareManagementCard";-import { EvernoteImportGuideCard } from "./settings/EvernoteImportGuideCard";+import { ThirdPartyImportCard } from "./settings/ThirdPartyImportCard";+import type { NoteImportController } from "@/lib/note-import/controller";import { FeedbackLink } from "./settings/FeedbackLink";import { SystemInfoPanel } from "./settings/SystemInfoPanel";import { McpConfigCard } from "./settings/McpConfigCard";@@ -73,6 +74,7 @@ interface SettingsPaneProps {isOwner: boolean;user: AuthUser | null;refreshWorkspaceAfterImport: () => Promise<void>;+ noteImport: NoteImportController;onOpenExecutionCenter: () => void;onOpenMemo: (memoId: string, notebookId: string) => void;}@@ -110,6 +112,7 @@ export const SettingsPane = ({isOwner,user,refreshWorkspaceAfterImport,+ noteImport,onOpenExecutionCenter,onOpenMemo,}: SettingsPaneProps) => {@@ -252,7 +255,7 @@ export const SettingsPane = ({return (<SettingsGroup>{window.edgeeverDesktop?.backup ? <DesktopScheduledBackupCard /> : <DataExportCard refreshWorkspaceAfterImport={refreshWorkspaceAfterImport} />}- <EvernoteImportGuideCard />+ <ThirdPartyImportCard controller={noteImport} onOpenMemo={onOpenMemo} /></SettingsGroup>);case "sharing":diff --git a/apps/web/src/components/WorkspaceApp.tsx b/apps/web/src/components/WorkspaceApp.tsxindex a7bc9e71..2494edeb 100644--- a/apps/web/src/components/WorkspaceApp.tsx+++ b/apps/web/src/components/WorkspaceApp.tsx@@ -1,3 +1,4 @@+import { useNoteImport } from "@/lib/note-import/use-note-import";import { NotebookAppearanceDialog } from "./dialogs/NotebookAppearanceDialog";import type { NotebookUpdateInput } from "@edgeever/shared";import { startPosterClientRenderer } from "@/lib/poster-client-render";@@ -651,6 +652,8 @@ export const WorkspaceApp = ({});}, [invalidateWorkspaceQueries, repository, runQueuedSync, syncSummary.total]);+ const noteImport = useNoteImport(authRequired ? user?.id ?? null : null, user?.displayName || user?.username || "Local", async () => { await refreshWorkspaceFromServer("manual"); });+const refreshedBackupRestores = useRef(new Set<string>());useEffect(() => {const bridge = window.edgeeverDesktop;@@ -3681,6 +3684,7 @@ export const WorkspaceApp = ({demoMode={demoMode}isOwner={authRequired && user?.role === "owner"}user={user}+ noteImport={noteImport}refreshWorkspaceAfterImport={async () => {await refreshWorkspaceFromServer("manual");}}diff --git a/apps/web/src/components/settings/EvernoteImportGuideCard.tsx b/apps/web/src/components/settings/EvernoteImportGuideCard.tsxdeleted file mode 100644index 0a9644ac..00000000--- a/apps/web/src/components/settings/EvernoteImportGuideCard.tsx+++ /dev/null@@ -1,46 +0,0 @@-import { ExternalLink, UploadCloud } from "lucide-react";-import { useTranslation } from "react-i18next";-import { Button } from "@/components/ui/button";-import { Card, CardDescription, CardHeader, CardTitle } from "@/components/ui/card";-import {- SETTINGS_CARD_DESCRIPTION_CLASSNAME,- SETTINGS_CARD_HEADER_CLASSNAME,- SETTINGS_CARD_ICON_CLASSNAME,- SETTINGS_CARD_TITLE_CLASSNAME,-} from "./settings-ui";-import { EVERNOTE_MIGRATION_BLOG_URL } from "@/lib/routes";--export const EvernoteImportGuideCard = () => {- const { t } = useTranslation();-- return (- <Card className="hidden w-full min-w-0 overflow-hidden shadow-none lg:block">- <CardHeader className={SETTINGS_CARD_HEADER_CLASSNAME}>- <div className="flex flex-col gap-3 sm:flex-row sm:items-center sm:justify-between">- <div className="min-w-0 space-y-1">- <CardTitle className={SETTINGS_CARD_TITLE_CLASSNAME}>- <UploadCloud className={SETTINGS_CARD_ICON_CLASSNAME} />- {t("evernoteImport.title")}- </CardTitle>- <CardDescription className={SETTINGS_CARD_DESCRIPTION_CLASSNAME}>- {t("evernoteImport.description")}- </CardDescription>- </div>- <div className="flex shrink-0 items-center gap-2">- <Button size="sm" variant="outline" className="h-8 gap-1.5 bg-card px-3 text-xs font-normal" type="button" asChild>- <a- href={EVERNOTE_MIGRATION_BLOG_URL}- target="_blank"- rel="noopener noreferrer"- aria-label={t("evernoteImport.openGuideAria")}- >- <ExternalLink className="h-3.5 w-3.5" />- {t("evernoteImport.guide")}- </a>- </Button>- </div>- </div>- </CardHeader>- </Card>- );-};diff --git a/apps/web/src/components/settings/ThirdPartyImportCard.tsx b/apps/web/src/components/settings/ThirdPartyImportCard.tsxnew file mode 100644index 00000000..a2632a0a--- /dev/null+++ b/apps/web/src/components/settings/ThirdPartyImportCard.tsx@@ -0,0 +1,26 @@+import { useSyncExternalStore } from "react";+import { Upload } from "lucide-react";+import { useTranslation } from "react-i18next";+import { Button } from "@/components/ui/button";+import { Card, CardDescription, CardHeader, CardTitle } from "@/components/ui/card";+import type { NoteImportController } from "@/lib/note-import/controller";+import { SETTINGS_CARD_DESCRIPTION_CLASSNAME, SETTINGS_CARD_HEADER_CLASSNAME, SETTINGS_CARD_ICON_CLASSNAME, SETTINGS_CARD_TITLE_CLASSNAME } from "./settings-ui";+import { ThirdPartyImportDialog } from "./ThirdPartyImportDialog";++export const ThirdPartyImportCard = ({ controller, onOpenMemo }: { controller: NoteImportController; onOpenMemo: (memoId: string, notebookId: string) => void }) => {+ const { t } = useTranslation();+ const state = useSyncExternalStore(controller.subscribe, controller.getSnapshot);+ const running = state.phase === "running" || state.phase === "scanning";+ return <Card className="w-full min-w-0 shadow-none">+ <CardHeader className={SETTINGS_CARD_HEADER_CLASSNAME}>+ <div className="flex flex-wrap items-center justify-between gap-3">+ <div className="min-w-0 space-y-1">+ <CardTitle className={SETTINGS_CARD_TITLE_CLASSNAME}><Upload className={SETTINGS_CARD_ICON_CLASSNAME} />{t("noteImport.title")}</CardTitle>+ <CardDescription className={SETTINGS_CARD_DESCRIPTION_CLASSNAME}>{t("noteImport.description")}</CardDescription>+ </div>+ <Button size="sm" variant="outline" onClick={controller.open}>{t(running ? "noteImport.viewProgress" : "noteImport.open")}</Button>+ </div>+ </CardHeader>+ <ThirdPartyImportDialog controller={controller} state={state} onOpenMemo={onOpenMemo} />+ </Card>;+};diff --git a/apps/web/src/components/settings/ThirdPartyImportDialog.tsx b/apps/web/src/components/settings/ThirdPartyImportDialog.tsxnew file mode 100644index 00000000..255f58e9--- /dev/null+++ b/apps/web/src/components/settings/ThirdPartyImportDialog.tsx@@ -0,0 +1,98 @@+import { useRef } from "react";+import { CheckCircle2, ExternalLink, FileUp, Folder, LoaderCircle, TriangleAlert } from "lucide-react";+import { useTranslation } from "react-i18next";+import { Button } from "@/components/ui/button";+import { Dialog, DialogContent, DialogDescription, DialogFooter, DialogHeader, DialogTitle } from "@/components/ui/dialog";+import { Select, SelectContent, SelectItem, SelectTrigger, SelectValue } from "@/components/ui/select";+import type { Notebook } from "@edgeever/shared";+import type { ImportFormat, ImportIssue } from "@edgeever/shared/note-import/types";+import type { NoteImportController, NoteImportSnapshot } from "@/lib/note-import/controller";+import { EVERNOTE_MIGRATION_BLOG_URL } from "@/lib/routes";++export const ThirdPartyImportDialog = ({ controller, state, onOpenMemo }: { controller: NoteImportController; state: NoteImportSnapshot; onOpenMemo: (memoId: string, notebookId: string) => void }) => {+ const { t } = useTranslation();+ const files = useRef<HTMLInputElement>(null);+ const directory = useRef<HTMLInputElement>(null);+ const busy = state.phase === "scanning" || state.phase === "running";+ const summary = state.summary;+ const failure = state.outcome?.failure;+ const selectFiles = (input: HTMLInputElement, folder: boolean) => {+ const selection = Array.from(input.files ?? []).map(file => ({ path: folder ? file.webkitRelativePath.split("/").slice(1).join("/") : file.name, file, expandArchive: !folder }));+ controller.select(selection); input.value = "";+ };+ const errorText = (code: string) => t(`noteImport.errors.${code}`, { defaultValue: code });+ const issueText = (issue: ImportIssue) => t(`noteImport.issues.${issue.code}`, { defaultValue: issue.code, count: issue.code === "ignoredTags" ? Number(issue.detail) : undefined });+ const foldersByKey = new Map(state.folders.map(folder => [folder.key, folder]));+ const folderDepth = (key: string): number => {+ const parent = foldersByKey.get(key)?.parentKey;+ return parent ? 1 + folderDepth(parent) : 0;+ };+ const destinationsById = new Map(state.destinations.map(notebook => [notebook.id, notebook]));+ const destinationPath = (notebook: Notebook): string => {+ const parent = notebook.parentId ? destinationsById.get(notebook.parentId) : undefined;+ return parent ? `${destinationPath(parent)} / ${notebook.name}` : notebook.name;+ };+ const openResult = (memoId: string, notebookId: string) => { controller.hide(); onOpenMemo(memoId, notebookId); };+ return <Dialog open={state.open} onOpenChange={open => { if (!open) controller.dismiss(); }}>+ <DialogContent className="flex max-h-[90dvh] flex-col gap-5 overflow-hidden sm:max-w-2xl" aria-describedby="note-import-target">+ <DialogHeader>+ <DialogTitle>{t("noteImport.title")}</DialogTitle>+ <DialogDescription id="note-import-target" className="break-all">{controller.target} · {controller.accountLabel}</DialogDescription>+ </DialogHeader>+ <div className="min-h-0 space-y-5 overflow-y-auto pr-1">+ {state.phase === "idle" ? <div className="space-y-4">+ <label className="grid gap-2 text-sm"><span>{t("noteImport.source")}</span>+ <Select value={state.format} onValueChange={value => controller.setFormat(value as ImportFormat)}>+ <SelectTrigger aria-label={t("noteImport.source")}><SelectValue /></SelectTrigger>+ <SelectContent><SelectItem value="evernote">Evernote / 印象笔记</SelectItem><SelectItem value="obsidian">Obsidian</SelectItem><SelectItem value="joplin">Joplin</SelectItem></SelectContent>+ </Select>+ </label>+ <div className="flex flex-wrap gap-2">+ <Button variant="outline" onClick={() => files.current?.click()}><FileUp className="h-4 w-4" />{t("noteImport.files")}</Button>+ <Button variant="outline" onClick={() => directory.current?.click()}><Folder className="h-4 w-4" />{t("noteImport.folder")}</Button>+ </div>+ <p className="text-sm text-muted-foreground">{state.format === "evernote" ? ".enex · .zip" : state.format === "obsidian" ? "Vault · .zip" : "RAW · Markdown · .zip · .jex"}</p>+ </div> : null}+ <input ref={files} type="file" multiple className="hidden" data-testid="note-import-files" accept={state.format === "evernote" ? ".enex,.zip,.notes" : state.format === "joplin" ? ".zip,.jex" : ".zip"} onChange={event => selectFiles(event.currentTarget, false)} />+ <input ref={directory} type="file" multiple className="hidden" data-testid="note-import-directory" {...{ webkitdirectory: "", directory: "" }} onChange={event => selectFiles(event.currentTarget, true)} />+ {state.phase === "scanning" ? <div className="space-y-3 py-3" role="status"><p className="flex items-center gap-2 text-sm"><LoaderCircle className="h-4 w-4 animate-spin" />{t("noteImport.scanning")}</p><p className="break-all text-sm text-muted-foreground">{state.scanningPath}</p></div> : null}+ {summary ? <dl className="grid grid-cols-3 gap-3 rounded-lg bg-muted/50 p-4 text-center">+ {([ ["notebooks", summary.notebooks], ["notes", summary.notes], ["resources", summary.resources] ] as const).map(([label, count]) => <div key={label}><dd className="text-2xl font-semibold tabular-nums">{count.toLocaleString()}</dd><dt className="mt-1 text-sm text-muted-foreground">{t(`noteImport.${label}`)}</dt></div>)}+ </dl> : null}+ {state.phase === "ready" ? <div className="space-y-4">+ <label className="grid gap-2 text-sm"><span>{t("noteImport.destination")}</span>+ <Select value={state.parentId || "__root"} onValueChange={value => controller.setParent(value === "__root" ? "" : value)}>+ <SelectTrigger aria-label={t("noteImport.destination")}><SelectValue /></SelectTrigger>+ <SelectContent><SelectItem value="__root">{t("noteImport.topLevel")}</SelectItem>{state.destinations.map(notebook => <SelectItem value={notebook.id} key={notebook.id}>{destinationPath(notebook)}</SelectItem>)}</SelectContent>+ </Select>+ </label>+ <div className="rounded-lg border p-3"><ul className="space-y-1 text-sm" aria-label={t("noteImport.folderPreview")}>{state.folders.map(folder => <li key={folder.key} className="flex items-center gap-2 break-all" style={{ paddingInlineStart: Math.min(folderDepth(folder.key), 10) * 12 }}><Folder className="h-3.5 w-3.5 shrink-0 text-muted-foreground" />{folder.name}</li>)}</ul>{summary && state.folders.length < summary.notebooks ? <Button variant="ghost" size="sm" className="mt-2" onClick={() => void controller.more("notebook")}>{t("noteImport.more")}</Button> : null}</div>+ </div> : null}+ {state.phase === "running" && state.progress ? <div className="space-y-2" role="status" aria-live="polite"><div className="flex items-center justify-between gap-3 text-sm"><span>{t(`noteImport.phases.${state.progress.phase}`)}</span><span className="tabular-nums">{state.progress.unit === "bytes" ? `${(state.progress.completed / 1048576).toFixed(1)} / ${(state.progress.total / 1048576).toFixed(1)} MiB` : `${state.progress.completed} / ${state.progress.total}`}</span></div><div className="h-1.5 overflow-hidden rounded-full bg-muted" role="progressbar" aria-valuemin={0} aria-valuemax={state.progress.total} aria-valuenow={state.progress.completed}><div className="h-full bg-foreground transition-[width]" style={{ width: `${state.progress.total ? state.progress.completed / state.progress.total * 100 : 0}%` }} /></div><p className="break-all text-sm text-muted-foreground">{state.progress.path}</p></div> : null}+ {(state.phase === "ready" && state.issues.length > 0) ? <details className="rounded-lg border p-3" open={summary && summary.errors > 0}>+ <summary className="cursor-pointer text-sm">{t("noteImport.issueCounts", { errors: summary?.errors ?? 0, warnings: summary?.warnings ?? 0 })}</summary>+ <ul className="mt-3 space-y-3 text-sm">{state.issues.map(issue => <li key={issue.key} className="break-words"><p className={issue.severity === "error" ? "text-destructive" : "text-muted-foreground"}>{issueText(issue)}</p><p className="break-all text-muted-foreground">{issue.path}{issue.detail && issue.code !== "ignoredTags" ? ` · ${issue.detail}` : ""}</p></li>)}</ul>+ {summary && state.issues.length < summary.errors + summary.warnings ? <Button size="sm" variant="ghost" className="mt-2" onClick={() => void controller.more("issue")}>{t("noteImport.more")}</Button> : null}+ </details> : null}+ {state.outcome ? <div className="space-y-3" role="status">+ <p className="flex items-center gap-2 text-sm font-medium">{state.outcome.completed === summary?.notes ? <CheckCircle2 className="h-4 w-4 text-emerald-600" /> : <TriangleAlert className="h-4 w-4" />}{t(state.outcome.cancelled ? "noteImport.cancelled" : state.outcome.completed === summary?.notes ? "noteImport.complete" : "noteImport.incomplete", { completed: state.outcome.completed, total: summary?.notes ?? 0 })}</p>+ {state.outcome.syncPending ? <p className="text-sm text-amber-700">{t("noteImport.syncPending")}</p> : null}+ {failure && !state.results.some(result => result.key === failure.key) ? <p className="break-all text-sm text-destructive">{failure.path} · {errorText(failure.uncertain ? "uncertainResource" : failure.message)}</p> : null}+ </div> : null}+ {state.error ? <p className="break-words text-sm text-destructive" role="alert">{errorText(state.error)}</p> : null}+ {state.phase === "finished" && state.results.length ? <div className="space-y-2 text-sm">{state.results.map(result => <div key={result.key} className="flex items-start justify-between gap-3"><div className="min-w-0"><p className="break-all">{result.path}</p>{!result.complete ? <p className="text-destructive">{errorText(result.uncertain ? "uncertainResource" : result.error ?? "incomplete")}</p> : null}</div><Button size="sm" variant="ghost" className="shrink-0" onClick={() => openResult(result.memo.id, result.memo.notebookId)}>{t("noteImport.openNote")}</Button></div>)}{state.results.length < state.resultCount ? <Button size="sm" variant="ghost" onClick={() => void controller.more("result")}>{t("noteImport.more")}</Button> : null}</div> : null}+ </div>+ <DialogFooter className="gap-2 sm:justify-between">+ <Button asChild variant="ghost" size="sm" className="px-0 text-muted-foreground"><a href={EVERNOTE_MIGRATION_BLOG_URL} target="_blank" rel="noopener noreferrer"><ExternalLink className="mr-1 h-3.5 w-3.5" />{t("noteImport.guide")}</a></Button>+ <div className="flex flex-wrap justify-end gap-2">+ <Button variant="outline" onClick={busy ? controller.cancel : controller.dismiss}>{t(busy ? "noteImport.cancel" : "noteImport.close")}</Button>+ {state.phase === "ready" ? <Button disabled={!summary?.notes || summary.errors > 0} onClick={controller.run}>{t("noteImport.confirm")}</Button> : null}+ {state.phase === "finished" && summary && !summary.errors ? <>+ {(state.outcome?.completed ?? 0) < summary.notes ? <Button onClick={controller.run}>{t("noteImport.retry")}</Button> : null}+ <Button variant="outline" onClick={() => void controller.restart()}>{t("noteImport.another")}</Button>+ </> : null}+ </div>+ </DialogFooter>+ </DialogContent>+ </Dialog>;+};diff --git a/apps/web/src/lib/api.ts b/apps/web/src/lib/api.tsindex 2d547e7c..95d07682 100644--- a/apps/web/src/lib/api.ts+++ b/apps/web/src/lib/api.ts@@ -405,6 +405,31 @@ export const api = {},};+/** Freeze destination/authentication for this foreground import, not other callers. */+export const createNoteImportClient = (signal: AbortSignal, accountId: string | null, isCurrent: () => boolean) => {+ const baseUrl = getConfiguredDesktopApiBaseUrl();+ const token = getDesktopSessionToken();+ let importContext: string | undefined;+ return createEdgeEverClient({+ baseUrl,+ token,+ fetch: async (url, init) => {+ if (!isCurrent() || getConfiguredDesktopApiBaseUrl() !== baseUrl || getDesktopSessionToken() !== token) {+ throw new DOMException("importIdentityChanged", "AbortError");+ }+ const headers = new Headers(init?.headers);+ if (importContext) headers.set("x-edgeever-import-context", importContext);+ const response = await fetch(url, { ...init, headers, signal: init?.method === "DELETE" ? undefined : signal });+ if (String(url).split("?")[0].endsWith("/api/v1/imports/capabilities") && response.ok) {+ const capability = await response.clone().json() as { importContext?: string; accountId?: string | null };+ if (capability.accountId !== accountId || !capability.importContext) throw new Error("importIdentityChanged");+ importContext = capability.importContext;+ }+ return response;+ },+ });+};+/** Keep an established desktop account usable when its server is unavailable. */export const getWorkspaceSession = async (): Promise<AuthSession> => {const baseUrl = getConfiguredDesktopApiBaseUrl();diff --git a/apps/web/src/lib/markdown-export.ts b/apps/web/src/lib/markdown-export.tsindex 3cc641b8..486cf64d 100644--- a/apps/web/src/lib/markdown-export.ts+++ b/apps/web/src/lib/markdown-export.ts@@ -13,11 +13,12 @@ export type MarkdownExportPage = {resources: Resource[];totalCount?: number;nextOffset: number | null;+ nextCursor?: string | null;};export type MarkdownExportSource = {listNotebooks: () => Promise<{ notebooks: Notebook[] }>;- getPage: (offset: number, limit: number) => Promise<MarkdownExportPage>;+ getPage: (offset: number, limit: number, cursor?: string) => Promise<MarkdownExportPage>;getResourceBlob: (resourceUrl: string) => Promise<Blob>;};@@ -71,11 +72,12 @@ export const createMarkdownExport = async (void (async () => {try {let offset = 0;+ let cursor: string | undefined;let completed = 0;let total = 0;while (true) {- const page = await source.getPage(offset, EXPORT_PAGE_SIZE);+ const page = await source.getPage(offset, EXPORT_PAGE_SIZE, cursor);total = page.totalCount ?? Math.max(total, completed + page.memos.length);onProgress?.({ completed, total });const resourcesByMemo = new Map<string, Resource[]>();@@ -123,6 +125,7 @@ export const createMarkdownExport = async (break;}offset = page.nextOffset;+ cursor = page.nextCursor ?? undefined;}zip.end();diff --git a/apps/web/src/lib/note-import/browser-index.ts b/apps/web/src/lib/note-import/browser-index.tsnew file mode 100644index 00000000..4b45a96f--- /dev/null+++ b/apps/web/src/lib/note-import/browser-index.ts@@ -0,0 +1,23 @@+import Dexie, { type Table } from "dexie";+import type { ImportIndex, ImportKind } from "@edgeever/shared/note-import/types";++type RecordRow = { id: string; importId: string; kind: ImportKind; key: string; value: unknown };+class ImportDatabase extends Dexie {+ records!: Table<RecordRow, string>;+ constructor() {+ super("edgeever-note-imports");+ this.version(1).stores({ records: "id, importId, [importId+kind]" });+ }+}+const database = new ImportDatabase();+export class BrowserImportIndex implements ImportIndex {+ constructor(readonly importId: string) {}+ private id(kind: ImportKind, key: string) { return `${this.importId}\0${kind}\0${key}`; }+ async put<T>(kind: ImportKind, key: string, value: T) { await database.records.put({ id: this.id(kind, key), importId: this.importId, kind, key, value }); }+ async get<T>(kind: ImportKind, key: string) { return (await database.records.get(this.id(kind, key)))?.value as T | undefined; }+ async page<T>(kind: ImportKind, offset = 0, limit = 50) { return (await database.records.where("[importId+kind]").equals([this.importId, kind]).offset(offset).limit(limit).toArray()).map(row => row.value as T); }+ count(kind: ImportKind) { return database.records.where("[importId+kind]").equals([this.importId, kind]).count(); }+ async remove(kind: ImportKind, key: string) { await database.records.delete(this.id(kind, key)); }+ async clearKind(kind: ImportKind) { await database.records.where("[importId+kind]").equals([this.importId, kind]).delete(); }+ async clear() { await database.records.where("importId").equals(this.importId).delete(); }+}diff --git a/apps/web/src/lib/note-import/browser-reader.test.mjs b/apps/web/src/lib/note-import/browser-reader.test.mjsnew file mode 100644index 00000000..3b06b0f1--- /dev/null+++ b/apps/web/src/lib/note-import/browser-reader.test.mjs@@ -0,0 +1,46 @@+import { expect, test } from "bun:test";+import { MemoryImportIndex } from "@edgeever/shared/note-import";+import { BrowserImportReader } from "./browser-reader";++test("worker failure rejects pending work and still permits task cleanup", async () => {+ const original = globalThis.Worker;+ let worker;+ class FailedWorker {+ constructor() { worker = this; }+ postMessage() {}+ terminate() { this.terminated = true; }+ }+ globalThis.Worker = FailedWorker;+ try {+ const index = new MemoryImportIndex();+ await index.put("chunk", "temporary", new Uint8Array(1));+ const reader = new BrowserImportReader(crypto.randomUUID(), "evernote", index, () => {});+ const scan = reader.scan([]);+ worker.onerror({ message: "Worker failed" });+ await expect(scan).rejects.toThrow("Worker failed");+ const closed = await Promise.race([reader.close().then(() => true), new Promise(resolve => setTimeout(() => resolve(false), 100))]);+ expect(closed).toBe(true);+ expect(worker.terminated).toBe(true);+ expect(await index.count("chunk")).toBe(0);+ } finally { globalThis.Worker = original; }+});++test("a closing Worker rejects new RPC instead of leaving it pending", async () => {+ const original = globalThis.Worker;+ let worker;+ class ClosingWorker {+ constructor() { worker = this; this.messages = []; }+ postMessage(message) { this.messages.push(message); }+ terminate() {}+ }+ globalThis.Worker = ClosingWorker;+ try {+ const reader = new BrowserImportReader(crypto.randomUUID(), "evernote", new MemoryImportIndex(), () => {});+ const closing = reader.close();+ const request = worker.messages.find(message => message.method === "close");+ const result = await Promise.race([reader.reset().then(() => "accepted", error => error.message), new Promise(resolve => setTimeout(() => resolve("pending"), 100))]);+ worker.onmessage({ data: { requestId: request.requestId } });+ await closing;+ expect(result).toBe("importClosed");+ } finally { globalThis.Worker = original; }+});diff --git a/apps/web/src/lib/note-import/browser-reader.ts b/apps/web/src/lib/note-import/browser-reader.tsnew file mode 100644index 00000000..8ef999a5--- /dev/null+++ b/apps/web/src/lib/note-import/browser-reader.ts@@ -0,0 +1,53 @@+import type { ImportDocument, ImportFile, ImportFormat, ImportIndex, ImportNote, ImportReader, ImportResource, ImportSummary } from "@edgeever/shared/note-import/types";++export class BrowserImportReader implements ImportReader {+ private worker = new Worker(new URL("./worker.ts", import.meta.url), { type: "module" });+ private number = 0;+ private pending = new Map<number, { resolve: (result: unknown) => void; reject: (error: Error) => void }>();+ private closed = false;+ private failure?: Error;+ private closing?: Promise<void>;+ constructor(readonly id: string, readonly format: ImportFormat, readonly index: ImportIndex, private progress: (path: string) => void) {+ this.worker.onmessage = ({ data }) => {+ if (data.progress !== undefined) { this.progress(data.progress); return; }+ const request = this.pending.get(data.requestId);+ if (!request) return;+ this.pending.delete(data.requestId);+ if (data.error) { const error = new Error(data.error.message); error.name = data.error.name; request.reject(error); }+ else request.resolve(data.result);+ };+ this.worker.onerror = event => {+ const error = new Error(event.message || "importWorkerFailed");+ this.failure = error;+ for (const request of this.pending.values()) request.reject(error);+ this.pending.clear();+ };+ }+ private request<T>(method: string, args?: unknown): Promise<T> {+ if (this.closed && method !== "close") return Promise.reject(new Error("importClosed"));+ if (this.failure) return Promise.reject(this.failure);+ return new Promise<T>((resolve, reject) => {+ const requestId = ++this.number;+ this.pending.set(requestId, { resolve: value => resolve(value as T), reject });+ this.worker.postMessage({ requestId, method, args });+ });+ }+ scan(files: ImportFile[]) { return this.request<ImportSummary>("scan", { id: this.id, format: this.format, files }); }+ reset() { return this.request<void>("reset"); }+ abort() { if (!this.closed) this.worker.postMessage({ method: "abort" }); }+ readNote(key: string) { return this.request<ImportDocument>("note", key); }+ readResourcePart(resource: ImportResource, start: number, end: number) { return this.request<Uint8Array<ArrayBuffer>>("part", { resource, start, end }); }+ releaseNote(note: ImportNote) { return this.request<void>("release", note); }+ finishReading() { return this.request<void>("finish"); }+ close() {+ if (this.closing) return this.closing;+ if (this.closed) return Promise.resolve();+ this.abort();+ this.closed = true;+ this.closing = (async () => {+ try { if (!this.failure) await this.request("close"); }+ finally { this.worker.terminate(); await this.index.clear(); }+ })();+ return this.closing;+ }+}diff --git a/apps/web/src/lib/note-import/controller.test.mjs b/apps/web/src/lib/note-import/controller.test.mjsnew file mode 100644index 00000000..098b2fcf--- /dev/null+++ b/apps/web/src/lib/note-import/controller.test.mjs@@ -0,0 +1,156 @@+import { expect, test } from "bun:test";+import { NoteImportController } from "./controller";++test("viewing a finished result preserves task mappings until explicit dismissal", async () => {+ await import("fake-indexeddb/auto");+ const { BrowserImportIndex } = await import("./browser-index");+ const { BrowserImportReader } = await import("./browser-reader");+ const previousWorker = globalThis.Worker;+ class IdleWorker {+ postMessage(message) {+ if (message.method !== "abort") queueMicrotask(() => this.onmessage({ data: { requestId: message.requestId } }));+ }+ terminate() {}+ }+ globalThis.Worker = IdleWorker;+ const id = crypto.randomUUID(), index = new BrowserImportIndex(id);+ const reader = new BrowserImportReader(id, "obsidian", index, () => {});+ const result = { key: "note-0", path: "note.md", memo: { id: "memo-id", notebookId: "folder-id" }, urls: {}, complete: false, error: "incomplete" };+ const controller = new NoteImportController(null, "http://import.test", () => "Owner", async () => {});+ try {+ await index.put("result", result.key, result);+ await index.put("folderResult", "folder:Folder", { id: "folder-id" });+ controller.reader = reader;+ controller.update({ phase: "finished", open: true, summary: { id, notes: 1, errors: 0 }, results: [result], resultCount: 1 });+ controller.hide();+ expect(controller.getSnapshot()).toMatchObject({ phase: "finished", open: false, results: [result], resultCount: 1 });+ controller.open();+ expect(controller.getSnapshot()).toMatchObject({ phase: "finished", open: true });+ expect(controller.reader).toBe(reader);+ expect(await index.get("result", result.key)).toEqual(result);+ expect(await index.get("folderResult", "folder:Folder")).toEqual({ id: "folder-id" });+ controller.dismiss();+ await reader.close();+ expect(controller.getSnapshot()).toMatchObject({ phase: "idle", results: [] });+ expect(await index.count("result")).toBe(0);+ } finally {+ controller.dispose();+ await reader.close();+ globalThis.Worker = previousWorker;+ }+});++for (const [kind, field] of [["notebook", "folders"], ["issue", "issues"], ["result", "results"]]) {+ test(`concurrent ${kind} pages are appended only once`, async () => {+ const controller = new NoteImportController(null, "http://import.test", () => "Owner", async () => {});+ const resolves = [];+ controller.reader = { index: { page: () => new Promise(done => resolves.push(done)) }, abort() {}, async close() {} };+ const first = controller.more(kind), second = controller.more(kind);+ resolves[1]([{ key: "page-item" }]); await second;+ resolves[0]([{ key: "page-item" }]); await first;+ expect(controller.getSnapshot()[field]).toEqual([{ key: "page-item" }]);+ controller.dispose();+ });++ test(`late ${kind} pages cannot repopulate a dismissed task`, async () => {+ const controller = new NoteImportController(null, "http://import.test", () => "Owner", async () => {});+ let resolve;+ const page = new Promise(done => { resolve = done; });+ controller.reader = { index: { page: () => page }, abort() {}, async close() {} };+ controller.update({ open: true, phase: "ready" });+ const pending = controller.more(kind);+ controller.dismiss();+ resolve([{ key: "old-task" }]);+ await pending;+ expect(controller.getSnapshot()[field]).toEqual([]);+ expect(controller.getSnapshot().phase).toBe("idle");+ controller.dispose();+ });+}++test("a dismissed scan cannot publish its delayed preflight pages", async () => {+ await import("fake-indexeddb/auto");+ const { BrowserImportIndex } = await import("./browser-index");+ const previousWorker = globalThis.Worker, previousPage = BrowserImportIndex.prototype.page;+ let started, resolve;+ const reading = new Promise(done => { started = done; });+ const page = new Promise(done => { resolve = done; });+ class ScanWorker {+ postMessage(message) {+ if (message.method === "abort") return;+ queueMicrotask(() => this.onmessage({ data: { requestId: message.requestId, result: message.method === "scan" ? { notes: 1, notebooks: 1, resources: 0, errors: 0, warnings: 0 } : undefined } }));+ }+ terminate() {}+ }+ globalThis.Worker = ScanWorker;+ BrowserImportIndex.prototype.page = function (kind) { if (kind === "notebook") { started(); return page; } return Promise.resolve([]); };+ const controller = new NoteImportController(null, "http://import.test", () => "Owner", async () => {});+ try {+ controller.select([{ path: "note.md", file: new File(["body"], "note.md") }]);+ await reading;+ const reader = controller.reader;+ controller.dismiss();+ resolve([{ key: "old-folder", name: "Old", parentKey: null }]);+ await controller.work;+ await reader.close();+ expect(controller.getSnapshot()).toMatchObject({ phase: "idle", open: false, folders: [], issues: [] });+ } finally {+ controller.dispose();+ BrowserImportIndex.prototype.page = previousPage;+ globalThis.Worker = previousWorker;+ }+});++for (const stage of ["initialization", "preview"]) test(`scan cancellation during ${stage} cannot publish late results`, async () => {+ await import("fake-indexeddb/auto");+ const { BrowserImportIndex } = await import("./browser-index");+ const previousWorker = globalThis.Worker, previousPage = BrowserImportIndex.prototype.page;+ const calls = [], terminated = [];+ let started, resolve, first = true;+ const reading = new Promise(done => { started = done; });+ const page = new Promise(done => { resolve = done; });+ class ScanWorker {+ number = calls.length;+ postMessage(message) {+ calls.push(message.method);+ if (message.method === "abort") return;+ queueMicrotask(() => this.onmessage({ data: { requestId: message.requestId, result: message.method === "scan" ? { notes: 1, notebooks: 1, resources: 0, errors: 0, warnings: 0 } : undefined } }));+ }+ terminate() { terminated.push(this.number); }+ }+ globalThis.Worker = ScanWorker;+ BrowserImportIndex.prototype.page = function (kind) {+ if (kind !== "notebook") return Promise.resolve([]);+ if (stage === "preview" && first) { first = false; started(); return page; }+ return Promise.resolve([{ key: "new-folder", name: "New", parentKey: null }]);+ };+ const controller = new NoteImportController(null, "http://import.test", () => "Owner", async () => {});+ const files = [{ path: "note.md", file: new File(["body"], "note.md") }];+ try {+ controller.update({ open: true });+ controller.select(files);+ if (stage === "preview") await reading;+ const oldReader = controller.reader;+ controller.cancel();+ const cleanup = controller.work;+ expect(controller.getSnapshot()).toMatchObject({ phase: "idle", open: true });+ if (stage === "initialization") {+ await cleanup;+ expect(calls).not.toContain("scan");+ } else {+ controller.select(files);+ resolve([{ key: "old-folder", name: "Old", parentKey: null }]);+ await Promise.all([cleanup, controller.work]);+ expect(controller.getSnapshot()).toMatchObject({ phase: "ready", folders: [{ key: "new-folder" }] });+ expect(controller.reader).not.toBe(oldReader);+ expect(terminated).toHaveLength(1);+ }+ } finally {+ resolve([]);+ controller.dispose();+ await controller.work;+ await controller.reader?.close();+ globalThis.Worker = previousWorker;+ BrowserImportIndex.prototype.page = previousPage;+ }+});diff --git a/apps/web/src/lib/note-import/controller.ts b/apps/web/src/lib/note-import/controller.tsnew file mode 100644index 00000000..aa589f12--- /dev/null+++ b/apps/web/src/lib/note-import/controller.ts@@ -0,0 +1,150 @@+import type { Notebook } from "@edgeever/shared";+import type { ImportFile, ImportFormat, ImportIssue, ImportNotebook, ImportNoteResult, ImportRunResult, ImportSummary } from "@edgeever/shared/note-import/types";+import type { ImportProgress } from "./execute";+import type { BrowserImportReader } from "./browser-reader";+import { api, createNoteImportClient } from "../api";++export type NoteImportSnapshot = {+ open: boolean; phase: "idle" | "scanning" | "ready" | "running" | "finished";+ format: ImportFormat; summary?: ImportSummary; error?: string;+ progress?: ImportProgress; scanningPath?: string; parentId: string;+ folders: ImportNotebook[]; issues: ImportIssue[]; results: ImportNoteResult[];+ destinations: Notebook[]; resultCount: number; outcome?: ImportRunResult;+};+export class NoteImportController {+ private snapshot: NoteImportSnapshot = { open: false, phase: "idle", format: "evernote", parentId: "", folders: [], issues: [], results: [], resultCount: 0, destinations: [] };+ private listeners = new Set<() => void>();+ private reader?: BrowserImportReader;+ private abort?: AbortController;+ private work: Promise<void> = Promise.resolve();+ private active = true;+ private generation = 0;+ constructor(readonly accountId: string | null, readonly target: string, private label: () => string, private refresh: () => Promise<void>) {}+ get accountLabel() { return this.label(); }+ subscribe = (callback: () => void) => { this.listeners.add(callback); return () => { this.listeners.delete(callback); }; };+ getSnapshot = () => this.snapshot;+ private update(patch: Partial<NoteImportSnapshot>) {+ if (!this.active) return;+ this.snapshot = { ...this.snapshot, ...patch };+ for (const listener of this.listeners) listener();+ }+ activate = () => { this.active = true; };+ open = () => {+ this.update({ open: true });+ void api.listNotebooks().then(({ notebooks }) => this.update({ destinations: notebooks })).catch(() => {});+ };+ setFormat = (format: ImportFormat) => { if (this.snapshot.phase === "idle") this.update({ format }); };+ setParent = (parentId: string) => { if (this.snapshot.phase === "ready") this.update({ parentId }); };+ select = (files: ImportFile[]) => {+ if (!files.length || this.snapshot.phase === "running" || this.snapshot.phase === "scanning") return;+ const generation = ++this.generation;+ const isCurrent = () => this.active && generation === this.generation;+ this.update({ phase: "scanning", error: undefined, summary: undefined, folders: [], issues: [], results: [], outcome: undefined, scanningPath: "", parentId: "" });+ this.work = (async () => {+ await this.reader?.close();+ if (!isCurrent()) return;+ this.reader = undefined;+ const [{ BrowserImportReader }, { BrowserImportIndex }] = await Promise.all([import("./browser-reader"), import("./browser-index")]);+ if (!this.active || generation !== this.generation) return;+ const id = crypto.randomUUID();+ const reader = new BrowserImportReader(id, this.snapshot.format, new BrowserImportIndex(id), path => { if (isCurrent()) this.update({ scanningPath: path }); });+ this.reader = reader;+ try {+ const summary = await reader.scan(files);+ if (!isCurrent()) return;+ const [folders, issues] = await Promise.all([reader.index.page<ImportNotebook>("notebook", 0, 30), reader.index.page<ImportIssue>("issue", 0, 20)]);+ if (isCurrent()) this.update({ phase: "ready", summary, folders, issues });+ } catch (error) {+ if (!isCurrent()) return;+ this.update({ phase: "idle", error: error instanceof Error && error.name === "AbortError" ? undefined : String(error instanceof Error ? error.message : error) });+ await reader.close();+ if (isCurrent()) this.reader = undefined;+ }+ })().catch(error => { if (isCurrent()) this.update({ phase: "idle", error: String(error) }); });+ };+ more = async (kind: "notebook" | "issue" | "result") => {+ const reader = this.reader, generation = this.generation;+ if (!reader || !this.active) return;+ const isCurrent = () => this.active && generation === this.generation && reader === this.reader;+ if (kind === "notebook") {+ const offset = this.snapshot.folders.length;+ const folders = await reader.index.page<ImportNotebook>(kind, offset, 30);+ if (isCurrent() && this.snapshot.folders.length === offset) this.update({ folders: [...this.snapshot.folders, ...folders] });+ }+ if (kind === "issue") {+ const offset = this.snapshot.issues.length;+ const issues = await reader.index.page<ImportIssue>(kind, offset, 20);+ if (isCurrent() && this.snapshot.issues.length === offset) this.update({ issues: [...this.snapshot.issues, ...issues] });+ }+ if (kind === "result") {+ const offset = this.snapshot.results.length;+ const results = await reader.index.page<ImportNoteResult>(kind, offset, 20);+ if (isCurrent() && this.snapshot.results.length === offset) this.update({ results: [...this.snapshot.results, ...results] });+ }+ };+ run = () => {+ if (!this.reader || !this.snapshot.summary?.notes || this.snapshot.summary.errors || !["ready", "finished"].includes(this.snapshot.phase)) return;+ const reader = this.reader;+ const generation = this.generation;+ this.abort = new AbortController();+ const abort = this.abort;+ const isCurrent = () => this.active && generation === this.generation;+ this.update({ phase: "running", error: undefined, progress: undefined });+ this.work = (async () => {+ try {+ await reader.reset();+ if (!isCurrent()) return;+ const [{ executeNoteImport }, { convertImportEnml }] = await Promise.all([import("./execute"), import("./enml")]);+ const outcome = await executeNoteImport({+ reader, index: reader.index,+ client: createNoteImportClient(abort.signal, this.accountId, isCurrent),+ parentId: this.snapshot.parentId || null, signal: abort.signal, convertEnml: convertImportEnml,+ pauseSync: window.edgeeverDesktop?.isAvailable ? async () => (await import("../desktop-sync")).pauseDesktopSyncForImport() : undefined,+ refresh: async () => { if (isCurrent()) await this.refresh(); },+ onProgress: progress => { if (isCurrent()) this.update({ progress }); },+ });+ const results = await reader.index.page<ImportNoteResult>("result", 0, 20), resultCount = await reader.index.count("result");+ if (isCurrent()) this.update({ phase: "finished", outcome, results, resultCount });+ } catch (error) {+ const code = typeof error === "object" && error && "status" in error && error.status === 404 && !this.snapshot.progress ? "unsupportedImportServer" : error instanceof Error ? error.message : String(error);+ if (!isCurrent()) return;+ const results = await reader.index.page<ImportNoteResult>("result", 0, 20), resultCount = await reader.index.count("result");+ if (isCurrent()) this.update({ phase: "finished", error: code, results, resultCount });+ }+ })();+ };+ cancel = () => {+ this.abort?.abort(); this.reader?.abort();+ if (this.snapshot.phase !== "scanning") return;+ const generation = ++this.generation;+ const reader = this.reader;+ this.reader = undefined;+ this.update({ phase: "idle", summary: undefined, scanningPath: "", folders: [], issues: [], error: undefined });+ this.work = this.work.then(() => reader?.close()).catch(error => {+ if (generation === this.generation) this.update({ error: String(error) });+ });+ };+ hide = () => { this.update({ open: false }); };+ dismiss = () => {+ if (this.snapshot.phase === "running") { this.update({ open: false }); return; }+ this.cancel();+ const generation = ++this.generation;+ const reader = this.reader;+ this.reader = undefined;+ this.update({ open: false, phase: "idle", summary: undefined, outcome: undefined, results: [], issues: [], folders: [], error: undefined });+ void this.work.then(() => reader?.close()).catch(error => {+ if (generation === this.generation) this.update({ error: String(error) });+ });+ };+ restart = async () => {+ await this.work;+ await this.reader?.close(); this.reader = undefined;+ this.generation++;+ this.update({ phase: "idle", open: true, summary: undefined, error: undefined, outcome: undefined, progress: undefined, results: [], issues: [], folders: [], parentId: "" });+ };+ dispose = () => {+ this.active = false; this.generation++;+ this.cancel();+ void this.work.finally(() => this.reader?.close()).catch(() => {});+ };+}diff --git a/apps/web/src/lib/note-import/enml.ts b/apps/web/src/lib/note-import/enml.tsnew file mode 100644index 00000000..028704be--- /dev/null+++ b/apps/web/src/lib/note-import/enml.ts@@ -0,0 +1,79 @@+import TurndownService from "turndown";+import { createDocument } from "@mixmark-io/domino";+import { tables, taskListItems } from "@joplin/turndown-plugin-gfm";+import type { ImportResource } from "@edgeever/shared/note-import/types";+import { resourceLinkToMarkdown, resourceNodeToMarkdown } from "../../../../../packages/shared/src/resource-links";++const hasStyleFlag = (node: Element, name: string) => new RegExp(`(?:^|;)\\s*${name}\\s*:\\s*true\\s*(?:;|$)`, "i").test(node.getAttribute("style") ?? "");++/** Normalize Evernote's current ENML to the HTML semantics Turndown supports. */+const prepareEnml = (source: string, resources: readonly ImportResource[]) => {+ const document = createDocument(source);+ const root = document.body;+ for (const node of Array.from(root.querySelectorAll("div[style]"))) {+ const style = node.getAttribute("style") ?? "";+ if (/(?:^|;)\s*--en-chs\s*:/i.test(style) && /(?:^|;)\s*display\s*:\s*none\s*(?:;|$)/i.test(style)) {+ node.remove();+ } else if (hasStyleFlag(node, "--en-codeblock")) {+ const pre = document.createElement("pre");+ const code = document.createElement("code");+ code.textContent = Array.from(node.childNodes, line => line.textContent ?? "").join("\n");+ pre.appendChild(code);+ node.parentNode!.replaceChild(pre, node);+ }+ }+ for (const list of Array.from(root.querySelectorAll("ul[style]"))) if (hasStyleFlag(list, "--en-todo")) {+ for (const item of Array.from(list.children)) if (item.nodeName === "LI") {+ const label = item.firstElementChild;+ if (label?.nodeName === "DIV") {+ while (label.firstChild) item.insertBefore(label.firstChild, label);+ label.remove();+ }+ const checkbox = document.createElement("input");+ checkbox.setAttribute("type", "checkbox");+ if (hasStyleFlag(item, "--en-checked")) checkbox.setAttribute("checked", "");+ item.insertBefore(checkbox, item.firstChild);+ }+ }+ for (const table of Array.from(root.querySelectorAll("table"))) {+ const row = table.querySelector("tr");+ if (!row?.children.length || row.parentElement?.nodeName === "THEAD" || Array.from(row.children).some(cell => cell.nodeName !== "TH")) continue;+ const head = document.createElement("thead");+ table.insertBefore(head, row.parentElement === table ? row : row.parentElement);+ head.appendChild(row);+ }+ for (const node of Array.from(root.querySelectorAll("en-media"))) {+ const hash = node.getAttribute("hash") || "missing";+ const label = (node.getAttribute("alt") || resources.find(resource => resource.hash === hash)?.filename || "Attachment").replace(/[\r\n]+/g, " ");+ const image = node.getAttribute("type")?.startsWith("image/");+ const media = document.createElement(image ? "img" : "a");+ media.setAttribute(image ? "src" : "href", `evernote-resource:${hash}`);+ if (image) media.setAttribute("alt", label);+ else media.textContent = label;+ const block = document.createElement("div");+ block.appendChild(media);+ node.parentNode!.replaceChild(block, node);+ }+ return root;+};++/** Parse in an inert DOM; no imported HTML is inserted into the application. */+export const convertImportEnml = (enml: string, resources: readonly ImportResource[] = []) => {+ const converter = new TurndownService({ headingStyle: "atx", bulletListMarker: "-", codeBlockStyle: "fenced", emDelimiter: "*" });+ converter.use([tables, taskListItems]);+ converter.remove(["script", "style", "iframe", "object", "embed"]);+ converter.addRule("evernote-media", {+ filter: node => (node.nodeName === "IMG" && node.getAttribute("src")?.startsWith("evernote-resource:"))+ || (node.nodeName === "A" && node.getAttribute("href")?.startsWith("evernote-resource:")) || false,+ replacement: (_, node) => node.nodeName === "IMG"+ ? resourceNodeToMarkdown({ type: "image", url: node.getAttribute("src")!, alt: node.getAttribute("alt") ?? "" })+ : resourceLinkToMarkdown(node.textContent ?? "", node.getAttribute("href")!),+ });+ converter.addRule("evernote-todo", { filter: "en-todo" as never, replacement: (_, node) => node.getAttribute("checked") === "true" ? "- [x] " : "- [ ] " });+ converter.addRule("evernote-crypt", { filter: "en-crypt" as never, replacement: (_, node) => `\n\n[Encrypted Evernote content]\n\n\`\`\`text\n${node.textContent?.replaceAll("```", "` ` `") ?? ""}\n\`\`\`\n\n` });+ converter.addRule("unsafe-links", { filter: node => node.nodeName === "A" && /^\s*(?:javascript|vbscript|file):/i.test(node.getAttribute("href") ?? ""), replacement: content => content });+ const source = enml.replace(/<\?xml[\s\S]*?\?>/gi, "").replace(/<!DOCTYPE[\s\S]*?>/gi, "")+ .replace(/<en-todo([^>]*?)(?:\/>|>\s*<\/en-todo>)/gi, "<en-todo$1>_</en-todo>")+ .replace(/<en-media([^>]*?)(?:\/>|>\s*<\/en-media>)/gi, "<en-media$1>_</en-media>");+ return converter.turndown(prepareEnml(source, resources));+};diff --git a/apps/web/src/lib/note-import/execute.ts b/apps/web/src/lib/note-import/execute.tsnew file mode 100644index 00000000..1082e58e--- /dev/null+++ b/apps/web/src/lib/note-import/execute.ts@@ -0,0 +1,171 @@+import type { createEdgeEverClient } from "@edgeever/client";+import { DEFAULT_MEMO_TITLE, type MemoDetail } from "@edgeever/shared";+import type { ImportIndex, ImportNotebook, ImportNote, ImportNoteResult, ImportReader, ImportRunResult, ImportIssue } from "@edgeever/shared/note-import/types";+import { indexItems } from "../../../../../packages/shared/src/note-import/index-store";+import { findImportReferences, rewriteImportMarkdown } from "../../../../../packages/shared/src/note-import/references";+import { resourceLinkToMarkdown } from "../../../../../packages/shared/src/resource-links";++export type ImportProgress = { phase: "folders" | "notes" | "resources" | "refresh"; path: string; completed: number; total: number; unit?: "bytes" };+type ImportClient = Pick<ReturnType<typeof createEdgeEverClient>, "getImportCapabilities" | "listNotebooks" | "createNotebook" | "importMemos" | "uploadMemoResourceParts" | "getMemo" | "createMemoEditSession" | "updateMemo">;+type FolderResult = { id?: string; uncertain?: boolean };+const statusOf = (error: unknown) => typeof error === "object" && error && "status" in error ? Number(error.status) : undefined;+const conflict = () => new Error("importConflict");+const sameBase = (a: ImportNoteResult["memo"], b: ImportNoteResult["memo"]) => a.revision === b.revision && a.contentHash === b.contentHash;+const memoBaseline = ({ id, notebookId, revision, contentHash, createdAt, updatedAt }: MemoDetail): ImportNoteResult["memo"] => ({ id, notebookId, revision, contentHash, createdAt, updatedAt });++export const executeNoteImport = async (input: {+ reader: ImportReader; index: ImportIndex; client: ImportClient;+ parentId?: string | null; signal?: AbortSignal;+ convertEnml: (source: string, resources?: ImportNote["resources"]) => string;+ pauseSync?: () => Promise<() => void>; refresh?: () => Promise<void>;+ onProgress?: (progress: ImportProgress) => void;+}): Promise<ImportRunResult> => {+ const { reader, index, client, signal, onProgress } = input;+ const result: ImportRunResult = { completed: 0, failed: 0, resources: 0, cancelled: false, syncPending: false };+ let release: (() => void) | undefined;+ let writesStarted = false;+ for await (const issue of indexItems<ImportIssue>(index, "issue")) if (issue.severity === "error") throw new Error("preflightErrors");+ const total = await index.count("note");+ signal?.throwIfAborted();+ const capabilities = await client.getImportCapabilities();+ if (capabilities.protocolVersion !== 1) throw new Error("unsupportedImportServer");+ if (input.parentId && !(await client.listNotebooks()).notebooks.some(nb => nb.id === input.parentId)) throw new Error("missingParent");+ const createdParent = await index.get<{ id: string | null }>("folderResult", "__destination");+ if (createdParent && createdParent.id !== (input.parentId ?? null)) throw new Error("destinationChanged");+ await index.put("folderResult", "__destination", { id: input.parentId ?? null });+ const folders = await index.count("notebook");+ let foldersDone = 0;+ const ensureFolder = async (key: string): Promise<string> => {+ signal?.throwIfAborted();+ const resultKey = `folder:${key}`;+ const existing = await index.get<FolderResult>("folderResult", resultKey);+ if (existing?.uncertain) throw new Error("uncertainNotebook");+ if (existing?.id) return existing.id;+ const folder = await index.get<ImportNotebook>("notebook", key);+ if (!folder) throw new Error("missingNotebook");+ const parentId = folder.parentKey ? await ensureFolder(folder.parentKey) : input.parentId;+ await index.put<FolderResult>("folderResult", resultKey, { uncertain: true });+ try {+ writesStarted = true;+ const { notebook } = await client.createNotebook({ name: folder.name, parentId });+ await index.put<FolderResult>("folderResult", resultKey, { id: notebook.id });+ return notebook.id;+ } catch (error) {+ if (statusOf(error) !== undefined && statusOf(error)! < 500) await index.remove("folderResult", resultKey);+ throw error;+ }+ };+ try {+ release = await input.pauseSync?.();+ for await (const folder of indexItems<ImportNotebook>(index, "notebook")) {+ onProgress?.({ phase: "folders", path: folder.key, completed: foldersDone, total: folders });+ await ensureFolder(folder.key); foldersDone++;+ }+ for await (const descriptor of indexItems<ImportNote>(index, "note")) {+ signal?.throwIfAborted();+ let state = await index.get<ImportNoteResult>("result", descriptor.key);+ if (state?.complete) { result.completed++; result.resources += Object.keys(state.urls).length; continue; }+ let priorUncertainty = state?.uncertain ?? false;+ let note = descriptor;+ let uploadCompleting = false;+ let createAttempted = false;+ let finalAttempted = false;+ try {+ onProgress?.({ phase: "notes", path: descriptor.path, completed: result.completed, total });+ signal?.throwIfAborted();+ const doc = await reader.readNote(descriptor.key, signal);+ note = doc.note;+ let original = doc.enml !== undefined ? input.convertEnml(doc.enml, note.resources) : doc.contentMarkdown;+ if (doc.enml !== undefined) {+ for (const resource of note.resources) if (!original.includes(`evernote-resource:${resource.hash}`)) {+ original += `\n\n${resourceLinkToMarkdown(resource.filename.replace(/[\r\n]+/g, " "), `evernote-resource:${resource.hash}`)}`;+ }+ }+ onProgress?.({ phase: "notes", path: note.path, completed: result.completed, total });+ signal?.throwIfAborted();+ if (!state) {+ const notebookId = await ensureFolder(note.notebookKey);+ createAttempted = true; writesStarted = true;+ const imported = await client.importMemos({ importId: reader.id, format: reader.format, notebookId, items: [{ externalId: note.key, title: note.title, tags: note.tags, contentMarkdown: original, createdAt: note.createdAt, updatedAt: note.updatedAt }] });+ const item = imported.results[0];+ if (!item?.memo || item.status === "failed") throw new Error(item?.error || "createFailed");+ if (item.status === "skipped" && (item.memo.contentMarkdown !== original || item.memo.title !== (note.title.trim() || DEFAULT_MEMO_TITLE) || JSON.stringify(item.memo.tags) !== JSON.stringify(note.tags))) throw conflict();+ state = { key: note.key, path: note.path, memo: memoBaseline(item.memo), urls: {}, complete: false };+ await index.put("result", note.key, state);+ }+ if (state.uncertain && !state.intendedMarkdown) throw new Error("uncertainResource");+ const current = (await client.getMemo(state.memo.id)).memo;+ priorUncertainty = false;+ if (!sameBase(current, state.memo)) {+ if (state.intendedMarkdown !== undefined && current.contentMarkdown === state.intendedMarkdown && current.createdAt === state.memo.createdAt && current.updatedAt === state.memo.updatedAt) {+ state.memo = memoBaseline(current); state.complete = true; state.error = undefined; state.uncertain = false; state.intendedMarkdown = undefined;+ await index.put("result", note.key, state); result.completed++; result.resources += Object.keys(state.urls).length;+ continue;+ }+ throw conflict();+ }+ for (const resource of note.resources) {+ signal?.throwIfAborted();+ if (Object.hasOwn(state.urls, resource.key)) continue;+ onProgress?.({ phase: "resources", path: `${note.path} / ${resource.filename}`, completed: Object.keys(state.urls).length, total: note.resources.length });+ uploadCompleting = false;+ const resourceState = state;+ const uploaded = await client.uploadMemoResourceParts(state.memo.id, {+ filename: resource.filename, mimeType: resource.mimeType, byteSize: resource.byteSize,+ readPart: (start, end) => reader.readResourcePart(resource, start, end, signal),+ onBeforeComplete: async () => {+ resourceState.uncertain = true;+ await index.put("result", note.key, resourceState);+ uploadCompleting = true;+ },+ onPartUploaded: bytes => onProgress?.({ phase: "resources", path: `${note.path} / ${resource.filename}`, completed: bytes, total: resource.byteSize, unit: "bytes" }),+ });+ Object.defineProperty(state.urls, resource.key, { value: uploaded.resource.url, enumerable: true, configurable: true, writable: true });+ state.uncertain = false;+ await index.put("result", note.key, state); uploadCompleting = false;+ }+ signal?.throwIfAborted();+ const references = doc.enml !== undefined ? findImportReferences(original).map(ref => ({ ...ref, resourceKey: note.resources.find(resource => `evernote-resource:${resource.hash}` === ref.href)?.key })) : note.references ?? [];+ const markdown = rewriteImportMarkdown(original, references, state.urls);+ if (markdown !== current.contentMarkdown) {+ const { editSession } = await client.createMemoEditSession(state.memo.id);+ if (editSession.baseRevision !== state.memo.revision || editSession.baseContentHash !== state.memo.contentHash) throw conflict();+ state.intendedMarkdown = markdown;+ await index.put("result", note.key, state);+ finalAttempted = true;+ const updated = await client.updateMemo(state.memo.id, { contentMarkdown: markdown, expectedRevision: state.memo.revision, expectedContentHash: state.memo.contentHash, editSessionId: editSession.id, createdAt: state.memo.createdAt, updatedAt: state.memo.updatedAt });+ state.memo = memoBaseline(updated.memo);+ }+ state.complete = true; state.error = undefined; state.uncertain = false; state.intendedMarkdown = undefined;+ await index.put("result", note.key, state);+ result.completed++; result.resources += Object.keys(state.urls).length;+ onProgress?.({ phase: "notes", path: note.path, completed: result.completed, total });+ } catch (error) {+ if (!signal?.aborted || state || createAttempted) result.failed++;+ if (state) {+ state.error = signal?.aborted ? "incomplete" : error instanceof Error ? error.message : String(error);+ state.uncertain = priorUncertainty || (uploadCompleting || finalAttempted) && (statusOf(error) === undefined || statusOf(error)! >= 500);+ await index.put("result", note.key, state);+ result.failure = { key: note.key, path: note.path, message: state.error, uncertain: state.uncertain ?? false };+ }+ if (!state && (!signal?.aborted || createAttempted)) result.failure = { key: note.key, path: note.path, message: signal?.aborted ? "incomplete" : error instanceof Error ? error.message : String(error), uncertain: createAttempted && statusOf(error) === undefined };+ if (signal?.aborted) result.cancelled = true;+ // A failed note is not a successful import; retain the rest unstarted.+ break;+ } finally { await reader.releaseNote(note); }+ }+ } catch (error) {+ if (signal?.aborted) result.cancelled = true;+ else throw error;+ } finally {+ release?.();+ try { await reader.finishReading?.(); }+ finally {+ if (writesStarted || result.completed || result.failed) {+ onProgress?.({ phase: "refresh", path: "", completed: result.completed, total });+ try { await input.refresh?.(); } catch { result.syncPending = true; }+ }+ }+ }+ return result;+};diff --git a/apps/web/src/lib/note-import/use-note-import.test.mjs b/apps/web/src/lib/note-import/use-note-import.test.mjsnew file mode 100644index 00000000..13a5a6df--- /dev/null+++ b/apps/web/src/lib/note-import/use-note-import.test.mjs@@ -0,0 +1,32 @@+import { expect, test } from "bun:test";+import { act, createElement } from "react";+import { createRoot } from "react-dom/client";+import { parseHTML } from "linkedom";+import { useNoteImport } from "./use-note-import";++test("display labels update without replacing a task; account and instance changes replace it", async () => {+ const keys = ["window", "document", "IS_REACT_ACT_ENVIRONMENT"];+ const previous = keys.map(key => Object.getOwnPropertyDescriptor(globalThis, key));+ const { window } = parseHTML("<html><body><div id='root'></div></body></html>");+ window.location = { origin: "http://import.test" };+ globalThis.window = window; globalThis.document = window.document; globalThis.IS_REACT_ACT_ENVIRONMENT = true;+ const root = createRoot(window.document.getElementById("root"));+ let controller;+ const Probe = ({ id, label }) => { controller = useNoteImport(id, label, async () => {}); return null; };+ try {+ await act(async () => root.render(createElement(Probe, { id: "account-a", label: "Old name" })));+ const first = controller;+ await act(async () => root.render(createElement(Probe, { id: "account-a", label: "New name" })));+ expect(controller).toBe(first);+ expect(controller.accountLabel).toBe("New name");+ await act(async () => root.render(createElement(Probe, { id: "account-b", label: "New name" })));+ expect(controller).not.toBe(first);+ const second = controller;+ window.edgeeverDesktop = { isAvailable: true, apiBaseUrl: "http://other-instance.test" };+ await act(async () => root.render(createElement(Probe, { id: "account-b", label: "New name" })));+ expect(controller).not.toBe(second);+ } finally {+ await act(async () => root.unmount());+ keys.forEach((key, i) => previous[i] ? Object.defineProperty(globalThis, key, previous[i]) : delete globalThis[key]);+ }+});diff --git a/apps/web/src/lib/note-import/use-note-import.ts b/apps/web/src/lib/note-import/use-note-import.tsnew file mode 100644index 00000000..45155843--- /dev/null+++ b/apps/web/src/lib/note-import/use-note-import.ts@@ -0,0 +1,14 @@+import { useEffect, useMemo, useRef } from "react";+import { getConfiguredDesktopApiBaseUrl } from "../api";+import { NoteImportController } from "./controller";++export const useNoteImport = (accountId: string | null, accountLabel: string, refresh: () => Promise<void>) => {+ const refreshRef = useRef(refresh);+ refreshRef.current = refresh;+ const labelRef = useRef(accountLabel);+ labelRef.current = accountLabel;+ const target = getConfiguredDesktopApiBaseUrl() || window.location.origin;+ const controller = useMemo(() => new NoteImportController(accountId, target, () => labelRef.current, () => refreshRef.current()), [accountId, target]);+ useEffect(() => { controller.activate(); return () => controller.dispose(); }, [controller]);+ return controller;+};diff --git a/apps/web/src/lib/note-import/worker.ts b/apps/web/src/lib/note-import/worker.tsnew file mode 100644index 00000000..721fbedf--- /dev/null+++ b/apps/web/src/lib/note-import/worker.ts@@ -0,0 +1,36 @@+import { NoteImportSession } from "@edgeever/shared/note-import";+import type { ImportFile, ImportFormat, ImportNote, ImportResource } from "@edgeever/shared/note-import/types";+import { BrowserImportIndex } from "./browser-index";++const scope = globalThis as unknown as { onmessage: ((event: MessageEvent) => void) | null; postMessage: (value: unknown, transfer?: Transferable[]) => void };+let session: NoteImportSession | undefined;+let abort = new AbortController();+let queue = Promise.resolve();+scope.onmessage = ({ data }) => {+ if (data.method === "abort") { abort.abort(); return; }+ queue = queue.then(async () => {+ try {+ let result: unknown;+ switch (data.method) {+ case "scan": {+ abort = new AbortController();+ const args = data.args as { id: string; format: ImportFormat; files: ImportFile[] };+ session = new NoteImportSession(args.format, args.files, new BrowserImportIndex(args.id), args.id);+ result = await session.scan(abort.signal, path => scope.postMessage({ progress: path })); break;+ }+ case "reset": await session?.finishReading(); abort = new AbortController(); break;+ case "finish": await session?.finishReading(); break;+ case "note": result = await session!.readNote(data.args as string, abort.signal); break;+ case "part": {+ const { resource, start, end } = data.args as { resource: ImportResource; start: number; end: number };+ result = await session!.readResourcePart(resource, start, end, abort.signal); break;+ }+ case "release": await session!.releaseNote(data.args as ImportNote); break;+ case "close": await session?.close(); session = undefined; break;+ }+ scope.postMessage({ requestId: data.requestId, result }, result instanceof Uint8Array ? [result.buffer] : []);+ } catch (error) {+ scope.postMessage({ requestId: data.requestId, error: { message: error instanceof Error ? error.message : String(error), name: error instanceof Error ? error.name : "Error" } });+ }+ });+};diff --git a/apps/web/src/lib/selected-markdown-export.ts b/apps/web/src/lib/selected-markdown-export.tsindex 9fcefe32..03102e95 100644--- a/apps/web/src/lib/selected-markdown-export.ts+++ b/apps/web/src/lib/selected-markdown-export.ts@@ -33,7 +33,7 @@ export const exportSelectedMemosAsMarkdownZip = async ({}: {memoIds: string[];listNotebooks: () => Promise<{ notebooks: Notebook[] }>;- getPage: (offset: number, limit: number, memoIds: string[]) => Promise<MarkdownExportPage>;+ getPage: (offset: number, limit: number, memoIds: string[], cursor?: string) => Promise<MarkdownExportPage>;getResourceBlob: (resourceUrl: string) => Promise<Blob>;onProgress?: (progress: MarkdownExportProgress) => void;save?: (blob: Blob) => void;@@ -55,8 +55,8 @@ export const exportSelectedMemosAsMarkdownZip = async ({let exported = 0;const blob = await createMarkdownExport({listNotebooks,- getPage: async (offset, limit) => {- const page = await getPage(offset, limit, exportable);+ getPage: async (offset, limit, cursor) => {+ const page = await getPage(offset, limit, exportable, cursor);exported += page.memos.length;return page;},diff --git a/apps/web/vite.config.ts b/apps/web/vite.config.tsindex 0f3d8750..11658278 100644--- a/apps/web/vite.config.ts+++ b/apps/web/vite.config.ts@@ -268,6 +268,8 @@ export default defineConfig({],resolve: {alias: {+ // Its browser export requires document; the default table also works in Workers.+ "decode-named-character-reference": fileURLToPath(import.meta.resolve("decode-named-character-reference")),"@": fileURLToPath(new URL("./src", import.meta.url)),},},diff --git a/bun.lock b/bun.lockindex f892ac36..179e40b1 100644--- a/bun.lock+++ b/bun.lock@@ -79,12 +79,15 @@"@types/node": "26.0.1","@types/react": "19.2.18","@types/react-dom": "19.2.3",+ "@types/sax": "1.2.7",+ "@types/spark-md5": "3.0.5","@types/turndown": "^5.0.6","@vitejs/plugin-react": "^6.1.2","concurrently": "10.0.3","esbuild": "0.28.1","fake-indexeddb": "^6.2.5","google-auth-library": "^11.0.2",+ "it-tar": "6.0.6","linkedom": "0.18.13","oxlint": "1.88.0","tailwindcss": "^4.3.2",@@ -105,7 +108,7 @@},"apps/desktop": {"name": "@edgeever/desktop",- "version": "2.2.0",+ "version": "2.16.1","dependencies": {"@agentclientprotocol/sdk": "^1.5.1","@edgeever/public-network": "workspace:*",@@ -244,7 +247,9 @@"@edgeever/client": "workspace:*","@floating-ui/react-dom": "^2.1.8","@fontsource-variable/noto-sans-sc": "^5.2.8",+ "@joplin/turndown-plugin-gfm": "1.0.68","@lezer/highlight": "^1.2.3",+ "@mixmark-io/domino": "2.2.0","@radix-ui/react-hover-card": "^1.1.23","@radix-ui/react-separator": "^1.1.15","@radix-ui/react-slot": "^1.3.3",@@ -260,6 +265,7 @@"@uiw/react-codemirror": "^4.25.11","ai": "7.0.137","beautiful-mermaid": "^1.1.3",+ "decode-named-character-reference": "1.3.0","docx-preview": "0.4.1","dompurify": "3.4.17","fabric": "7.4.0",@@ -316,9 +322,17 @@"@tiptap/pm": "3.31.4","@tiptap/starter-kit": "3.31.4","@zip.js/zip.js": "2.24.0",+ "it-tar": "6.0.6","js-base64": "3.9.3","katex": "0.18.2","marked": "17.0.6",+ "mdast-util-to-markdown": "2.1.2",+ "micromark": "4.0.2",+ "micromark-extension-gfm": "3.0.0",+ "micromark-util-decode-string": "2.0.1",+ "micromark-util-normalize-identifier": "2.0.1",+ "sax": "1.6.0",+ "spark-md5": "3.0.2","yaml": "2.8.3","zod": "4.6.6",},@@ -1766,6 +1780,8 @@"@types/semver": ["@types/[email protected]", "", {}, "sha512-FmgJfu+MOcQ370SD0ev7EI8TlCAfKYU+B4m5T3yXc1CiRN94g/SZPtsCkk506aUDtlMnFZvasDwHHUcZUEaYuA=="],+ "@types/spark-md5": ["@types/[email protected]", "https://registry.npmmirror.com/@types/spark-md5/-/spark-md5-3.0.5.tgz", {}, "sha512-lWf05dnD42DLVKQJZrDHtWFidcLrHuip01CtnC2/S6AMhX4t9ZlEUj4iuRlAnts0PQk7KESOqKxeGE/b6sIPGg=="],+"@types/trusted-types": ["@types/[email protected]", "", {}, "sha512-ScaPdn1dQczgbl0QFTeTOmVHFULt394XJgOQNoyVhZ6r2vLnMLJfBPd53SB52T/3G36VI1/g2MZaX0cwDuXsfw=="],"@types/turndown": ["@types/[email protected]", "", {}, "sha512-ru00MoyeeouE5BX4gRL+6m/BsDfbRayOskWqUvh7CLGW+UXxHQItqALa38kKnOiZPqJrtzJUgAC2+F0rL1S4Pg=="],@@ -3024,8 +3040,18 @@"isexe": ["[email protected]", "", {}, "sha512-6B3tLtFqtQS4ekarvLVMZ+X+VlvQekbe4taUkf/rhVO3d/h0M2rfARm/pXLcPEsjjMsFgrFgSrhQIxcSVrBz8w=="],+ "iso-constants": ["[email protected]", "https://registry.npmmirror.com/iso-constants/-/iso-constants-0.1.2.tgz", {}, "sha512-OTCM5ZCQsHBCI4Wdu4tSxvDIkmDHd5EwJDps5mKqnQnWJSKlnwMs3EDZ4n3Fh1tmkWkDlyd2vCDbEYuPbyrUNQ=="],+"isomorphic.js": ["[email protected]", "", {}, "sha512-PIeMbHqMt4DnUP3MA/Flc0HElYjMXArsw1qwJZcm9sqR8mq3l8NYizFMty0pWwE/tzIGH3EKK5+jes5mAr85yw=="],+ "it-reader": ["[email protected]", "https://registry.npmmirror.com/it-reader/-/it-reader-7.0.0.tgz", { "dependencies": { "it-stream-types": "^2.0.1", "uint8arraylist": "^3.0.1" } }, "sha512-bTWQPHH1if8N1K9XeidDjhTDm51ALbOksMa9uCZVVsQkoIPXP0XVKM88/Ynqr7HS18La/P1Kvu7C73j4rRoKWQ=="],++ "it-stream-types": ["[email protected]", "https://registry.npmmirror.com/it-stream-types/-/it-stream-types-2.0.4.tgz", {}, "sha512-tsX+klvMQ53J4Jm2B52vCIs7WD609ck+VS9X2TKMEv7VPY9VwaYKmSWyHek5QS0wHBtP0bWj9KMqCtAHgVKiXw=="],++ "it-tar": ["[email protected]", "https://registry.npmmirror.com/it-tar/-/it-tar-6.0.6.tgz", { "dependencies": { "iso-constants": "^0.1.2", "it-reader": "^7.0.0", "it-stream-types": "^2.0.1", "it-to-buffer": "^5.0.0", "p-defer": "^4.0.0", "uint8arraylist": "^3.0.2", "uint8arrays": "^6.1.1" } }, "sha512-+DXnOod8oC8iWEabGmQ039l5RnAeI4i6pl1BrPD1Bu1pGCy4YgxX1LjoJsj/OPLdXguOSH0pg/0rfceMhPEXXQ=="],++ "it-to-buffer": ["[email protected]", "https://registry.npmmirror.com/it-to-buffer/-/it-to-buffer-5.0.0.tgz", { "dependencies": { "uint8arrays": "^6.1.0" } }, "sha512-DyinWj+79wxFDQNiPZcmV8Fz2PJFOcM3KRRN4GiZiIdEfxO11cCkrZJMfUMrxryy+rLoNhAfh1bpoVUFHPR6pQ=="],+"jackspeak": ["[email protected]", "", { "dependencies": { "@isaacs/cliui": "^9.0.0" } }, "sha512-ykkVRwrYvFm1nb2AJfKKYPr0emF6IiXDYUaFx4Zn9ZuIH7MrzEZ3sD5RlqGXNRpHtvUHJyOnCEFxOlNDtGo7wg=="],"jake": ["[email protected]", "", { "dependencies": { "async": "^3.2.6", "filelist": "^1.0.4", "picocolors": "^1.1.1" }, "bin": { "jake": "bin/cli.js" } }, "sha512-wpHYzhxiVQL+IV05BLE2Xn34zW1S223hvjtqk0+gsPrwd/8JNLXJgZZM/iPFsYc1xyphF+6M6EvdE5E9MBGkDA=="],@@ -3410,6 +3436,8 @@"muggle-string": ["[email protected]", "", {}, "sha512-VNTrAak/KhO2i8dqqnqnAHOa3cYBwXEZe9h+D5h/1ZqFSTEFHdM65lR7RoIqq3tBBYavsOXV84NoHXZ0AkPyqQ=="],+ "multiformats": ["[email protected]", "https://registry.npmmirror.com/multiformats/-/multiformats-14.0.5.tgz", {}, "sha512-vbIm83F2yZ1pWJGS0yl0ysracIvv56LtbrIyiIQHoLdYDJOMoLfVFsXhh9DUH4SFdkdkFhucyWniihsNzVEjkQ=="],+"multimatch": ["[email protected]", "", { "dependencies": { "@types/minimatch": "^3.0.5", "array-differ": "^4.0.0", "array-union": "^3.0.1", "minimatch": "^3.0.4" } }, "sha512-I7tSVxHGPlmPN/enE3mS1aOSo6bWBfls+3HmuEeCUBCE7gWnm3cBXCBkpurzFjVRwC6Kld8lLaZ1Iv5vOcjvcQ=="],"multitars": ["[email protected]", "", {}, "sha512-6GwVw5eLi9sThdtlS4PKwC7yRLaf45pYhIEzKBHdKxi+YOXGKFX8acIniH+Uh/+k9mS2lQOupTccjoe5r0/1IQ=="],@@ -3512,6 +3540,8 @@"p-cancelable": ["[email protected]", "", {}, "sha512-BZOr3nRQHOntUjTrH8+Lh54smKHoHyur8We1V8DSMVrl5A2malOOwuJRnKRDjSnkoeBh4at6BwEnb5I7Jl31wg=="],+ "p-defer": ["[email protected]", "https://registry.npmmirror.com/p-defer/-/p-defer-4.0.1.tgz", {}, "sha512-Mr5KC5efvAK5VUptYEIopP1bakB85k2IWXaRC0rsh1uwn1L6M0LVml8OIQ4Gudg4oyZakf7FmeRLkMMtZW1i5A=="],+"p-limit": ["[email protected]", "", { "dependencies": { "yocto-queue": "^1.1.1" } }, "sha512-kuUqqHNUqoIWp/c467RI4X6mmyuojY5jGutNU0wVTmEOOfcuwLqyMVoAi9MKi2Ak+5i9+nhmrK4ufZE8069kHA=="],"p-locate": ["[email protected]", "", { "dependencies": { "p-limit": "^3.0.2" } }, "sha512-LaNjtRWUBY++zB5nE/NwcaoMylSPk+S+ZHNB1TzdbMJMny6dynpAGt7X/tl/QYq3TIeE6nxHppbo2LGymrG5Pw=="],@@ -3978,6 +4008,8 @@"space-separated-tokens": ["[email protected]", "", {}, "sha512-PEGlAwrG8yXGXRjW32fGbg66JAlOAwbObuqVoJpv/mRgoWDQfgH1wDPvtzWyUSNAXBGSk8h755YDbbcEy3SH2Q=="],+ "spark-md5": ["[email protected]", "https://registry.npmmirror.com/spark-md5/-/spark-md5-3.0.2.tgz", {}, "sha512-wcFzz9cDfbuqe0FZzfi2or1sgyIrsDwmPwfZC4hiNidPdPINjeUwNfv5kldczoEAcjl9Y1L3SM7Uz2PUEQzxQw=="],+"split": ["[email protected]", "", { "dependencies": { "through": "2" } }, "sha512-mTyOoPbrivtXnwnIxZRFYRrPNtEFKlpB2fvjSnCQUiAA6qAZzqwna5envK4uk6OIeP17CsdF3rSBGYVBsU0Tkg=="],"split-on-first": ["[email protected]", "", {}, "sha512-43ZssAJaMusuKWL8sKUBQXHWOpq8d6CfN/u1p4gUzfJkM05C8rxTmYrkIPTXapZpORA6LkkzcUulJ8FqA7Uudw=="],@@ -4172,6 +4204,10 @@"uhyphen": ["[email protected]", "", {}, "sha512-qz3o9CHXmJJPGBdqzab7qAYuW8kQGKNEuoHFYrBwV6hWIMcpAmxDLXojcHfFr9US1Pe6zUswEIJIbLI610fuqA=="],+ "uint8arraylist": ["[email protected]", "https://registry.npmmirror.com/uint8arraylist/-/uint8arraylist-3.0.2.tgz", { "dependencies": { "uint8arrays": "^6.0.0" } }, "sha512-LDVoq9BQaGJzGDUovEnoX6rpKCvnY/Jbtws4ikwnBzjRbq5qBAFpBZevUEbSmMM87aO0Sp+wOZy2ZXf5yODmXQ=="],++ "uint8arrays": ["[email protected]", "https://registry.npmmirror.com/uint8arrays/-/uint8arrays-6.1.1.tgz", { "dependencies": { "multiformats": "^14.0.0" } }, "sha512-iz7JN0XCSZYA111lhFG2Ui9EhFvTNekqSRHw3lvMHq+dzwWy1OQftxFQREEh4rffU0oSoXdQHsk2TiHKVm4fsA=="],+"ultrahtml": ["[email protected]", "", {}, "sha512-2xRd0VHoAQE4M+vF/DvFFB7pUV0ZxTW1TLi7lHQWnF/Sb5TPeEUV/l+hxcNnGO00ZXGnR0voCMmYRKQf+rvJ2g=="],"unbox-primitive": ["[email protected]", "", { "dependencies": { "call-bound": "^1.0.3", "has-bigints": "^1.0.2", "has-symbols": "^1.1.0", "which-boxed-primitive": "^1.1.1" } }, "sha512-nWJ91DjeOkej/TA8pXQ3myruKpKEYgqvpw9lz4OPHj/NWFNluYrjbz9j01CJ8yKQd2g4jFoOkINCTW2I5LEEyw=="],diff --git a/docs/changes/2026-10-27-third-party-import.md b/docs/changes/2026-10-27-third-party-import.mdnew file mode 100644index 00000000..ab0bd209--- /dev/null+++ b/docs/changes/2026-10-27-third-party-import.md@@ -0,0 +1,117 @@+# Third-party note import change scope++[简体中文](2026-10-27-third-party-import.zh-CN.md)++Related: [Issue #657](https://github.com/tianma-if/edgeever/issues/657). Contract: [third-party import plan](../third-party-import-plan.md).++## Problem and objective++Extend the former Evernote command-line guide entry with online, additive Web/desktop imports from Evernote, Obsidian and Joplin, preserving hierarchy, metadata, images and attachments. Preflight precedes writes; retries within a task reuse its created objects, while separate imports do not automatically merge existing data.++## Feature scope and acceptance++| ID | Allowed behavior | Acceptance |+| --- | --- | --- |+| I01 | Replace the guide card with shared third-party import while retaining the guide link. Web/desktop accept files, multiple ENEX files, directories, ZIPs, and Joplin JEX. Retain backup entries. | Select every source from Settings; old backup/guide entries remain available. |+| I02 | Add platform-neutral parsers, lazy source readers, and folder/resource indexes. Cover Evernote, Obsidian, Joplin RAW/Markdown/JEX under plan Section 3 without dropping notes based on resource directory names. | Add export fixtures and expectations before implementation; verify nested paths, bodies, metadata, and resources. |+| I03 | Map available metadata under Section 3. Warn on absent timestamps while retaining creation defaults; do not guess conflicting or timezone-ambiguous times. Preserve unknown Front Matter and encrypted source text. | Check original instants, missing/conflicting metadata feedback, and recoverable source text. |+| I04 | Preflight scans without remote writes and shows new notebook/note/resource counts, paths, destination identity, and issues. Users select top-level or an existing parent; no folder creation before confirmation. | Dismissing preflight writes nothing; planned and actual counts agree. |+| I05 | Add import capabilities and memo REST endpoints, reusing importMemosRecord, existing permissions, and 1–25-item batches. Fix source/item IDs per task. Unsupported older servers disable only this import entry with upgrade feedback. | Real API checks for idempotency, isolation, item failures, and old endpoint compatibility. |+| I06 | Create, upload, and rewrite one note at a time without text/title truncation or image recompression. Reuse a resource within one note; retain per-note ownership across notes. Expose already-supported optional update timestamps in the client; preserve source times and protect final writes using the import baseline and edit session. | Read actual EdgeEver resources without source access; read back times, tags, final bodies, and non-overwriting conflicts. |+| I07 | Preflight retains API limits: 80-character notebook names, 160-character titles, 100 MiB images, and 1 GiB attachments. Normalize tags in source order, remove empty/duplicate values and keep the first 24; ignore extras with a non-blocking preflight warning showing the ignored count. Explain ENEX export for .notes. Locate escaping paths, ambiguous/missing resources, and duplicate archive paths; do not follow symlinks, resolve external entities, or execute scripts. Optional-metadata warnings may be acknowledged. | Invalid sources identify issues and block confirmation without writes; valid relative paths remain usable. Excess-tag imports from all three sources succeed and read back the first 24; task-local retry introduces neither conflicts nor duplicates. |+| I08 | Scan/decompress in a Worker and page indexes into task-scoped IndexedDB. Do not buffer entire libraries of bodies/base64 or archives. Read ENEX sequentially twice; stage resources in chunks for existing multipart upload. Fail on exhausted temporary storage without whole-library memory fallback. | Measure large ENEX fields and 1/4 GiB archives with fixed note counts, memory, disk use, cancellation, and cleanup. |+| I09 | Workspace-owned controller, one active task per workspace. Bind identity/request context after confirmation; cancel on logout or instance/account changes and reject late cross-workspace results. Pause desktop sync, wait for active sync, release on every exit, then refresh. | Check settings unmounts, identity changes, and sync/readback after success/failure/cancellation. |+| I10 | Retain created notes while distinguishing incomplete, failed, cancelled, and unconfirmed outcomes. Task-local retry uses existing note/resource maps and missing steps only. Do not blindly replay uncertain directory/resource/final-update writes or delete existing data. New tasks remain additive. | First note succeeds, second resource fails, third has not started; retry creates no duplicates, conflicts do not overwrite, uncertain writes are not reported successful. |+| I11 | Add mature ZIP/SAX/TAR/incremental-MD5 dependencies and necessary build integration, client methods, shared subpath exports, existing locale text, and only relevant tests/sanitized evidence. | Prove browser builds/large-field behavior; run targeted flows and the three required typecheck/build commands. |++## Unchanged boundaries++- Preserve MCP imports, ordinary editing, offline queues, Rust structures, sync, backup and encrypted restore contracts. Apart from R28's export-index migration, add no database structures, global required settings or authorization scopes.+- No native mobile import entry, cross-run merging, resume after exiting the app, Obsidian plugin/inter-note conversion, remote-image capture or whole-library transaction rollback.+- Keep installers, automatic updates, versions and release/deployment mechanisms unchanged. Existing Cloudflare upgrades retain the Worker, D1, R2, administrator and authentication Secret without resetting passwords or replacing the instance. Tests use authored sources and isolated directories, never personal libraries.++## Important choices and evolution++Parse on the client and write through the shared API rather than adding a desktop offline-import protocol. This gives Web and Electron one behavior and Cloudflare and Docker one business implementation without extending Rust or sync protocols. Indexes contain locators and metadata; bodies and resources are read per note. Results retain only locators, version baselines and resource mappings rather than accumulating the library's bodies again.++The original rejection of more than 24 tags is superseded by I07/R11: normalize in source order, keep the first 24, ignore extras and warn. This retains the existing API storage boundary without blocking a library migration over surplus tags. Missing Joplin ancestors are export boundaries rather than invented folders; included cycles and invalid parent types remain errors.++Use mature Markdown parsers and serializers rather than global regex replacement or a dual-parser adapter. Optional image-link attributes fit the existing block-image schema; structural empty list paragraphs are handled only in the export projection, not by changing storage. Dependency choices follow the lockfile and actual TipTap contract, not drift in a local dependency directory.++## Source, write and lifecycle repairs++| ID | Allowed behavior | Acceptance |+| --- | --- | --- |+| R01 | Persist the unconfirmed marker before resource commit, retaining it after a lost response or failed mapping save. Repeated retries must not clear it and upload again. | Lose a response after the real API commits a resource; repeated retries leave exactly one resource. |+| R02 | Refresh folders after attempted directory writes fail or cancel before any note exists, after releasing the sync pause. | Fail the second folder; read back the first and check release before refresh. |+| R03 | Gate asynchronous pages/scan results by task generation and append concurrent copies of one page only once. Reject new RPCs while a Worker closes, avoiding stale state and hung promises. | Control page/close response ordering; discard old results, prevent duplicate pages and reject new requests promptly. |+| R04 | Key task identity by account and instance only; display-name changes retain the controller while updating its label. | React rerender retains the controller for label changes; account/instance changes still replace and dispose it. |+| R05 | Preserve task text and adjacent paragraphs after self-closing ENML todos. | Read back checked/unchecked task labels and the following paragraph. |+| R06 | Replace actual Markdown destination spans only, preserving nested labels, titles, reference uses and code. URI-encode literal fragment parentheses so existing attachment tokenizers do not truncate them. Reuse locked `[email protected]` with the existing GFM extension and decoding/identifier helpers as direct shared dependencies, without custom Markdown codecs. Remove the highlighting-AST/string-gate adapter. | Upload both nested-link resources through the real API; retain labels/titles/reference definitions and ignore unused definitions. |+| R07 | Distinguish explicitly selected archives from ordinary ZIP/JEX attachments inside a directory; expand only the former. | Read original archive attachment bytes without extra notes; direct archive import continues working. |+| R08 | Prefer the Vault root for non-explicit-relative Obsidian folder paths; treat Wiki filenames literally instead of URL-decoding them. Convert non-image Wiki embeds to existing readable attachment links, defaulting labels to filenames and retaining fragments. Remove the constant condition and unused alias lookup. | Resolve root/local collisions to root bytes and import filenames containing a literal percent sign. |+| R09 | Do not normalize invalid calendar dates into another day; retain existing invalid/missing-time warnings and creation defaults. | Warn for February 30 in a non-leap year; preserve valid leap days and timezone instants. |+| R10 | Classify RAW structure by source entry, not basename, preventing renamed RAW files from also becoming Markdown notes. | A renamed RAW entry creates one note with unchanged hierarchy/resources. |+| R11 | Canonicalize valid import tags before limiting to 24 (strip leading #, remove empty/duplicate values); replay comparisons use the server default for missing titles. Ordinary editing is unchanged. | All three sources retain correct counts with hash-only/prefixed tags; retry after a lost create response for an untitled RAW note creates no duplicates or false conflicts. |+| R12 | Do not apply the editor's 64 KiB property parsing budget to imported metadata. Preserve ordinary editing's limit and YAML syntax, mapping, alias and cycle protections. Retain original source; still persist only the first 24 tags. | Import valid Front Matter larger than 64 KiB and read back title, times, 24 tags and complete source; existing ordinary property protections still pass. |+| R13 | Follow Joplin's export contract: absent parents are the export boundary, while included hierarchy is preserved. A standalone note uses the existing default notebook; do not invent missing ancestors. Existing non-folder parents and cycles still fail. | Import standalone RAW notes and a JEX subtree; read back hierarchy, body and resources. Invalid parent types and cycles still block preflight. |+| R14 | Resolve `decode-named-character-reference` exactly to its own DOM-free default implementation in Web builds, fixing the import Worker's `document is not defined`. Development and production share this resolution with unchanged entity decoding semantics. Declare locked version 1.3.0 directly without upgrading other dependencies. | Import ENEX and a Vault using the production build; verify Worker startup, entity/escaped paths, images and attachments. |+| R15 | Attachment Markdown tokenizers must not consume partial links inside nested labels. Leave nested links/images to the existing Markdown engine; retain ordinary attachment recognition and persistence rules. | A real imported nested image remains an image in saved JSON and the actual page; existing attachment and PDF cases pass. |+| R16 | Preserve image outer links in optional `link: { href, title }` attributes throughout the shared image schema, Markdown parsing/export and aligned-image HTML. Split image paragraphs to fit the existing block-image schema, retaining container text order, required list paragraphs and galleries. Apply existing public-resource mapping to shared image outer links. Reuse locked `[email protected]` for link serialization and existing link safety rules without activating unsafe HTML targets or rewriting ordinary HTML anchors. Image previews, text links and sync/backup protocols remain unchanged. | After real import, rich-text edit, save, reopen and export retain outer resource URL/title and image src/title. Custom schemas, lists/quotes, galleries and aligned images round-trip links; sharing URLs, ordinary anchors and safety checks pass. |+| R17 | Preserve resource URL queries and fragments during public-share rewriting, covering image outer links and existing ordinary resource links. Replace only the resource base without changing permissions or share scope. | Retain an image link's `?download=1#page=3` in memo/notebook shares and Markdown export without mutating the source object. |+| R18 | Cancel scans during Worker initialization and preview loading: immediately invalidate that generation, return to selection and close its captured reader/index. Running cancellation still retains results and task-local retries. | Initialization cancellation sends no scan; cancelled previews cannot publish late results, and old cleanup cannot close a newly selected reader. |+| R19 | After attempted writes, refresh after releasing the sync pause even if Worker/temporary-resource cleanup fails. Preserve the cleanup error and completed writes. | A real API write followed by a failing finishReading still runs release→finish→refresh; the result and note remain readable. |+| R20 | Prefix source-folder result keys to separate them from the destination control record. Do not reject or rename a legal `__destination` source folder. | Same-named ENEX creates a real child under an existing parent; a failed top-level import retries without duplicate creation. |+| R21 | Recognize Joplin RAW/JEX metadata records that start directly with `id:` without a title/body separator. Non-note records, including note/tag associations, must not become notes; restore their tag relationships. | Real Joplin 3.7.21 desktop RAW/JEX exports scan and import as 5 notes, 3 notebooks and 6 note-owned resources; keep 24 of 30 tags. |+| R22 | Resolve Obsidian ZIP root resources inside a single top-level wrapper directory. Root `.obsidian` configuration contributes to root detection but is not imported. Preserve notebook paths, root-path precedence and directory archive attachments. | Real Obsidian 1.14.5 Vault ZIP and unwrapped directory both read back 6 notes and 6 resources; root resources still win local-name collisions. |+| R23 | Protect literal percent signs that are not two-hex-digit escapes before standard URI decoding of Markdown attachment paths. Wiki paths remain literal; traversal and invalid UTF-8 remain rejected. | Joplin desktop Markdown / Front Matter exports resolve `说明%20100%.txt` to the original bytes; existing traversal, ambiguity and Wiki percent cases still pass. |+| R24 | Under I02's body/task preservation contract, recognize current Evernote ENEX `--en-todo` / `--en-checked` lists and `--en-codeblock` blocks, excluding hidden layout metadata from visible content. Reuse locked `@mixmark-io/[email protected]`, already used by the clipper, for a detached DOM before Turndown and the existing GFM plugin handle lists, code blocks and escaping. Do not execute source styles/scripts; retain legacy `en-todo` support. | A real Evernote 11.40.2 export first reproduces lost task states/formatting; real API and Web imports then read back checked/unchecked tasks, code blocks, headings, tables, tags, original timestamps and all five attachment bytes. |+| R25 | Under I06's attachment preservation contract, label ENEX media with its original resource filename when `alt` is absent, retaining existing PDF recognition and preview controls. Normalize media to standard detached-DOM images/links and reuse Turndown escaping; existing `alt` still takes precedence. Resource context is an optional conversion-callback argument, preserving one-argument callers. | The real export's PDF shows its filename and opens a preview; other attachments/images retain names and original bytes. Ordinary ENEX, task-local retries and existing callbacks still pass. |+| R26 | Under I02's table preservation contract, put an ENML table's actual all-`th` first row in a standard `thead`, preventing `colgroup` from making the existing GFM plugin insert an empty header. Do not promote ordinary data rows. | The same real ENEX's two-row table still has two rows after import, retaining the original header text. |++## Large-library reads and exports++Per-note creation previously called `getNotebook` with repeated `COUNT/MAX`, producing `N × (N−1)` cumulative memo-row aggregate contributions for N notes in one folder. Web exports also rescanned and sorted successive OFFSET pages. R27 uses lightweight existence checks; R28 gives updated clients indexed continuation while retaining legacy fields, ordering, OFFSET calls and backup consistency.++| ID | Allowed behavior | Acceptance |+| --- | --- | --- |+| R27 | Use a lightweight ID/workspace/non-deleted lookup for boolean notebook existence checks in import/create, duplication, move/merge, child creation and parent changes. Preserve `getNotebook` statistics, permissions and existing missing-target errors. | Reproduce cumulative aggregates through real imports/folder creation, then verify large note/folder operations do not recount notes. Missing, foreign-workspace and deleted targets retain their existing rejection contract. |+| R28 | Add optional opaque `cursor` / `nextCursor` to browser JSON/ZIP and Markdown exports, retaining creation-time/ID ordering. A cursor carries the continuation position and traversed offset. Cursor requests omit repeated totals by default; explicit `includeTotal=1` still returns totals. Only malformed new cursors return `invalid_export_cursor` / HTTP 400. Preserve legacy OFFSET requests, response fields, defaults and ordering. Updated consumers retain OFFSET traversal when a server provides no cursor, without adding older-server prerequisites. Add an incremental migration for a `(workspace_id, is_deleted, created_at, id)` memo index. | Reproduce full page scans first. Verify complete ZIP/Markdown ordering, resources and revisions without gaps/duplicates, tied timestamps and selected-note filters. Upgrade a real prior schema/data set, preserving fields and old calls. Local whole-traversal work grows approximately linearly with indexed continuation queries. |++`0085_export_cursor_index.sql` adds one-time index-build reads and ongoing index write/storage costs without changing business fields. The main risk is skipped or duplicate page boundaries. Application rollback can restore old requests while leaving the index; removal requires another migration, never editing an executed file. Multipart upload retains `P + 2` R2 Class A operations per resource and at most three attempts per part. Add no import caps, required settings, automatic retries or remote reads. Measure representative local data rather than load-testing Cloudflare.++## Content preservation, staging and result state++| ID | Allowed behavior | Acceptance |+| --- | --- | --- |+| R29 | Restore dependencies from the existing lockfile first and check the actual TipTap 3.31.4/shared `marked` contract; do not upgrade the parser to accommodate local dependency-directory drift. Repair Markdown round-trips for image-only list items, preserving bullet/ordered/task lists, checked state, adjacent text and nesting, plus image and outer-link targets. Keep the existing list schema without new persisted nodes or attributes. | Reproduce under locked dependencies, then verify list structure, schema and linked images for existing JSON → Markdown → JSON and Markdown → JSON → Markdown. Check ordinary paragraphs, quotes and lists. Both typechecks, affected tests and Web build pass; exercise saving and reopening in the actual editor. |+| R30 | Serialize ENML media and otherwise unreferenced attachments through existing Markdown tooling so unmatched brackets, backslashes and other label characters are escaped correctly. Display CR/LF in labels as spaces; uploaded filenames retain the existing API normalization (trim surrounding whitespace, replace path separators with hyphens, remove control characters and keep the first 160 characters). Resource bytes and upload rules are unchanged. Use the same mature Markdown serializer for shared images; derive image alt from parsed inline tokens and decode PDF/file labels with the existing `micromark` escape/entity decoder, keeping syntax escapes out of visible filenames. Keep original label escapes when tokenizing image outer links, reusing Marked’s built-in link/reflink grammar and lexer without new syntax or an HTML fallback. Retain explicit `alt` precedence and introduce no filename rejection. | Import images/attachments with standalone `]`, `[`, backslashes, line breaks, spaces and percent signs through real HTTP. No `evernote-resource:` remains; image/attachment nodes and original bytes read back correctly. Existing URL-encoded path cases continue to pass. |+| R31 | Persist a pending-confirmation marker before creating a source folder. Retain it when creation succeeds but local mapping persistence fails, or when the commit outcome is unknown, preventing blind task-local recreation. Definite server rejection retains existing retry behavior; do not create remotely if the marker cannot be stored. | Real API runs simulate local-space failure before and after creation. After space recovers, safely retry or retain uncertainty without duplicate folders. |+| R32 | Treat Joplin `rawBinary` entries as candidates, confirming a payload only through a valid `type_: 4` RAW resource record belonging to a different source entry. Classify in stages without reading Markdown attachments in full. Unconfirmed Markdown remains a note regardless of resource-folder names or 32-character filenames. | Plain and RAW-shaped Markdown attachments upload as resources without extra notes or full-text preflight reads. Valid Markdown in resource folders without RAW descriptors and renamed descriptors remain supported. |+| R33 | Continue uploading identical same-note ENEX resources once. Remove staged chunks for deduplicated resources during per-note reading/release instead of accumulating them until task completion. | Real imports with duplicate embedded resources create one resource, preserve original bytes and leave no duplicate chunks after releasing that note. |+| R34 | Terminate ZIP read/write channels when decompression initialization, validation or cancellation fails so pending reads and cleanup settle. Retain existing error reporting without a password entry point or fallback. | An unreadable valid encrypted ZIP promptly yields preflight errors; cancellation settles and ordinary ZIP reading/cleanup still work. |+| R35 | Opening a result note hides the dialog without discarding the current task, folder mappings or retry state. Explicit result dismissal and importing another source retain their existing cleanup. | Open an incomplete note, return to Settings and retry with the same task/IDs; explicit dismissal still cleans up. |+| R36 | Use own-property semantics for filenames, MIME extensions and resource URL maps, safely storing legal keys such as `__proto__` without treating prototype properties as MIME values or uploaded resources. Add no filename restrictions. | Real uploads and retries of `toString`, `constructor`, `__proto__` and related names preserve final URLs, MIME values and original bytes without duplicates. |+| R37 | Store only required memo baselines in import results: IDs, notebook, revision/content fingerprint and source timestamps, not full Markdown/JSON/text. Clear intended Markdown after successful or confirmed commits; retain commit-comparison content only for the current incomplete item. | Multiple large completed notes leave no full bodies in the result index or UI snapshots. Lost-final-response verification, task-local retries and concurrent-edit protection still pass. |+| R38 | Use Turndown’s existing asterisk emphasis option for ENML so default underscores do not become literal text between adjacent Chinese characters. Preserve bold, links, tables and attachments without changing shared Markdown syntax. | The real Evernote export retains Chinese italics in saved API JSON and the Web page; bold and other formatting still pass. |++## Delivery contents++| ID | Allowed behavior | Acceptance |+| --- | --- | --- |+| C01 | Keep only concise bilingual acceptance conclusions in the evidence directory. Move screenshots, response captures, installation details and one-off scripts out, and update dependent documentation. Retain minimal authored source fixtures and independent expected bytes directly consumed by integration tests, marking the PDF fixture as binary in Git to preserve its format bytes. Remove the same temporary artifacts from unpublished feature history, retaining recovery copies outside the repository. | Neither the current tree nor history to be pushed contains these temporary artifacts. Tests still consume retained fixtures, and documentation has no broken references. |+| C02 | Remove the unreferenced `EvernoteImportGuideCard.tsx`; retain the legacy guide page, route, links and still-used translations. | Reference checks and Web build pass; the import entry and guide route remain usable. |++Keep screenshots and operational records outside Git and attach images to the PR. The repository retains bilingual conclusions, maintainable tests and the authored source fixtures they consume. Preserve an independent recovery copy before cleaning unpublished history; do not rewrite other branches.++## Risks and rollback++Import is high-risk data writing: partial new objects, duplicates, incorrect timestamps and resource references are the worst outcomes. Append only, retain sources, item outcomes and commit baselines; cancellation does not undo completed writes and code rollback does not delete imported notes. Removing the entry/routes leaves imported data usable as ordinary notes. Cleanup requires confirmed actual IDs, never bulk deletion by name.++Shared codec repairs affect editing round-trips for images, lists and attachment labels. Use source Markdown, original JSON and independent resource bytes as expectations; do not rewrite historical notes automatically. Before deployment, retain the Worker version, private D1 backup and an independent recovery entry. Stop later deployment steps if migrations or key acceptance fail and restore from these materials as needed.++## Completion reconciliation++I01–I11, R01–R38 and C01/C02 are implemented; every behavior in the task diff maps to these entries, with no additional input restrictions, permissions or sync/backup protocol changes. Focused checks cover source formats, hierarchy/metadata/original bytes, failure retries, concurrent-edit protection and resource cleanup. See [acceptance conclusions](evidence/2026-10-27-third-party-import/README.md) for the conditions and measurements of large-library reads, cursor exports, real-browser preflight and large-body result retention. Both typechecks, the Web build and targeted lint pass.++Cloudflare has R27–R30 and `0085`; the acceptance document records the real prior-instance upgrade and readback. R31–R38 are not deployed. Earlier macOS/Linux native results do not cover the latest repairs. Windows native, USB mobile devices, older-client editing, AppImage installation/cross-version updates and Docker container deployment remain unverified; builds and driver-level checks do not replace those flows.diff --git a/docs/changes/2026-10-27-third-party-import.zh-CN.md b/docs/changes/2026-10-27-third-party-import.zh-CN.mdnew file mode 100644index 00000000..00645e68--- /dev/null+++ b/docs/changes/2026-10-27-third-party-import.zh-CN.md@@ -0,0 +1,117 @@+# 第三方笔记导入变更清单++[English](2026-10-27-third-party-import.md)++关联:[Issue #657](https://github.com/tianma-if/edgeever/issues/657)。实现契约:[第三方导入方案](../third-party-import-plan.zh-CN.md)。++## 问题与目标++将原 Evernote 命令行指南入口扩展为 Web/桌面的联网追加导入,支持 Evernote、Obsidian 和 Joplin,保留目录、元数据、图片与附件。先预检再写入;同任务重试复用已创建对象,新的导入任务不自动合并旧数据。++## 功能范围与验收++| 编号 | 允许改变的行为 | 验收 |+| --- | --- | --- |+| I01 | 用统一第三方导入卡替换指南卡,保留原指南链接;Web/桌面提供文件、多个 ENEX、目录和 ZIP 选择,Joplin 另支持 JEX。原备份入口保留。 | 从设置实际选择每种来源;旧备份/指南入口仍可用。 |+| I02 | 增加平台无关的格式解析、源文件按需读取和目录/资源引用索引。完整支持方案第 3 节的 Evernote、Obsidian、Joplin RAW/Markdown/JEX 格式,不按附件目录名称误删其中的笔记。 | 先加入导出样本和明确预期,再实现;核对多级目录、正文、元数据及资源。 |+| I03 | 按方案第 3 节映射存在的元数据;缺失时间使用原创建接口默认值并预警,冲突和无法确定时区的时间不猜测。未知 Front Matter 和加密来源文本保留。 | 核对有效原始时刻、缺失/冲突提示和可恢复的原文。 |+| I04 | 预检仅扫描,不写远端;显示新增笔记本/笔记/资源数量、目录、目标实例账号及问题。顶层或指定现有父笔记本由用户选择;未确认不创建目录。 | 取消预检后零写入;预检与最终结果数量一致。 |+| I05 | 新增 imports capabilities 与 memos REST 入口,复用 importMemosRecord、原权限和 1–25 批次契约;任务级来源键与条目 ID 固定。旧服务端缺接口时仅该导入入口提示升级。 | 真实 API 验证幂等、隔离、逐项失败及旧接口兼容。 |+| I06 | 逐篇创建、上传并改写链接;不截断正文、标题或自动压图。同篇相同附件只上传一次,跨笔记依原资源归属分别上传。补齐客户端已有服务端时间更新字段,提交时保留导入时间并核对导入基线和编辑会话,不能覆盖人工编辑。 | 关闭来源文件后实际读取 EdgeEver 图片/附件;读回时间、标签、最终正文;冲突不覆盖。 |+| I07 | 预检按原 API 限制阻止无法保真导入的项目:目录名 80 字符、标题 160 字符、图片 100 MiB、附件 1 GiB;标签按来源顺序去空、去重并保留前 24 个,超出部分忽略,预检仅警告忽略数量、不阻止导入;.notes 提示导出 ENEX。越界路径、歧义资源、缺附件、重复归档路径均定位报错;不跟随符号链接、解析外部实体或执行脚本。可选元数据缺失等警告可确认继续。 | 用失败来源验证逐项问题、确认按钮状态及零写入;合法相对路径仍可用;三种来源的超量标签导入成功、服务端读回前 24 个,同任务重试不产生冲突或重复。 |+| I08 | 扫描/解压在 Worker,索引分页入任务级 IndexedDB;原始正文、base64 和归档不全量缓存。ENEX 两遍顺序读取,资源分块暂存并复用分片上传;临时空间不足明确失败,不回退整库内存缓存。 | 大 ENEX 字段及固定笔记数的 1/4 GiB 归档检查峰值内存、临时空间、取消和清理。 |+| I09 | 任务由工作区控制器持有,每工作区一次运行一个;确认后绑定实例/账号上下文,退出登录或切换实例/账号取消旧任务,迟到结果不污染新工作区。桌面写入前暂停同步,在途同步完成后写入;所有终止路径释放,再刷新。 | 验证设置卡卸载、身份变化,以及成功/失败/取消后的同步与读回。 |+| I10 | 保留已创建笔记并区分未完成、失败、取消和结果待确认。同任务重试复用笔记/资源映射,只补缺步骤;目录创建、资源提交及最终更新状态不明不盲重放。不自动删除已有数据;新的导入任务仍追加。 | 首篇成功、第二篇附件失败、第三篇未开始;重试不重复,版本冲突不覆盖,提交结果不明不虚报成功。 |+| I11 | 使用成熟 ZIP/SAX/TAR/增量 MD5 依赖及必要构建适配,新增 API 客户端、共享子路径和现有语言目录文案;仅添加上述行为的测试及脱敏验收材料。 | 先验证浏览器依赖和大字段行为;执行针对性流程及三项必需类型检查/构建。 |++## 保持不变的边界++- 保留 MCP 导入、普通编辑、离线队列、Rust 数据结构、同步协议、备份和加密恢复契约。除 R28 的导出索引 migration 外,不新增数据库结构、全局必填配置或权限。+- 不新增移动端导入入口、跨次自动合并、退出应用后续传、Obsidian 插件或跨笔记语法转换、远程图片抓取及全库事务回滚。+- 不改变安装器、自动升级、版本号和发布/部署机制。既有 Cloudflare 升级复用原 Worker、D1、R2、管理员和认证 Secret;不重置密码或替换实例。测试使用自造来源和隔离目录,不修改个人知识库。++## 重要选择与演进++采用客户端解析、共享 API 写入,而不是另建桌面离线导入协议:这样 Web 与 Electron 共用行为,Cloudflare 与 Docker 共用业务代码,不扩大 Rust 和同步协议。索引只保存定位与元数据,正文和资源逐篇读取;结果只保存定位、版本与资源映射,避免结果页重新累积全库正文。++标签原先按超过 24 个阻断处理,现由 I07/R11 的规则替代:按来源顺序规范化后保留前 24 个,忽略其余并警告。这样保留 API 的既有保存边界,同时不因多余标签阻断整库迁移。Joplin 未导出的祖先按来源导出边界处理,而不是伪造目录;包内循环和非法父类型仍拒绝。++Markdown 位置识别和序列化复用成熟解析器,不继续维护正则全局替换或双解析适配。图片外链作为可选属性保留,以适配现有块图片 schema;列表空首段只在导出投影中处理,不改存储结构。依赖以锁文件和实际 TipTap 契约为准,不为本机依赖目录漂移升级解析器。++## 来源、写入与生命周期修复++| 编号 | 允许改变的行为 | 验收 |+| --- | --- | --- |+| R01 | 附件提交前先持久化待确认标记,提交响应或后续映射保存失败时仍保留;连续重试不能清除此状态并再次上传。 | 真实 API 完成附件后丢弃响应,连续重试仍只有一个资源。 |+| R02 | 已开始创建目录但失败或取消、尚未创建笔记时,仍在释放同步暂停后刷新已写入目录。 | 第二个目录失败,读回第一个目录并核对释放先于刷新。 |+| R03 | 对所有异步分页/扫描结果使用当前任务代次,同页并发返回只追加一次;拒绝关闭中的 Worker 新 RPC,避免迟到结果污染新任务或悬挂。 | 控制分页返回和关闭应答顺序,丢弃旧任务结果,同页不重复,新请求及时失败。 |+| R04 | 任务身份只取账号和实例;更新显示名不销毁当前控制器,显示标签仍更新。 | React 钩子重渲染显示名后控制器不变;账号/实例变化仍更换并清理任务。 |+| R05 | ENML 自闭合待办保留后续任务文字与相邻段落,不让 HTML 解析吞掉文字。 | checked/unchecked 任务及后续段落读回完整正文。 |+| R06 | 仅替换 Markdown 实际目标范围,保留嵌套标签、title、引用式用法和代码;将片段中的字面括号做 URI 编码,避免既有附件 tokenizer 截断。复用已锁定的 `[email protected]` 、现有 GFM 扩展及其解码/标识符辅助包并声明 shared 直接依赖,不自写 Markdown 编解码;撤回高亮 AST 与字符串 gate 的双解析适配。 | 嵌套图片链接两资源真实上传,标题/标签/引用定义仍可解析;未用定义不上传。 |+| R07 | 区分直接选的归档与目录内的普通 ZIP/JEX 附件,只展开前者。 | 目录内 ZIP/JEX 作为附件读回原字节,无额外笔记;直接选归档仍可导入。 |+| R08 | Obsidian 非显式相对的文件夹路径优先从 Vault 根解析;Wiki 保留字面文件名,不当作 URL 解码;非图片 Wiki 嵌入转为现有可读附件链接,默认标签使用文件名,保留 fragment。去掉恒真判断及无消费方的 alias 查询。 | 根/当前目录同路径时读回正确根资源,单个百分号文件名可导入。 |+| R09 | 无效日历日期不归一化成另一天;沿用原有缺失/无效时间的预检警告和创建默认值。 | 非闰年 2 月 30 日提示无效;有效闰日和带时区时间保持原时刻。 |+| R10 | RAW 结构分类绑定源 entry,不依赖文件 basename,避免改名后的 RAW 再按 Markdown 导入。 | RAW 改名后仍只有一篇笔记、原有层级及资源不变。 |+| R11 | 导入前完成有效标签的稳定规范化(移除前导 # 后去空、去重,再取前 24 个),重放比较沿用服务端的无标题默认值;不改普通编辑规则。 | 多个 # 前缀及空标签的三来源计数、无标题 RAW 创建响应丢失后的重试均不重复或误报冲突。 |+| R12 | 导入元数据不沿用编辑器的 64 KiB 属性解析预算;保持普通编辑的原上限及 YAML 语法、映射、别名和循环保护。完整保留原文,标签仍只存前 24 个。 | 大于 64 KiB 的有效 Front Matter 导入并读回标题、时间、24 个标签和完整原文;普通属性解析的原有保护仍通过。 |+| R13 | 按 Joplin 官方导出契约,将未随包导出的父级视为导入边界,保留包内层级;单篇笔记使用既有默认笔记本,不伪造缺失祖先。现存非目录父级及循环仍报错。 | RAW 单篇笔记、JEX 子目录导入并读回层级、正文及资源;非法父类型和循环仍阻断预检。 |+| R14 | Web 构建将 `decode-named-character-reference` 精确解析到该库自带的无 DOM 默认实现,修复导入 Worker 的 `document is not defined`;开发与生产共用此解析,不改变实体解码语义。显式依赖已锁定的 1.3.0,不升级其他依赖。 | 生产构建实际导入 ENEX 与 Vault,确认 Worker 启动、实体及转义路径、图片和附件可用。 |+| R15 | 附件 Markdown tokenizer 不截取嵌套标签内的半个链接;交还现有 Markdown 引擎处理嵌套链接/图片,不改变普通附件识别和持久化规则。 | 真实导入的嵌套图片在保存的 JSON 与实际页面中仍为图片;现有附件和 PDF 用例通过。 |+| R16 | 图片使用可选 `link: { href, title }` 属性保留外层链接;共享图片 schema、Markdown 解析/导出和对齐图片 HTML 均保留该属性。图片段落按既有块图片 schema 分块,保留容器内文字顺序、列表首段和画廊;分享中的外层资源链接沿用现有公开资源映射。复用已锁定的 `[email protected]` 序列化链接,沿用链接安全规则,不激活危险 HTML 目标,不改写普通 HTML anchor。普通图片预览、文本链接及同步/备份协议不变。 | 真实导入后富文本编辑、保存、重新打开并导出,外层资源 URL/title 和图片 src/title 均保留;自定义图片 schema、列表/引用、画廊及对齐图往返不丢链接;分享 URL、普通 anchor 及安全检查通过。 |+| R17 | 公开分享改写资源地址时保留原 URL 的查询参数和 fragment,覆盖图片外层链接及既有普通资源链接;只替换资源基址,不改变权限或分享作用域。 | 图片链接的 `?download=1#page=3` 在单笔记/笔记本分享及 Markdown 导出中保持不变,原文对象不被修改。 |+| R18 | 扫描取消覆盖 Worker 初始化和预览加载阶段:立即使该任务代次失效、回到选择状态,并关闭该任务捕获的 reader/临时索引;运行中的取消仍保留结果及同任务重试。 | 初始化取消不发送 scan;预览取消后迟到结果不回填,立即选择新来源时旧清理不关闭新 reader。 |+| R19 | 已尝试写入后,即使 Worker/临时资源清理失败,也在释放同步暂停后尝试刷新;保留清理错误,不把已完成写入当作未发生。 | 真实 API 写入成功、finishReading 抛错时仍执行 release→finish→refresh,结果记录和笔记均可读回。 |+| R20 | 来源目录结果使用内部前缀,与目标父级控制记录分开存储;不拒绝或重命名合法的 `__destination` 来源目录。 | 同名 ENEX 在已有父级下创建实际子目录;顶层导入失败后重试仍成功且不重复创建。 |+| R21 | 识别 Joplin RAW/JEX 中从 `id:` 开始、没有标题或正文分隔段的元数据记录;标签关联等非笔记记录不创建笔记,仍按导出关联还原标签。 | 使用 Joplin 3.7.21 桌面真实 RAW/JEX 导出,预检和实际导入均为 5 篇笔记、3 个笔记本、6 个笔记所属资源;30 个标签保留 24 个。 |+| R22 | Obsidian ZIP 只有一个顶层包裹目录时,以该目录作为 Vault 根解析资源;根目录下的 `.obsidian` 配置仍参与根位置判定但不导入。保留笔记本路径和原有根路径优先级,不展开目录中的压缩附件。 | Obsidian 1.14.5 真实 Vault ZIP 和去除包裹层的目录均读回 6 篇笔记、6 个资源,根资源优先于同名局部资源。 |+| R23 | Markdown 附件路径混用百分号转义与字面 `%` 时,先保护不是两位十六进制转义的字面百分号,再执行标准 URI 解码;Wiki 仍按字面路径解析,越界与无效 UTF-8 仍拒绝。 | Joplin 桌面 Markdown / Front Matter 导出的 `说明%20100%.txt` 读回原字节;现有越界、歧义及 Wiki 百分号用例保持通过。 |+| R24 | 按 I02 的正文和待办保真契约,识别 Evernote 现行 ENEX 的 `--en-todo` / `--en-checked` 清单及 `--en-codeblock` 代码块,隐藏的版式元数据不进入可见正文。复用已锁定、剪藏器已使用的 `@mixmark-io/[email protected]` 解析独立 DOM,再交由 Turndown 及既有 GFM 插件处理列表、代码块和转义;不执行来源样式或脚本。旧 `en-todo` 标签继续可用。 | Evernote 11.40.2 实际导出先复现状态/格式丢失,再通过真实 API 和 Web 导入读回已勾选/未勾选待办、代码块、标题、表格、标签、原始时间及 5 个附件的完整字节。 |+| R25 | 按 I06 的附件保真契约,在 ENEX 媒体未提供 `alt` 时使用对应资源的原文件名作为正文标签,让 PDF 保持既有识别和预览入口。媒体先转为独立 DOM 中的标准图片/链接,复用 Turndown 转义;已有 `alt` 继续优先。转换回调的资源上下文为可选参数,原单参数用法保留。 | 真实导出中的 PDF 显示原文件名并可展开预览,其他附件和图片保留文件名/原字节;普通 ENEX、同任务重试及既有回调用法仍通过。 |+| R26 | 按 I02 的表格保真契约,将 ENML 表格中实际由 `th` 组成的首行归入标准 `thead`,避免 `colgroup` 使既有 GFM 插件额外添加空表头;不把普通数据行提升为表头。 | 同一真实 ENEX 的两行表格导入后仍为两行,首行保留原表头内容。 |++## 大库读取与导出++逐篇创建原先通过 `getNotebook` 重复执行 `COUNT/MAX`,单目录导入 N 篇的累计笔记行参与次数为 `N × (N−1)`。Web 导出原先逐页 OFFSET 扫描和排序。R27 改用轻量存在性查询;R28 为新版客户端采用索引续页,同时保留旧字段、排序、OFFSET 和备份一致性契约。++| 编号 | 允许改变的行为 | 验收 |+| --- | --- | --- |+| R27 | 笔记本存在性校验使用按 ID、工作区及未删除状态的轻量查询,覆盖导入/普通创建、复制、移动/合并及创建子目录、更换父级的布尔校验。不改变原 `getNotebook` 的统计字段、权限或不存在时的错误。 | 先用真实导入与目录创建复现累计聚合,再验证大量笔记/目录不重复计数;缺失、跨工作区和已删除目标仍按原契约拒绝。 |+| R28 | Web JSON/ZIP 和 Markdown 导出增加可选的不透明 `cursor` 与 `nextCursor`,按原创建时间、ID 顺序续页。游标包含续页位置和已遍历偏移;携带游标时默认不重复统计,显式 `includeTotal=1` 仍可取总数。仅新游标格式错误返回 `invalid_export_cursor` / HTTP 400。旧 OFFSET 请求、字段、默认值和排序保持;新版消费者在服务端未提供游标时继续原 OFFSET 路径,不增加旧服务端前置条件。新增递增 migration,为笔记添加 `(workspace_id, is_deleted, created_at, id)` 索引。 | 先复现分页全扫,再验证完整 ZIP/Markdown 的顺序、资源及修订无遗漏/重复,覆盖相同时间和选中笔记过滤;真实旧 Schema/数据迁至新索引后原字段与旧调用仍可用;本地完整续页累计工作量近似线性,相关查询走索引。 |++新增 `0085_export_cursor_index.sql` 有一次性构建读取及持续的索引写入/空间成本,不修改业务字段。主要风险为续页边界漏项或重复。应用回退可恢复旧请求方式,索引可保留;需要删除时另增 migration,不能修改已执行文件。分片上传继续沿用每资源 `P + 2` 次 R2 Class A 操作及每片最多三次尝试;不增加导入上限、必填配置、自动重试或远程读取。规模测量使用本地代表数据,不在 Cloudflare 压测。++## 内容保真、暂存与结果状态++| 编号 | 允许改变的行为 | 验收 |+| --- | --- | --- |+| R29 | 先按现有锁文件恢复依赖,核对 TipTap 3.31.4 和共享 `marked` 的实际版本契约;不为本机依赖目录漂移升级解析器。修复图片独占列表项时的 Markdown 往返,保留无序/有序/任务列表、勾选状态、相邻文字与嵌套层级,图片目标及外层链接保持不变。沿用现有列表 schema,不新增持久化节点或属性。 | 在锁定依赖下先复现,再验证已有 JSON → Markdown → JSON 及 Markdown → JSON → Markdown 的列表结构、schema 和图片链接;检查普通段落、引用和列表。两项类型检查、受影响测试及 Web 构建通过,并在实际编辑器中保存、重开。 |+| R30 | ENML 媒体及未被正文引用的附件,统一用现有 Markdown 序列化器输出标签和资源目标,正确转义不配对的方括号、反斜线等字符;标签中的 CR/LF 显示为空格;上传文件名仍沿用既有 API 的规范化(去首尾空白、将路径分隔符改为连字符、移除控制字符并保留前 160 个字符),资源字节和上传规则不变。共享图片的 Markdown 序列化也复用同一成熟工具;图片 alt 从解析后的行内 token 还原,PDF/普通附件标签用既有 `micromark` 解码函数还原标准转义与实体,不把语法转义写入可见文件名。外层图片链接的词法分析保留标签内的原始转义,复用 Marked 内置 link/reflink 语法和 lexer,不新增语法或 HTML fallback。保留已有 `alt` 优先规则,不新增文件名拒绝条件。 | 真实 HTTP 导入含单独 `]`、`[`、反斜线、换行、空格和百分号的图片/附件,正文不残留 `evernote-resource:`,图片及附件节点可读回,资源字节不变;原有 URL 编码路径用例继续通过。 |+| R31 | 创建来源目录前先持久化待确认标记;创建成功但本地映射写入失败,或提交结果不明时保留标记,禁止同任务盲目重复创建。明确的服务端拒绝仍按既有规则允许重试;本地无法记录标记时不发起创建。 | 真实 API 模拟创建前与创建后的本地空间不足,恢复空间后分别正常重试或保持待确认;实际目录不重复。 |+| R32 | Joplin 的 `rawBinary` 仅作为资源候选;关联到不同源条目的有效 `type_: 4` RAW 描述后才认定为附件。分阶段分类避免全文读取 Markdown 附件;未被资源描述确认的 Markdown 仍按笔记处理,不因目录名或 32 位文件名排除。 | 普通及 RAW 内容的 Markdown 附件只上传资源、不创建额外笔记且预检不全文读取;资源目录内无 RAW 描述的合法 Markdown 笔记仍可导入,改名的描述记录继续可用。 |+| R33 | ENEX 同篇同内容资源继续只上传一次;逐篇读取和释放时清理被去重资源的暂存块,避免跨篇累积到任务结束。 | 含重复内嵌资源的真实导入只创建一个资源;逐篇释放后不残留该篇重复资源块,原始字节一致。 |+| R34 | ZIP 解压初始化、校验或取消失败时终止读写通道,使等待中的读取和清理及时结束;沿用现有错误提示,不新增密码入口或 fallback。 | 合法加密 ZIP 的不可读取状态及时返回预检错误;取消不悬挂,正常 ZIP 读取及清理仍可完成。 |+| R35 | 结果页“打开笔记”仅隐藏弹窗并打开结果,不丢弃当前任务、目录映射或重试状态;显式关闭结果及导入另一份文件仍执行原清理。 | 打开未完成笔记后返回设置仍可同任务重试并复用原 ID;显式关闭仍清理任务。 |+| R36 | 文件名、扩展名及资源 URL 映射使用自有属性语义,安全写入 `__proto__` 等合法键,不把原型属性当作 MIME 或已上传资源;不增加文件名限制。 | `toString`、`constructor`、`__proto__` 等附件真实上传、重试不重复,最终 URL、MIME 和原始字节正确。 |+| R37 | 导入结果索引中的 memo 仅保存 ID、目录、版本、内容指纹和原始时间等必要基线,不保留完整正文、JSON 或摘要;成功或确认成功后清除待确认正文,仅当前未完成项保留核对提交所需内容。 | 多篇大正文完成后,结果索引及结果页快照不包含整篇正文;最终响应丢失核对、同任务重试和人工编辑冲突保护仍通过。 |+| R38 | ENML 斜体改用 Turndown 已有的星号分隔选项,避免默认下划线在相邻中文之间变成字面文本;保留原粗体、链接、表格及附件语义,不改共享 Markdown 语法。 | 真实 Evernote 导出的中文斜体在实际 API 保存的 JSON 和 Web 页面中仍为斜体;原有粗体与其他格式继续通过。 |++## 交付内容++| 编号 | 允许改变的行为 | 验收 |+| --- | --- | --- |+| C01 | 验收目录仅保留简短双语结论;移出截图、抓包、安装信息和一次性脚本,修正依赖这些文件的文档。保留集成测试直接使用的最小自造来源样本与独立预期字节,PDF 夹具在 Git 中声明为二进制以保持其格式字节。未推送功能提交中的同类临时产物也应移除,恢复副本保存在仓库外。 | 当前树和待推送分支历史不再含本次临时产物;测试夹具仍被实际测试消费,文档无失效引用。 |+| C02 | 删除无调用方的 `EvernoteImportGuideCard.tsx`;保留旧指南页面、路由、链接及仍使用的翻译。 | 引用检查和 Web 构建通过,第三方导入入口与指南路由仍可用。 |++截图和运行记录留在仓库外,PR 使用附件;仓库保留双语结论、可维护的测试及其实际消费的自造来源样本。提交历史整理前保留独立恢复副本,不改写其他分支。++## 风险与回退++导入是高风险数据写入:最坏结果为部分新对象、重复项、错误时间或资源引用。只追加对象,保留来源、逐项结果及提交基线;取消不撤销完成的写入,代码回退不删除已导入笔记。移除入口和新增路由后,已导入数据仍可按普通笔记使用。清理须依据实际 ID 确认,不能按名称批量删除。++共享编解码修复影响图片、列表和附件标签的编辑往返;以来源 Markdown、原 JSON 和独立资源字节为预期,不自动重写历史笔记。部署前保留 Worker 版本、私密 D1 备份及独立恢复入口,迁移或关键验收失败时停止后续部署并按备份恢复。++## 完成核对++I01–I11、R01–R38、C01/C02 已实现,实际 diff 的行为变化均对应上述条目;没有额外输入限制、权限或同步/备份协议变更。来源格式、目录/元数据/原字节、失败重试、并发编辑保护及资源清理通过针对性验证。大库读量、导出续页、真实浏览器预检及大正文结果留存的条件与测量见[验收结论](evidence/2026-10-27-third-party-import/README.zh-CN.md)。两项类型检查、Web 构建和目标 lint 通过。++Cloudflare 已部署 R27–R30 与 `0085`,真实旧实例升级及数据读回结论见验收文档;R31–R38 尚未部署。此前 macOS/Linux 原生结果未覆盖最新修复。Windows 原生、USB 移动真机、旧客户端编辑、AppImage 安装/跨版本升级和 Docker 容器部署仍未验证;不得将构建或驱动级检查作为这些验收的替代。diff --git a/docs/changes/evidence/2026-10-27-third-party-import/README.md b/docs/changes/evidence/2026-10-27-third-party-import/README.mdnew file mode 100644index 00000000..3c77df8f--- /dev/null+++ b/docs/changes/evidence/2026-10-27-third-party-import/README.md@@ -0,0 +1,103 @@+# Third-party import acceptance conclusions++[简体中文](README.zh-CN.md)++## Actual imports from three sources++The Web Settings entry and earlier isolated native runtimes passed the authored source fixtures below. Paths, bodies, tags, original timestamps and full attachment bytes read back correctly; images and PDFs are readable.++| Source | Source notebooks | Notes | Note-owned resources |+| --- | ---: | ---: | ---: |+| Evernote 11.40.2 ENEX | 1 | 1 | 5 |+| Obsidian 1.14.5 Vault | 4 | 6 | 6 |+| Joplin 3.7.21 JEX | 3 | 5 | 6 |++See the [real-source fixtures](../../../../tests/fixtures/note-import/real-apps/README.md) for provenance. Integration tests also cover Joplin RAW, plain Markdown / Front Matter, directories and ZIP. ENEX retains current task states, code blocks and its two-row table. Obsidian retains root-path precedence, literal percent signs and archive attachments inside directories. Joplin retains included hierarchy, tag associations and valid missing-ancestor boundaries.++- Cancelling preflight writes nothing. Tags beyond the first 24 are ignored in source order with a non-blocking warning.+- Task-local retries do not duplicate notes or attachments; uncertain commits are not blindly replayed. Final link updates do not overwrite concurrent human edits.+- Hiding the dialog or changing Settings categories retains a running task. Actual Web acceptance opens an incomplete note after attachment failure, returns to Settings and completes the same task by retry; explicit result dismissal cleans it up. Success, failure, cancellation and cleanup-error paths release paused sync and refresh committed writes.+- Image outer links, separate titles and resource URL suffixes survive editing, saving, reopening and export; ordinary HTML anchors are unchanged.++## Platform and upgrade coverage++- macOS: Electron 44.7.0 development shell and Rust sidecar with an isolated profile; imports, resource readback and resumed sync pass.+- Linux: Ubuntu 25.10 x86_64 / Xvfb, official v2.16.1 runtime with the import renderer. Three-source imports, failure retry, cancellation and full-restart readback pass; 16 acceptance notes and 22 cached resources persist.+- Cloudflare: an earlier candidate merged with upstream 2.16.1 passed an existing-instance upgrade and actual three-source imports. The `0072` → `0084` upgrade retained four existing notes and one resource, while upstream `0078` cleared factory colors on five default notebooks. The current R27–R30 API/Web and `0085` are now deployed; all pre-upgrade business fields in 18 notes, 19 notebooks, 23 resources and the workspace remain identical. The existing authenticated session, health and OpenAPI probes pass. Deployed Settings imports one additional ENEX note with original timestamps/tags and five matching attachment payloads; PNG/SVG load and the PDF is readable.+- Upgrading the original API against the same database retained existing bodies, tags, timestamps and resource bytes. Driver-level checks do not establish Docker container deployment acceptance.++Earlier native candidates do not cover the latest image/list and R31–R38 repairs. R31–R38 are not deployed to Cloudflare. Windows native, physical mobile devices, older-client editing, AppImage installation and cross-version automatic updates remain unverified. Current Cloudflare checks did not repeat password login or complete saving a Web ZIP to the local filesystem; the user can confirm these through the login and export entries.++## Large libraries and quotas++These are local measurements, not production D1 billing or Worker CPU data.++| Notes imported into one notebook | Pre-repair memo-row aggregate contributions | After R27 |+| --- | ---: | ---: |+| 100 | 9,900 | 0 |+| 1,000 | 999,000 | 0 |+| 2,000 | 3,998,000 | 0 |++A newly created empty notebook returns its full statistics once; existence checks no longer aggregate its notes. The same read amplification in parent and duplicate-note checks is also removed.++| Notes in complete export | Pre-repair OFFSET SQL VM instructions | Legacy OFFSET after R28 | New cursor after R28 |+| --- | ---: | ---: | ---: |+| 1,015 | 1,152,000 | 239,000 | 37,000 |+| 4,015 | 16,032,000 | 2,700,000 | 138,000 |++SQL VM counts are approximate. Cursors use the `(workspace_id, is_deleted, created_at, id)` index; legacy OFFSET still incurs cumulative offset skipping. Real HTTP → client → JSON ZIP / Markdown ZIP exports contain 79 notes, nine resources and 13 revisions. Bodies, tags, timestamps and independent attachment bytes match after the source server stops. Plain/encrypted backup, offline restoration and unchanged scheduled backup checks pass. The API/Web and `0085` are now deployed to the existing Cloudflare instance. Live JSON and Markdown exports each traverse three pages containing 19 notes and 28 resources, with no omissions or duplicates and no repeated total on continuation pages.++### Current-candidate browser scale checks++Actual Worker/IndexedDB preflight on macOS arm64, Ego Browser Chromium 156 and the development Web build, using separate small Markdown files with timestamps and no remote writes:++| Source | 1,000 notes | 4,000 notes |+| --- | ---: | ---: |+| Obsidian | 2.6 s | 9.9 s |+| Joplin Markdown | 2.4 s | 12.2 s |++Counts match the inputs, with no errors/warnings or remaining resource chunks. Four times the notes takes approximately 3.8/5.1 times as long. These are preflight measurements, not production per-note write throughput.++Actual Worker → IndexedDB → local D1 API import completes 96 bodies of 256 KiB each (24 MiB total) in 17.5 s. All 96 results serialize to approximately 36.7 KiB, with no full Markdown, JSON, text or intended-commit bodies and zero resource chunks. Separate SQLite API regressions pass multiple 1 MiB bodies, lost-response confirmation and concurrent human-edit protection.++Single large bodies remain subject to backend storage limits: [D1 limits a string, BLOB or row to 2,000,000 bytes](https://developers.cloudflare.com/d1/platform/limits/). Current content rows store Markdown, JSON and plain text together. A local D1 import and the existing ordinary-create entry both reject a single 1 MiB plain-text body; import returns `SQLITE_TOOBIG`. This existing storage boundary is unchanged. Large-library tests do not establish arbitrary per-note size support; oversized sources remain in the original files rather than being truncated and counted as successes.++### Implementation-baseline large resources++Implementation-baseline large-resource measurements use a separate profile per case. RSS includes the same-process Worker; heap covers only the main page. Test bytes are highly compressible, and storage estimates reflect browser-reported physical usage.++| Total input resources | Duration | Peak RSS | Peak heap | Peak estimated storage |+| --- | ---: | ---: | ---: | ---: |+| ZIP 1 GiB | 31.7 s | 1322.9 MiB | 131.7 MiB | 13.7 MiB |+| ZIP 4 GiB | 119.8 s | 1412.6 MiB | 154.7 MiB | 47.6 MiB |+| ENEX 192 MiB | 14.2 s | 1198.3 MiB | 135.4 MiB | 4.7 MiB |++All three cases end with zero temporary resource chunks. Generate large fixtures when needed rather than committing them:++```sh+bun tests/fixtures/note-import/generate-large.mjs /tmp/edgeever-note-import-large+```++## Reproduce affected checks++```sh+bun install --frozen-lockfile+bun test tests/note-import.integration.test.mjs packages/shared/src/image-link.test.mjs packages/shared/src/content.test.mjs+bun run typecheck+bun run typecheck:mobile+bun run build:web+```++Screenshots, request captures, installation details and one-off scripts are excluded from the product repository; this directory retains acceptance conclusions only.++## Image lists and special labels++Production Web imports a test ENEX with unmatched brackets, line breaks and literal `©` through Settings. Labels and images survive editing, saving and reopening, all five resource payloads match independent originals, and the PDF opens for reading. Uploaded filenames retain existing API normalization; visible label line breaks become spaces.++Bullet, ordered, task and nested lists retain four images in their list items after Markdown/rich-text editing, saving and reopening, preserving outer links, starting numbers and checked states. Real-source, shared-content, related image/editor and lifecycle checks, both typechecks, the Web build and targeted lint pass. Dependencies retain locked `marked@17`; no parser upgrade is needed.++ENML uses Turndown’s asterisk emphasis delimiter so Chinese italics adjacent to surrounding text remain italic, verified in the real Evernote export’s saved JSON and Web render.++## Task state and cleanup++Local-space failure before folder creation writes nothing remotely; failure to save a mapping after creation retains uncertainty without duplicate folders. Confirmed RAW Markdown attachments are not fully scanned as notes, while unconfirmed resource-folder notes remain supported. Same-note duplicate ENEX resources are cleaned per note; unreadable encrypted ZIPs and initialization cancellation settle promptly. Legal prototype-property attachment names upload and retry without duplicate resources or changed bytes.diff --git a/docs/changes/evidence/2026-10-27-third-party-import/README.zh-CN.md b/docs/changes/evidence/2026-10-27-third-party-import/README.zh-CN.mdnew file mode 100644index 00000000..13717ebb--- /dev/null+++ b/docs/changes/evidence/2026-10-27-third-party-import/README.zh-CN.md@@ -0,0 +1,103 @@+# 第三方导入验收结论++[English](README.md)++## 三种来源的实际导入++Web 设置入口及此前隔离的原生运行时已验证以下自造来源样本,目录、正文、标签、原始时间和附件完整字节均可读回,图片与 PDF 可阅读:++| 来源 | 来源笔记本 | 笔记 | 笔记所属资源 |+| --- | ---: | ---: | ---: |+| Evernote 11.40.2 ENEX | 1 | 1 | 5 |+| Obsidian 1.14.5 Vault | 4 | 6 | 6 |+| Joplin 3.7.21 JEX | 3 | 5 | 6 |++测试夹具及其来源见 [真实来源样本](../../../../tests/fixtures/note-import/real-apps/README.zh-CN.md)。集成测试还覆盖 Joplin RAW、普通 Markdown / Front Matter、目录与 ZIP。ENEX 保留现行待办状态、代码块和两行表格;Obsidian 保留根路径优先级、字面百分号和目录内压缩附件;Joplin 保留包内层级、标签关联及合法的缺失祖先边界。++- 预检取消零写入;超出 24 个的标签按来源顺序忽略并警告,不阻止导入。+- 同任务失败重试不重复创建笔记或附件;未知提交状态不盲目重放。并发人工编辑不被最终链接更新覆盖。+- 隐藏弹窗或切换设置后仍可恢复运行中的任务。实际 Web 中,附件失败后打开未完成笔记、返回设置,仍能同任务重试并完成;显式关闭结果才清理任务。成功、失败、取消和清理异常路径均释放同步暂停并刷新已写入内容。+- 图片外层链接、独立标题及资源 URL 后缀通过编辑、保存、重开和导出读回;普通 HTML anchor 不被改写。++## 平台与升级范围++- macOS:Electron 44.7.0 开发壳及 Rust sidecar,使用隔离用户目录,导入、附件读回和同步恢复通过。+- Linux:Ubuntu 25.10 x86_64 / Xvfb,官方 v2.16.1 运行时配合导入功能渲染器。三来源导入后,失败重试、取消及完整重启读回通过;16 篇验收笔记和 22 个缓存资源保留。+- Cloudflare:此前合并上游 2.16.1 的候选完成既有实例升级与三来源实际导入。`0072` → `0084` 保持 4 篇既有笔记及 1 个资源,5 个默认笔记本配色按上游 `0078` 清除。当前 R27–R30 的 API/Web 及 `0085` 已部署,升级前的 18 篇笔记、19 个笔记本、23 个资源和工作区业务字段保持一致;既有登录会话、健康检查和 OpenAPI 探针通过。线上设置入口再导入 1 篇 ENEX,原始时间、标签和 5 个附件字节一致,PNG/SVG 加载且 PDF 可阅读。+- 同一数据库从原 API 基线升级后,已有正文、标签、时间和资源字节保持一致。Docker 驱动的接口检查不等同于 Docker 容器部署验收。++旧原生候选不覆盖最新图片/列表及 R31–R38 修复;R31–R38 尚未部署到 Cloudflare。Windows 原生、移动端真机、旧客户端编辑、AppImage 安装及跨版本自动升级仍未验证。当前 Cloudflare 验证未重新输入密码登录,Web ZIP 的本机保存也未完成;这两项需用户通过登录页和导出入口确认。++## 大库与额度++以下均为本地测量,不是线上 D1 账单或 Worker CPU 数据。++| 单目录导入笔记数 | 修前累计笔记聚合行参与次数 | R27 修后 |+| --- | ---: | ---: |+| 100 | 9,900 | 0 |+| 1,000 | 999,000 | 0 |+| 2,000 | 3,998,000 | 0 |++新建空目录仅返回一次完整统计;存在性校验不再统计目录中的笔记。子目录父级和复制笔记的相同读取放大也已移除。++| 完整导出笔记数 | 修前 OFFSET SQL VM 指令 | R28 后旧 OFFSET | R28 后新游标 |+| --- | ---: | ---: | ---: |+| 1,015 | 1,152,000 | 239,000 | 37,000 |+| 4,015 | 16,032,000 | 2,700,000 | 138,000 |++SQL VM 指令为近似值。新游标使用 `(workspace_id, is_deleted, created_at, id)` 索引;旧 OFFSET 仍保留累计跳过偏移的开销。真实 HTTP → 客户端 → JSON ZIP / Markdown ZIP 导出 79 篇笔记,包含 9 个资源、13 个修订;关闭来源服务后正文、标签、时间及独立附件字节一致。普通/加密备份、离线恢复和无变更定时备份通过。API/Web 与 `0085` 已部署到既有 Cloudflare 实例;线上 JSON 和 Markdown 导出各读取 3 页,均返回 19 篇笔记、28 个资源,无遗漏或重复,续页不重复返回总数。++### 当前候选的浏览器规模验证++macOS arm64、Ego Browser Chromium 156、开发 Web 的实际 Worker/IndexedDB 预检,输入为带时间元数据的独立小 Markdown 文件,不写远端:++| 来源 | 1,000 篇 | 4,000 篇 |+| --- | ---: | ---: |+| Obsidian | 2.6 s | 9.9 s |+| Joplin Markdown | 2.4 s | 12.2 s |++计数与输入一致,均无错误或警告,完成后无资源暂存块。来源数增至四倍,预检耗时约为 3.8/5.1 倍;这些测量覆盖预检,不代表线上逐篇写入吞吐。++真实 Worker → IndexedDB → 本地 D1 API 导入 96 篇各 256 KiB 正文(合计 24 MiB)耗时 17.5 s,96 篇均完成。全部结果序列化约 36.7 KiB,不含完整 Markdown、JSON、摘要或待确认正文,临时资源块为零。另有 SQLite API 的多篇 1 MiB 正文、响应丢失后的确认和人工编辑冲突回归通过。++单篇大正文仍受后端存储边界约束:[D1 单个字符串、BLOB 或行最多 2,000,000 字节](https://developers.cloudflare.com/d1/platform/limits/)。当前正文行同时保存 Markdown、JSON 和纯文本;本地 D1 的单篇 1 MiB 纯文本在导入和原有普通创建中均失败,导入返回 `SQLITE_TOOBIG`。这项已有存储限制未在本次改变,不能将大库测试表述为任意单篇大小均可导入;超限来源保留在原文件中,不能截断后计为成功。++### 实现基线的大附件测量++实现基线的大附件测量使用每项独立的用户目录,RSS 包含同进程 Worker,heap 仅包含主页面;测试字节可高度压缩,存储数值为浏览器物理占用估计:++| 输入资源总量 | 用时 | 峰值 RSS | 峰值 heap | 峰值存储估计 |+| --- | ---: | ---: | ---: | ---: |+| ZIP 1 GiB | 31.7 s | 1322.9 MiB | 131.7 MiB | 13.7 MiB |+| ZIP 4 GiB | 119.8 s | 1412.6 MiB | 154.7 MiB | 47.6 MiB |+| ENEX 192 MiB | 14.2 s | 1198.3 MiB | 135.4 MiB | 4.7 MiB |++三项完成后临时资源块均为零。大样本按需生成,不存入仓库:++```sh+bun tests/fixtures/note-import/generate-large.mjs /tmp/edgeever-note-import-large+```++## 复现受影响检查++```sh+bun install --frozen-lockfile+bun test tests/note-import.integration.test.mjs packages/shared/src/image-link.test.mjs packages/shared/src/content.test.mjs+bun run typecheck+bun run typecheck:mobile+bun run build:web+```++截图、请求抓包、安装信息和一次性脚本不纳入产品仓库;本目录仅保留验收结论。++## 图片列表与特殊标签++生产 Web 通过设置入口导入含不配对方括号、换行和字面 `©` 的测试 ENEX,编辑、保存和重开后标签与图片保持正确,5 个资源字节与独立原件一致,PDF 可展开阅读。上传文件名继续遵循既有 API 规范化;可见标签中的换行显示为空格。++无序、有序、任务及嵌套列表经过 Markdown/富文本编辑、保存和重开,4 张图片仍位于对应列表项,外层链接、起始序号及勾选状态保留。真实来源、共享内容、相关图片/编辑器与生命周期检查、两项类型检查、Web 构建和目标 lint 通过。依赖保持锁定的 `marked@17`,无需升级解析器。++ENML 使用 Turndown 的星号斜体分隔选项,保留相邻中文之间的斜体;真实 Evernote 导出的 API JSON 和 Web 页面均验证通过。++## 任务状态与清理++本地空间在目录创建前失败时不写远端;创建后映射保存失败仍保留待确认状态,不重复建目录。确认后的 RAW Markdown 附件不全文扫描成笔记,无描述的资源目录笔记仍可导入。ENEX 同篇重复资源按篇清理,加密 ZIP 无法读取及初始化取消均及时结束;合法原型属性名附件上传和重试保持原字节、无重复。diff --git a/docs/third-party-import-plan.md b/docs/third-party-import-plan.mdnew file mode 100644index 00000000..e78cec59--- /dev/null+++ b/docs/third-party-import-plan.md@@ -0,0 +1,143 @@+# Third-party note import implementation plan++[简体中文](third-party-import-plan.zh-CN.md)++Related: [Issue #657](https://github.com/tianma-if/edgeever/issues/657). Implemented contract; [acceptance scope and results](changes/evidence/2026-10-27-third-party-import/README.md).++## 1. Approach and first-release boundaries++Add one “Import from other apps” entry under Settings → Import & Export. All three sources share file access, preflight, destination selection, execution, and results, with separate format parsers. Users do not configure MCP, API keys, or command-line tools.++The first release covers **Web and Electron desktop, importing online into the current instance and account**. Local scanning works without a connection; confirmed writes require the instance. Desktop pauses background sync during writes, then resumes and pulls the results. Android and iOS receive imported notes through existing sync; native file-import entry points are outside this release.++Imports are additive: create source folders and notes without name-based merging or overwriting existing content. A selected parent notebook changes only where new folders are attached. Each new import creates a new set of data. Retries within one task reuse its created notes; automatic merging across separate imports and resume after closing the app are not included.++All three sources and all file, directory, and archive forms requested by the Issue remain in the first release; implementation stages do not reduce the final deliverable.++## 2. Reuse and architecture choices++| Location | Reusable capability | Integration |+| --- | --- | --- |+| `apps/web/src/components/SettingsPane.tsx` | Web uses `DataExportCard`; desktop uses `DesktopScheduledBackupCard` | Keep both backup entries; replace the guide card with third-party import and retain its guide link |+| `packages/shared/src/backup-archive.ts` | Uses `@zip.js/zip.js` 2.24.0 for ZIP64, entry reads, and streaming restore | Reuse the dependency and reading approach without requiring an EdgeEver manifest or changing the backup format |+| `apps/api/src/memo-service.ts::importMemosRecord` | Batches of 1–25 notes, per-item results, imported timestamps, and `source + externalId` idempotency through the database adapter | Add a thin REST entry that reuses this service; retain existing MCP semantics |+| `packages/client/src/index.ts` | Creation accepts timestamps; resources support multipart uploads; document updates require edit sessions and version protection | Add import methods and the optional update timestamp fields already supported by the server |+| `apps/web/src/lib/desktop-sync.ts` | `pauseDesktopSyncForImport()` waits for active sync and returns a release function | Hold it after confirmation, release on every exit path, and refresh only after releasing |+| `crates/desktop-sidecar/src/memo.rs::create_memo` | Ordinary local creation uses database-generated times rather than imported timestamp parameters | Do not route this import through ordinary local creation or extend Rust storage and offline queue protocols |+| `scripts/import-evernote-enex-via-mcp.mjs` | ENML conversion, resource matching, and metadata mappings | Reuse format knowledge, not whole-file reads, last-parent-only paths, 400,000-character truncation, or automatic image compression |++Use **client-side parsing with shared server-side writes**, rather than separate offline import implementations for Web and desktop. Cloudflare and Docker continue running the same API business logic. The original archive is not uploaded wholesale.++## 3. Input, metadata, and link rules++### Evernote++- Accept one or multiple `.enex` files, nested directories containing ENEX, and ZIPs containing them. Preserve the full directory path; each ENEX filename becomes a leaf notebook. Multiple selected files without relative paths become sibling notebooks. Do not infer source hierarchy absent from the export.+- For `.notes`, explicitly ask users to export `.enex` from Evernote and try again; do not guess that it is XML or ZIP.+- Scan XML in chunks for titles, tags, `created`, `updated`, ENML, and resource descriptors. Import one note at a time, decoding resource base64 incrementally. A SAX text callback must not rebuild an entire attachment in memory.+- Use Turndown for ENML conversion, covering tables, tasks, images, and attachments. Incremental MD5 is used only to match ENEX `en-media` identifiers, not as a security check.+- Upload resources attached to a note even when its body does not reference them, appending attachment links. Retain encrypted source content as plain text with a notice rather than discarding it behind a placeholder.++### Obsidian Vault++- Accept a directory or ZIP and reconstruct folders from Markdown paths. Ignore `.obsidian`, `.git`, `.trash`, and OS index files. Do not execute plugins or scripts.+- Reuse Front Matter parsing and valid `title` / `tags` mappings. Add import-specific timestamp mappings for `created` / `createdAt` and `updated` / `updatedAt`; preserve other properties and original Front Matter. Conflicting synonyms are reported at preflight rather than arbitrarily resolved.+- Resolve ordinary Markdown local image/attachment links and `![[image-or-attachment]]`. Follow Vault path rules for Unicode, spaces, relative paths, and root-relative paths, staying within the selected Vault. Report ambiguous duplicate names rather than guessing. [Obsidian link rules](https://obsidian.md/help/links)+- Do not translate inter-note wikilinks, block references, note embeds, Canvas, or Dataview in this release. Retain source text and warn about references that will not work. Image and attachment embeds remain required resource migration, not exclusions.++### Joplin++- Explicitly support RAW export directories, Markdown export directories with or without Front Matter, ZIPs containing those exports, and `.jex`. Detect entry structure rather than relying solely on extensions.+- JEX is TAR; RAW is the equivalent directory representation. Parse notebook parent relationships, notes, tag associations, and resources before resolving body resource IDs. Not every `.md` entry represents a user note. [Joplin export documentation](https://joplinapp.org/help/apps/import_export/#exporting)+- A resource-folder/32-character filename is only a resource candidate. Confirm Markdown payloads through a valid RAW resource descriptor from another entry before reading them as text; unconfirmed Markdown remains eligible as a note.+- Markdown exports use paths and available metadata. Times and tags absent from an export cannot be reconstructed; preflight distinguishes absent source data from parsing failures.++### Shared rules++- Preserve valid source instants as timezone-qualified ISO timestamps. Retain and warn about missing or timezone-ambiguous values rather than presenting invented times as original. Omitted fields retain the existing creation API default of the import time.+- Whether `_resources`, `assets`, or similar directories become notebooks depends on whether they contain notes or ancestors of notes—not their name alone. Resource-only directories do not become empty notebooks.+- Repeated references to one attachment in one note create one upload. Across notes, upload separately under existing resource ownership rules. Preflight attachment counts mean planned EdgeEver resources, not unique source files. Resources unrelated to any note are listed as not imported.+- Rewrite actual image/link targets, not code blocks, inline code, or matching plain text. Preserve external HTTP(S) links without downloading remote images.+- Normalized paths must stay within the selected root. Do not follow archive symlinks or extract archives to arbitrary user directories. Report multiple entries mapping to one normalized path as ambiguous.+- Convert only static HTML content; do not execute scripts or event handlers. XML processing does not fetch DTDs or resolve external entities, while still accepting ordinary ENEX format declarations.+- Retain existing limits: notebook names of 80 characters, note titles of 160, images up to 100 MiB, and other attachments up to 1 GiB. Normalize tags in source order, remove empty/duplicate values and keep the first 24. Ignore extras with a non-blocking preflight warning showing the ignored count; ordinary editing retains its existing tag persistence rules. Preflight reports violations; it does not truncate text/titles or compress images to claim success. Errors preventing faithful import must be fixed first; warnings such as absent optional metadata may be acknowledged.++## 4. Modules and memory model++Add `packages/shared/src/note-import/` for platform-neutral source descriptors, directory plans, parsers, reference resolution, and result types. Export it through a subpath so the shared root entry does not pull in parsing dependencies. Module responsibilities:++- `types.ts`: `ImportFile`, `ImportNote`, `ImportSummary`, resource descriptors, and phase-specific results.+- `sources.ts`: a common directory/ZIP/TAR reader returning entry metadata and lazily opened byte streams.+- `session.ts` and `metadata.ts`: format scanning, folder/resource mapping, metadata and per-note reading; `evernote.ts` handles streaming ENEX. Inject ENML conversion so the Worker does not depend on a DOM.+- `references.ts`: parse actual image/link positions and rewrite target URLs precisely.++Add `apps/web/src/lib/note-import/` for file selection, Web Workers, temporary storage, execution, and task state. Web and Electron renderers share it. File and directory inputs preserve relative paths; the first release does not introduce Electron IPC accepting arbitrary filesystem paths.++The task index stores the import ID, source format, folders, note metadata, resource descriptors, issues, and reading locators—not the library's bodies or attachment base64. Page large indexes into task-scoped IndexedDB and read previews by page. Completed results store only note location/version/timestamps and resource mappings; keep intended commit content only while the current note is incomplete.++Run scanning and decompression in a Worker. Reuse `@zip.js/zip.js`; prefer `sax` for XML and async-iterable `it-tar` for TAR, validating browser builds and large-field behavior before pinning versions and licenses. Do not use archived `saxes` or write custom XML/TAR codecs. [SAX](https://github.com/isaacs/sax-js) · [it-tar](https://github.com/alanshaw/it-tar)++The first ENEX pass scans metadata and resource sizes. After confirmation, a second pass processes each file sequentially rather than rescanning it for every note. ZIP/TAR readers honor backpressure rather than extracting all entries at once. Stage a note's decoded resources in chunks in app-private storage and connect them to the existing `uploadMemoResourceParts` `readPart` interface. Perform DOM conversion one note at a time, yielding to the event loop.++Process only one note's body and attachments at a time, releasing its bytes afterward. Insufficient temporary space stops the task explicitly rather than falling back to whole-archive memory buffering. Clean decoded resource chunks on completion or cancellation. Retain the indexes and ID mappings needed for incomplete results until those results close, allowing task-local retries to reread source files on demand. Opening a result note hides the dialog and retains this task for retry; explicit result dismissal or selecting another export clears its indexes. Never modify source directories.++## 5. Writes and failure handling++### API additions++Add `apps/api/src/import-routes.ts` with:++- `GET /api/v1/imports/capabilities`: protocol version and the existing batch limit, checked before any writes. An unsupported older server prompts an upgrade for this entry only; existing editing, sync, and backups remain usable.+- `POST /api/v1/imports/memos`: accepts `importId`, format, destination notebook, items, and `dryRun`, validates them, and calls `importMemosRecord`. The server constructs `third-party-<format>-<importId>` as the source; each note uses a fixed preflight item ID, respecting existing source/external-ID limits of 80/512 characters. Return per-item created, skipped, and failed outcomes.++Keep session authentication, workspace isolation, and existing note read/write permissions. Do not require MCP tokens or introduce authorization scopes. Use existing APIs for notebook creation, attachments, edit sessions, and document updates. Add corresponding client methods and optional `createdAt` / `updatedAt` update fields; existing callers gain no required arguments. Import needs no persistent import-task table. The companion large-library export repair adds the `0085` memo index described in the [change scope](changes/2026-10-27-third-party-import.md#large-library-reads-and-exports).++### Execution order++1. Select the source format and files. After scanning, show the target instance/account, planned notebook/note/resource counts, the folder tree, and issues. Scanning and dismissing preflight do not write remotely.+2. Choose top-level notebooks or an existing parent. Read destinations from the server; locally pending parents become selectable after ordinary sync. Show the actual new tree without imposing an extra source wrapper. Create no notebooks before confirmation.+3. On confirmation, fix the instance, account, workspace, destination, and request context, then verify API availability. Desktop obtains the sync pause handle and waits for in-flight sync before creating notebooks in parent-first order. Persist a pending-confirmation marker before creating each folder, then retain its source-to-target ID mapping. If local mapping persistence fails after the server creates the folder, keep it unconfirmed rather than blindly creating again.+4. Create notes by destination one at a time, using batches of one within the server's existing 1–25-item contract without imposing a new limit. Complete the next step before processing another note. Retain source body and times, recording the returned ID, `revision`, and `contentHash` as the import baseline. `skipped` means creation already occurred, not that attachments and the final document are complete.+5. Upload resources note by note and retain source-resource-to-URL mappings. When required resources are ready, obtain an edit session, verify that current revision/content still match the task's saved import baseline, and submit protected rewritten content with source or initial creation timestamps. A fresh session must not authorize overwriting intervening user edits; report any baseline mismatch as a conflict. Resource uploads and rewriting must not turn the original modification time into the migration time. Advance the task baseline after a successful commit.+6. Count a note as successful only after its body, required resources, and final commit are complete. Release the desktop pause handle before refreshing the workspace: existing sync skips work while paused. If import succeeded but local pulling failed, separately show “imported” and “sync pending”; do not import again.++### Failure, cancellation, and retries++- A failed attachment or final commit retains the created note and task-local mappings as incomplete, allowing retries within this task. Retry missing steps without creating another note. Results link to incomplete notes and identify failed attachments instead of showing only an aggregate success count.+- Add no blind retries for batch POSTs, notebook creation, or final document updates. Resolve/replay uncertain note creation with its fixed import key. If uncertain notebook creation or attachment completion cannot be reconciled to an owned result, stop and report rather than assuming failure and creating again.+- Preserve the existing multipart PUT retry behavior. A new task does not scan existing notes for title-based duplicates or pretend to resume an earlier run.+- Cancellation stops new writes and aborts cancellable reads/uploads, retaining completed results and explicitly listed incomplete items. It does not delete existing data or promise a whole-library transaction rollback. Report submitted requests without known outcomes as “result pending confirmation.”+- Logout or account/instance changes cancel the old task; late results cannot update a different workspace. Run one foreground import per workspace, with a workspace-level controller surviving settings-card unmounts. Fully exiting the app ends execution.+- Release the sync pause in `finally` on every termination path. Keep pre-existing local edits in their original queue; do not clear, overwrite, or forcibly migrate them for import.++## 6. Show only what the operation needs++Add `ThirdPartyImportCard` and a shared `ThirdPartyImportDialog`, using existing shadcn/ui Dialog, selection, progress, and Tooltip components. The entry shows source selection, file/folder selection, and the migration-guide link.++Preflight emphasizes three counts, destination, and the folder tree. Expand warnings beside affected files/notes and make blocking errors locatable. During execution, show the phase, current file, and completed/total counts; do not fabricate percentages while scan totals are unknown. Separate successful, incomplete, cancelled, and unconfirmed results, with actions to open the destination and retry failed steps.++Update existing locale catalogs for import text. Retain other settings groups, backup/encrypted restore, single-file Markdown drag import, and the command-line guide's existing usage.++## 7. Acceptance++Acceptance follows real user flows:++- Operate every source from Settings. Preflight counts match actual new data; cancelling preflight leaves databases and object storage unchanged. Use lawful sanitized exports or exports created for testing, not personal libraries.+- Run the same core flows through Cloudflare local D1/R2 and Docker SQLite/object-storage adapters. Read back full paths, titles, tags, original creation/modification times, and resource associations. Create existing data in the baseline version, upgrade to the version with the new API, then import and confirm existing data and old editing/backup entries retain their contracts.+- Reopen imported notes after making source directories unavailable; images and attachments must load from EdgeEver resources. Check actual content and link positions, not merely HTTP 200. Reuse applicable evidence for unchanged guide, EdgeEver ZIP v1/v2, and encrypted desktop restore behavior.+- Use a first-note-success, second-note-resource-failure, third-note-not-started flow. Check accurate results, task-local retries without duplicate first/second notes, and no overwrite after concurrent edits. Desktop sync must resume after success, failure, and cancellation; local timestamp readback must match the server after refresh.+- Fix note counts and body sizes, then compare multi-attachment archives totaling about 1 GiB and 4 GiB, keeping each file within current upload limits. Record browser/desktop versions, peak heap/RSS, active buffers, and temporary disk use. Memory must not grow roughly in proportion to total attachment bytes; the UI must remain cancellable without whole-archive memory copies. Separately cover a single large ENEX base64 field, not only Vaults containing many small files.+- Compare real Worker/IndexedDB preflight as note count grows, and import multiple large bodies through the real API. Result pages must retain baselines without full Markdown/JSON/text, while retries and concurrent-edit protection still work.+- Cover `.notes`, missing resources, ambiguous references, root escapes, resource-only directories, absent metadata, and existing limits; check feedback and final counts. Add isolated parsing tests only for distinctions not effectively covered end to end.++Required checks are `bun run typecheck`, `bun run typecheck:mobile` and `bun run build:web`, alongside the affected actual flows. Any later Android entry requires USB-device acceptance.++## 8. Risks, rollback, and remaining validation++This is a high-risk data-writing feature. Worst outcomes include partially created folders/notes with incomplete resources, duplicate imports, or incorrect timestamps/link conversions. Write only to newly created objects, retain source files and per-item outcomes, avoid overwriting existing notes, and protect final writes with edit sessions. Import changes the workspace backup revision under existing rules; automatic backups may retry after source changes. Do not alter scheduling or weaken backup consistency checks.++Feature rollback can remove the new entry and REST routes without changing old editing, sync, or backup protocols; imported data remains ordinary usable notes. Code rollback does not undo data already written. Any cleanup must be confirmed using actual IDs recorded for that import, not bulk deletion by name.++The [acceptance conclusions](changes/evidence/2026-10-27-third-party-import/README.md) distinguish the current Web/API candidate, earlier macOS/Linux native flows, existing-instance Cloudflare upgrades and large-library measurements. Real Evernote 11.40.2 ENEX, Obsidian 1.14.5 Vault and Joplin 3.7.21 RAW/JEX/both Markdown exports preserve declared bodies, hierarchy, source timestamps and original resource bytes.++R27–R30 and the `0085` index are deployed to the existing Cloudflare instance. R31–R38 are verified locally and are not deployed; earlier native acceptance does not cover these latest repairs. Windows native, physical mobile devices, older-client editing, AppImage installation/cross-version updates and Docker container deployment remain unverified. Fresh password login and saving a Web ZIP to the local filesystem were not covered by the latest Cloudflare acceptance; they require checks through those entries when that deployment is accepted.diff --git a/docs/third-party-import-plan.zh-CN.md b/docs/third-party-import-plan.zh-CN.mdnew file mode 100644index 00000000..a113a84a--- /dev/null+++ b/docs/third-party-import-plan.zh-CN.md@@ -0,0 +1,143 @@+# 第三方笔记导入实现方案++[English](third-party-import-plan.md)++关联:[Issue #657](https://github.com/tianma-if/edgeever/issues/657)。本文为已实现契约;[验收范围与结论](changes/evidence/2026-10-27-third-party-import/README.zh-CN.md)。++## 1. 实现方向与首版边界++在“设置 → 导入导出”新增统一的“从其他应用导入”入口。三种来源共用文件读取、预检、目标目录选择、执行和结果界面,各自负责解析来源格式。用户无需配置 MCP、API Key 或命令行。++首版覆盖 **Web 与 Electron 桌面端,联网导入到当前实例、当前账号**。本地扫描不依赖网络,确认写入时需要连接实例。桌面端在写入阶段暂停后台同步,结束后恢复并拉取结果。Android 和 iOS 继续通过现有同步读取导入结果,本期不增加原生文件导入入口。++导入使用追加语义:新建源目录和笔记,不按名称合并、覆盖既有内容。指定父笔记本只改变新增目录的挂载位置,不修改父笔记本。一次新的导入创建一批新数据;同一任务内的重试复用已创建的笔记,不提供跨次自动合并或关闭应用后的断点续传。++三种来源及 Issue 要求的文件、目录和压缩包形式都保留在首版,不以分步开发缩减最终交付范围。++## 2. 复用与架构选择++| 位置 | 可复用能力 | 接入方式 |+| --- | --- | --- |+| `apps/web/src/components/SettingsPane.tsx` | Web 使用 `DataExportCard`,桌面使用 `DesktopScheduledBackupCard` | 保留两个备份入口,以第三方导入卡替换指南卡,卡内保留指南链接 |+| `packages/shared/src/backup-archive.ts` | 使用 `@zip.js/zip.js` 2.24.0,支持 ZIP64、按条目读取与流式恢复 | 复用依赖及读取方式,不要求第三方 ZIP 携带 EdgeEver manifest,不改备份格式 |+| `apps/api/src/memo-service.ts::importMemosRecord` | 每批 1–25 篇、逐项结果、时间戳导入、`source + externalId` 幂等;通过数据库适配层写入 | 增加薄 REST 入口复用该服务,不另写一套创建笔记逻辑,不改变 MCP 的既有语义 |+| `packages/client/src/index.ts` | 普通创建可传时间戳;资源支持分片上传;正文更新需要编辑会话与版本保护 | 增加导入方法,并为 `updateMemo` 类型补齐服务端已支持的可选时间字段 |+| `apps/web/src/lib/desktop-sync.ts` | `pauseDesktopSyncForImport()` 等待在途同步并返回释放函数 | 在确认写入后持有,所有退出路径释放;先释放,再刷新工作区 |+| `crates/desktop-sidecar/src/memo.rs::create_memo` | 普通本地创建依赖数据库生成时间,不读取导入时间字段 | 不把本次导入接到普通本地创建,避免扩展 Rust 存储和离线队列协议 |+| `scripts/import-evernote-enex-via-mcp.mjs` | 已有 ENML 转换、附件关联和元数据映射 | 参考格式规则;不沿用整文件读取、仅取末级父目录、400,000 字符截断及自动压图 |++因此,采用**客户端解析、共用服务端写入**,而不是让 Web、桌面各实现一套离线导入。Cloudflare 与 Docker 继续执行相同的 API 业务代码,原始归档不整包上传到服务器。++## 3. 输入、元数据与链接规则++### Evernote / 印象笔记++- 接受单个或多个 `.enex`、包含 ENEX 的多级目录,以及这些内容的 ZIP。目录路径完整保留,每个 ENEX 文件名对应一个叶级笔记本;多选且没有相对路径的文件作为同级笔记本。不推测导出中不存在的原平台目录。+- `.notes` 明确提示“请从印象笔记导出为 .enex 后重试”,不作为 XML 或 ZIP 猜测解析。+- 分块扫描 XML,读取标题、标签、`created`、`updated`、ENML 和内嵌资源描述。导入时逐篇处理,资源 base64 分块解码;不能在 SAX 的单个文本事件里重新积累整个附件。+- ENML 转换沿用 Turndown,覆盖表格、待办、图片和附件。MD5 只用于匹配 ENEX 的 `en-media` 资源标识,采用增量实现;不把它当作安全校验。+- 笔记自带但未在正文中引用的附件仍上传,并在正文末尾补充附件链接。加密段落以纯文本保留原始加密内容及提示,不用占位文案替换后丢弃原文。++### Obsidian Vault++- 接受目录或 ZIP,按 Markdown 所在路径还原目录;忽略 `.obsidian`、`.git`、`.trash` 及系统索引文件,不执行插件或脚本。+- 复用 Front Matter 读取能力,映射有效的 `title`、`tags`;导入解析器增加 `created` / `createdAt`、`updated` / `updatedAt` 时间映射,保留其他属性与原始 Front Matter。两个同义字段冲突时预检列出冲突,不任取一个。+- 处理标准 Markdown 本地图片/附件和 `![[图片或附件]]`。路径按 Vault 规则解析,包括中文、空格、相对路径和根相对路径;仅能在所选 Vault 中解析。重名且无法唯一定位时列出问题,不猜测。[Obsidian 链接规则](https://obsidian.md/help/links)+- 本次不转换笔记间双链、块引用、笔记嵌入、Canvas 或 Dataview;保留来源文本,对无法继续使用的引用给出预检提示。图片/附件嵌入属于必须完成的资源迁移,不归入这些排除项。++### Joplin++- 明确支持 RAW 导出目录、Markdown 导出目录(含有/无 Front Matter 两种)及其 ZIP,另支持 `.jex`。按条目结构识别,而不是仅按扩展名识别。+- JEX 是 TAR;RAW 是相同数据的目录形式。读取笔记本父子关系、笔记、标签关联及资源记录,再处理正文中的资源 ID 引用;不能把每个 `.md` 条目都当作用户笔记。[Joplin 导出说明](https://joplinapp.org/help/apps/import_export/#exporting)+- 资源目录或 32 位文件名只表示资源候选;Markdown 须由另一个源条目的有效 RAW 资源描述确认后才按附件跳过全文读取,未确认的 Markdown 仍可作为笔记。+- Markdown 导出按路径和存在的元数据处理。源文件没有导出的时间和标签无法还原,预检必须区分“来源未提供”和“解析失败”。++### 共用规则++- 有效来源时间保留其时刻,写入为带时区的 ISO 时间。缺失或无法确定时区的时间保留原始值并提示;不冒充原始笔记时间,缺失字段沿用现有创建接口的导入时刻默认值。+- `_resources`、`assets` 等目录是否建笔记本取决于是否包含笔记或其子目录是否含笔记,不能只按名称整目录排除。纯资源目录不建空笔记本。+- 同一附件在同一篇笔记中被多次引用只上传一次;跨笔记沿用现有资源归属模型分别上传。预检的“附件数”指计划创建的 EdgeEver 资源数,不是唯一源文件数;未关联到任何笔记的资源列为未导入项。+- 只改写正文中实际的图片/链接目标,不对代码块、行内代码或相似文字做全局替换。外部 HTTP(S) 链接保留,不下载远程图片。+- 路径规范化后不得越出所选根目录;不跟随归档中的符号链接,不把归档解压到用户任意目录。同一规范路径对应多个条目时报告歧义。+- HTML 仅转换静态内容,不执行脚本或事件处理器;XML 不联网读取 DTD 或解析外部实体,正常 ENEX 的格式声明仍可读取。+- 沿用当前名称、标题和上传限制,包括笔记本名 80 字符、标题 160 字符、图片 100 MiB、其他附件 1 GiB。标签按来源顺序去空、去重后保留前 24 个;超出部分忽略,预检提示忽略数量但不阻止导入。普通编辑的标签保存规则不变。预检列出不符合项,不截断正文、标题或自动压图来“导入成功”。影响完整还原的错误须先修正,缺失可选元数据等警告可确认后继续。++## 4. 模块与内存模型++新增 `packages/shared/src/note-import/`,保存平台无关的来源描述、目录计划、解析器、引用解析和结果类型。按子路径导出,不从共享包总入口拉入解析依赖。模块职责如下:++- `types.ts`:`ImportFile`、`ImportNote`、`ImportSummary`、资源描述和分阶段结果。+- `sources.ts`:目录、ZIP、TAR 的统一读取接口,返回条目元数据及按需打开的字节流。+- `session.ts`、`metadata.ts`:来源格式扫描、目录/资源映射、元数据及逐篇读取;`evernote.ts` 专门处理流式 ENEX。ENML 转换通过注入的转换函数完成,不让 Worker 依赖 DOM。+- `references.ts`:解析实际图片/链接位置并精确改写目标 URL。++Web 端新增 `apps/web/src/lib/note-import/` 负责文件选择、Web Worker、临时存储、执行器与任务状态;Web 和 Electron 渲染器共用它。使用目录输入和文件输入保留相对路径,不为首版新增接受任意路径的 Electron IPC。++任务索引只记录导入 ID、来源类型、目录、笔记元数据、资源描述、问题清单及读取定位,不保存全库正文或附件 base64。大索引按页存入任务级 IndexedDB,界面按页读取预览。已完成结果只保存笔记位置、版本、时间及资源映射;仅当前未完成项保留核对提交所需的目标正文。++扫描与解压在 Worker 执行。ZIP 沿用 `@zip.js/zip.js`;XML 首选 `sax`,TAR 首选基于异步迭代器的 `it-tar`,实施时先验证浏览器构建和大字段行为,再锁定版本与许可证。不采用已归档的 `saxes`,不自行实现 XML/TAR 编解码。[SAX](https://github.com/isaacs/sax-js) · [it-tar](https://github.com/alanshaw/it-tar)++ENEX 第一遍只扫描元数据和资源大小,确认后第二遍按文件顺序处理,避免为每篇笔记重新扫描整个 ENEX。ZIP/TAR 读取遵守背压,不一次解压全部条目。单篇所需的解码资源按块暂存到应用私有存储,并接入已有 `uploadMemoResourceParts` 的 `readPart` 接口;DOM 转换逐篇执行并让出事件循环。++同时只处理一篇笔记的正文和附件,完成后释放其字节。临时空间不足时明确停止,不能降级成全量内存缓存。完成或取消后清理解码资源块;未完成结果的必要索引和 ID 映射保留到结果关闭,以便当前任务重试时按需重读来源文件。打开结果笔记仅隐藏弹窗、保留当前任务以供重试;显式关闭结果或改选来源才清理任务索引。不触碰源目录。++## 5. 写入流程与错误处理++### 接口增量++在 `apps/api/src/import-routes.ts` 增加:++- `GET /api/v1/imports/capabilities`:返回协议版本及现有批次上限;开始写入前检测支持情况。旧服务端不支持时仅提示升级服务端,原编辑、同步和备份功能保持可用。+- `POST /api/v1/imports/memos`:接受 `importId`、来源类型、目标笔记本、条目和 `dryRun`,校验后调用现有 `importMemosRecord`。由服务端构造 `third-party-<format>-<importId>` 来源标识;每篇使用预检时固定的条目 ID,符合既有 80/512 字符来源键限制。返回逐项创建、跳过和失败结果。++沿用会话认证、工作区隔离及笔记读写权限,不要求客户端生成 MCP Token,不新增授权范围。普通笔记本创建、附件上传、编辑会话和正文更新继续使用现有 API。`packages/client/src/index.ts` 增加对应方法及可选 `createdAt` / `updatedAt` 更新字段,既有调用不新增必填参数。导入不需要持久化任务表;配套大库导出修复新增 `0085` 笔记索引,见[变更清单](changes/2026-10-27-third-party-import.zh-CN.md#大库读取与导出)。++### 执行顺序++1. 选择来源类型和文件,扫描完成后显示目标实例/账号、计划新增的笔记本/笔记/附件数、目录树及问题;扫描和取消预检都不写远端。+2. 用户选择“创建为顶层笔记本”或已有父笔记本。目标从服务端目录读取,本地尚未同步的父笔记本需正常同步后才能选择。显示实际将新增的树,不额外强加来源包装层;确认前不创建笔记本。+3. 确认时固定实例、账号、工作区、目标和请求上下文;检查新 API 可用。桌面取得同步暂停句柄并等待在途同步结束,随后按父子顺序调用现有 API 创建目录,每个目录创建前先持久化待确认标记,再保存源目录到目标 ID 的映射;服务端已创建但本地映射保存失败时保持待确认,不盲目重复创建。+4. 按目标笔记本逐篇创建笔记,每次 1 篇,沿用服务端既有 1–25 篇批次契约,不新增限制;完成下一步后再处理下一篇。保留来源正文和时间,记录返回的真实 ID、`revision` 和 `contentHash` 作为导入基线;`skipped` 只代表该项已创建,不代表附件和最终正文已经完成。+5. 逐篇上传附件,保存源资源到远端 URL 的映射。全部必要附件就绪后取得编辑会话,先核对当前版本和内容仍匹配该任务保存的导入基线,再受保护地提交改写正文,同时带回来源或首次创建时的时间戳。取得新会话不能用于接纳并覆盖中途人工编辑;不匹配即报告冲突。上传和改写不能把原修改时间变成迁移时间,成功提交后更新任务基线。+6. 只有正文、所需附件和最终提交都完成才计为成功。最后释放桌面暂停句柄,再调用工作区刷新;刷新在暂停期间会被现有同步函数跳过,不能颠倒顺序。导入已完成但本地拉取失败时分别显示“已导入”“同步待重试”,不重新导入。++### 失败、取消与重试++- 单篇附件或最终提交失败时保留已写入的笔记及任务内映射,标为未完成,允许当前任务重试;重试只补缺失步骤,不再创建另一篇。结果列出可打开的未完成笔记与具体失败附件,不能只显示总成功数。+- 不增加整批 POST、目录创建或最终正文更新的盲重试。笔记创建结果不明时可用同一导入键查明/重放;目录创建或附件提交结果不明且无法确认归属时停止该步骤并报告,不假定失败、再次创建。+- 分片 PUT 及其重试沿用现有上传客户端。新任务不扫描既有知识库按标题猜测重复,不将一次新导入伪装为上次任务续传。+- 取消停止新写入并终止可取消的读取/上传,保留已完成结果和明确列出的未完成项;不自动删除既有数据,也不承诺整库事务回滚。请求已提交但响应未收到的项目单独显示“结果待确认”。+- 退出登录、切换账号或实例时取消旧任务,迟到结果不能更新新工作区。当前工作区一次运行一个前台导入任务,状态由工作区级控制器持有,不随设置卡片卸载而丢失;完全退出应用后不继续执行。+- 所有终止路径在 `finally` 中释放同步暂停。已有本地待同步编辑保持原队列,不为导入清空、覆盖或强行迁移它们。++## 6. 界面只展示完成操作所需内容++新建 `ThirdPartyImportCard` 和共用 `ThirdPartyImportDialog`,复用 shadcn/ui 的 Dialog、选择器、进度和 Tooltip。入口只显示来源选择、选择文件/文件夹与迁移指南链接。++预检弹窗的重点是三个数量、目标位置和目录树;警告在对应文件/笔记旁展开,影响导入的错误集中可定位。执行期间展示当前阶段、当前文件及已完成/总数,扫描总量未知时不伪造百分比。结果区分成功、未完成、取消和待确认,并提供打开目标目录及重试失败步骤。++导入文案更新项目现有语言目录,不改变设置页其他分组、备份/加密恢复、单文件 Markdown 拖入和原命令行指南的使用方式。++## 7. 验收++关键验收围绕实际流程:++- 每种来源均从设置入口操作,预检数量与实际新增数据一致;取消预检后数据库和资源存储无新增。使用合法的脱敏导出或自行创建的真实导出,不使用个人知识库。+- 在 Cloudflare 本地 D1/R2 与 Docker SQLite/对象存储适配器上跑同一组核心流程,读回完整路径、标题、标签、创建/修改时间和资源关联。先在当前版本实例创建既有数据,再升级到新增 API 版本并导入,确认既有数据及旧编辑/备份入口仍按原契约工作。+- 关闭源目录访问后重开导入笔记,图片和附件从 EdgeEver 资源地址读取;校验实际内容与引用位置,而不只确认 HTTP 200。旧指南和 EdgeEver ZIP v1/v2、桌面加密恢复相关未改行为复用仍适用的既有证据。+- 构造首篇成功、第二篇附件失败、第三篇未开始的流程,确认结果准确;同一任务重试不重复第一篇或第二篇;编辑冲突不覆盖人工修改。桌面成功、失败、取消后同步都能恢复,刷新后的本地时间与服务端一致。+- 固定笔记数量与正文规模,分别导入总附件约 1 GiB 和 4 GiB 的多附件归档,各单文件保持现有上传限制内。记录浏览器/桌面版本、峰值 heap/RSS、活动缓冲与临时磁盘占用;确认内存不随总附件体积近似同比增长,界面可取消、无整包内存副本。单个大 ENEX base64 字段另外覆盖,不能只验证由很多小文件组成的 Vault。+- 增长笔记数量,对比真实 Worker/IndexedDB 的预检表现,并通过真实 API 导入多篇大正文。结果分页只保留基线,不含完整 Markdown/JSON/摘要;同任务重试及人工编辑冲突保护仍成立。+- 覆盖 `.notes`、缺附件、重名引用、目录越界、纯资源目录、缺失元数据及现有限制,检查提示和最终计数。无法通过端到端有效区分的解析细节再补隔离测试。++必要检查为 `bun run typecheck`、`bun run typecheck:mobile`、`bun run build:web` 及受影响的实际流程。若后续扩大到 Android,必须使用 USB 真机验收。++## 8. 风险、回退与待验证项++这是高风险的数据写入功能。最坏结果是部分目录/笔记已创建但附件未完成、重复导入,或时间/链接转换错误;因此只向新建对象写入、保留源文件、记录逐项结果、不覆盖既有笔记,并用编辑会话保护最终提交。导入过程中工作区备份版本会按既有机制变化,自动备份可能因数据变化重试;不为此修改备份调度或放宽一致性校验。++功能回退可移除新增入口和 REST 路由,旧编辑、同步、备份协议不变;已导入数据仍按普通笔记可用。代码回退不会撤销已写入数据。需要清理结果时按本次记录的实际 ID 人工确认,不按名称批量删除。++[验收结论](changes/evidence/2026-10-27-third-party-import/README.zh-CN.md)区分当前 Web/API 候选、此前 macOS/Linux 原生流程、Cloudflare 旧实例升级和大库测量。Evernote 11.40.2 原始 ENEX、Obsidian 1.14.5 Vault 及 Joplin 3.7.21 RAW/JEX/两种 Markdown 导出均按声明保留正文、层级、原始时间及资源字节。++R27–R30 和 `0085` 索引已部署到既有 Cloudflare 实例。R31–R38 已在本地验证、尚未部署;此前原生验收未覆盖最新修复。Windows 原生、移动端真机、旧客户端编辑、AppImage 安装/跨版本升级及 Docker 容器部署仍未验证。最近一次 Cloudflare 验收未覆盖密码重新登录和 Web ZIP 本机保存,接受该次部署时需通过对应入口检查。diff --git a/migrations/0085_export_cursor_index.sql b/migrations/0085_export_cursor_index.sqlnew file mode 100644index 00000000..e00e3afd--- /dev/null+++ b/migrations/0085_export_cursor_index.sql@@ -0,0 +1,3 @@+-- Preserve creation-time export order while seeking directly to continuation pages.+CREATE INDEX idx_memos_workspace_created_export+ ON memos(workspace_id, is_deleted, created_at, id);diff --git a/package.json b/package.jsonindex 4bb6a0b4..5db00662 100644--- a/package.json+++ b/package.json@@ -200,12 +200,15 @@"@types/node": "26.0.1","@types/react": "19.2.18","@types/react-dom": "19.2.3",+ "@types/sax": "1.2.7",+ "@types/spark-md5": "3.0.5","@types/turndown": "^5.0.6","@vitejs/plugin-react": "^6.1.2","concurrently": "10.0.3","esbuild": "0.28.1","fake-indexeddb": "^6.2.5","google-auth-library": "^11.0.2",+ "it-tar": "6.0.6","linkedom": "0.18.13","oxlint": "1.88.0","tailwindcss": "^4.3.2",diff --git a/packages/client/src/index.test.mjs b/packages/client/src/index.test.mjsindex e53c6927..3de23530 100644--- a/packages/client/src/index.test.mjs+++ b/packages/client/src/index.test.mjs@@ -8,6 +8,17 @@ const jsonResponse = (body, init = {}) => new Response(JSON.stringify(body), {});describe("EdgeEver client HTTP contract", () => {+ test("sends optional export cursors without changing legacy offset requests", async () => {+ const calls = [];+ const client = createEdgeEverClient({ baseUrl: "https://notes.example", fetch: async input => { calls.push(new URL(input)); return jsonResponse({}); } });+ const cursor = JSON.stringify({ id: "memo_续页", createdAt: "2020-01-01T00:00:00.000Z", offset: 25 });+ await client.getJsonBackupPage(25, 25, cursor);+ await client.getMarkdownExportPage(25, 25, ["memo_1"], cursor);+ expect(calls.map(url => url.searchParams.get("cursor"))).toEqual([cursor, cursor]);+ expect(calls.map(url => url.searchParams.get("includeTotal"))).toEqual(["0", "0"]);+ expect(calls[1].searchParams.get("ids")).toBe("memo_1");+ });+test("requests full metadata initially and opts out only for later sync and export pages", async () => {const calls = [];const client = createEdgeEverClient({ baseUrl: "https://notes.example", fetch: async (input) => { calls.push(new URL(input)); return jsonResponse({}); } });diff --git a/packages/client/src/index.ts b/packages/client/src/index.tsindex bd53393f..5b8962d2 100644--- a/packages/client/src/index.ts+++ b/packages/client/src/index.ts@@ -1,3 +1,4 @@+import type { ImportMemosInput, ImportMemosResponse } from "@edgeever/shared/note-import/types";import type { NotebookUpdateInput } from "@edgeever/shared";import {parsePreparedAiGeneration,@@ -109,6 +110,9 @@ export type MultipartResourceUploadSource = {mimeType: string;byteSize: number;readPart: (start: number, end: number) => Promise<Blob | Uint8Array<ArrayBuffer>>;+ /** Importers use this to distinguish an uncertain commit from a failed part. */+ onBeforeComplete?: () => void | Promise<void>;+ onPartUploaded?: (byteSize: number) => void;};export type InstanceHealth = {@@ -278,6 +282,7 @@ export type MarkdownExportPage = {resources: Resource[];totalCount?: number;nextOffset: number | null;+ nextCursor?: string | null;};export type JsonBackupPage = MarkdownExportPage & {@@ -530,6 +535,7 @@ export const createEdgeEverClient = (options: EdgeEverClientOptions = {}) => {body: chunk,},);+ source.onPartUploaded?.(Math.min(partNumber * upload.partSize, source.byteSize));break;} catch (error) {attempt += 1;@@ -542,6 +548,7 @@ export const createEdgeEverClient = (options: EdgeEverClientOptions = {}) => {}}}+ await source.onBeforeComplete?.();return await request<ResourceResponse>(`/api/v1/resource-uploads/${encodeURIComponent(upload.id)}/complete`,{ method: "POST", body: JSON.stringify({}) },@@ -1272,6 +1279,10 @@ export const createEdgeEverClient = (options: EdgeEverClientOptions = {}) => {return request<ListMemosResponse>(`/api/v1/memos?${search.toString()}`);},+ getImportCapabilities: () => request<{ protocolVersion: number; maxBatchSize: number; formats: string[]; importContext: string; accountId: string | null }>("/api/v1/imports/capabilities"),++ importMemos: (payload: ImportMemosInput) => request<ImportMemosResponse>("/api/v1/imports/memos", { method: "POST", body: JSON.stringify(payload) }),+createMemo: (payload: {notebookId: string;title?: string;@@ -1547,7 +1558,7 @@ export const createEdgeEverClient = (options: EdgeEverClientOptions = {}) => {method: "DELETE",}),- getMarkdownExportPage: (offset = 0, limit = 50, memoIds?: string[]) => {+ getMarkdownExportPage: (offset = 0, limit = 50, memoIds?: string[], cursor?: string) => {const search = new URLSearchParams({offset: String(offset),limit: String(limit),@@ -1556,11 +1567,16 @@ export const createEdgeEverClient = (options: EdgeEverClientOptions = {}) => {search.set("ids", memoIds.join(","));}if (offset > 0) search.set("includeTotal", "0");+ if (cursor !== undefined) search.set("cursor", cursor);return request<MarkdownExportPage>(`/api/v1/exports/markdown?${search.toString()}`);},- getJsonBackupPage: (offset = 0, limit = 25) =>- request<JsonBackupPage>(`/api/v1/backups/json?offset=${offset}&limit=${limit}${offset > 0 ? "&includeTotal=0" : ""}`),+ getJsonBackupPage: (offset = 0, limit = 25, cursor?: string) => {+ const search = new URLSearchParams({ offset: String(offset), limit: String(limit) });+ if (offset > 0) search.set("includeTotal", "0");+ if (cursor !== undefined) search.set("cursor", cursor);+ return request<JsonBackupPage>(`/api/v1/backups/json?${search}`);+ },restoreJsonNotebooks: (notebooks: JsonBackupNotebook[]) =>request<{ ok: true }>("/api/v1/restores/json/notebooks", {@@ -1702,6 +1718,8 @@ export const createEdgeEverClient = (options: EdgeEverClientOptions = {}) => {contentJson?: TiptapDoc;contentMarkdown?: string;tags?: string[];+ createdAt?: string;+ updatedAt?: string;allowDestructiveOverwrite?: boolean;}) =>diff --git a/packages/shared/package.json b/packages/shared/package.jsonindex f5ef0a03..959de69d 100644--- a/packages/shared/package.json+++ b/packages/shared/package.json@@ -45,7 +45,9 @@"./companion-discovery-context": "./src/companion-discovery-context.ts","./provider-event-stream": "./src/provider-event-stream.ts","./backup-archive": "./src/backup-archive.ts",- "./backup-markdown": "./src/backup-markdown.ts"+ "./backup-markdown": "./src/backup-markdown.ts",+ "./note-import": "./src/note-import/index.ts",+ "./note-import/types": "./src/note-import/types.ts"},"types": "./src/index.ts","dependencies": {@@ -61,9 +63,17 @@"@tiptap/pm": "3.31.4","@tiptap/starter-kit": "3.31.4","@zip.js/zip.js": "2.24.0",+ "it-tar": "6.0.6","js-base64": "3.9.3","katex": "0.18.2","marked": "17.0.6",+ "mdast-util-to-markdown": "2.1.2",+ "micromark": "4.0.2",+ "micromark-extension-gfm": "3.0.0",+ "micromark-util-decode-string": "2.0.1",+ "micromark-util-normalize-identifier": "2.0.1",+ "sax": "1.6.0",+ "spark-md5": "3.0.2","yaml": "2.8.3","zod": "4.6.6"}diff --git a/packages/shared/src/backup-archive.ts b/packages/shared/src/backup-archive.tsindex c2fb7c4f..1dfb4202 100644--- a/packages/shared/src/backup-archive.ts+++ b/packages/shared/src/backup-archive.ts@@ -39,6 +39,7 @@ type JsonBackupPage = {revisions: JsonBackupRevision[];totalCount?: number;nextOffset: number | null;+ nextCursor?: string | null;};export type JsonBackupSource = {@@ -46,7 +47,7 @@ export type JsonBackupSource = {onManifest?: (manifest: JsonBackupManifest) => void;resourceOrigin?: string;signal?: AbortSignal;- getPage: (offset: number, limit: number) => Promise<JsonBackupPage>;+ getPage: (offset: number, limit: number, cursor?: string) => Promise<JsonBackupPage>;getResourceResponse?: (resourceUrl: string) => Promise<Response>;getResourceBlob?: (resourceUrl: string) => Promise<Blob>;listPrompts: () => Promise<{ prompts: JsonBackupAiPrompt[] }>;@@ -127,13 +128,14 @@ export const createEdgeEverZipStream = async (await addJson("notebooks.json", backupNotebooks);await addJson("prompts.json", prompts);let offset = 0;+ let cursor: string | undefined;let completed = 0;let total = 0;let revisionCount = 0;let resourceCount = 0;while (true) {abort.signal.throwIfAborted();- const page = await source.getPage(offset, 25);+ const page = await source.getPage(offset, 25, cursor);total = page.totalCount ?? Math.max(total, completed + page.memos.length);onProgress?.({ completed, total });for (const memo of page.memos) {@@ -171,6 +173,7 @@ export const createEdgeEverZipStream = async (}if (page.nextOffset === null) break;offset = page.nextOffset;+ cursor = page.nextCursor ?? undefined;}const manifest: JsonBackupManifest = {format: EDGEEVER_ZIP_FORMAT, formatVersion: EDGEEVER_ZIP_FORMAT_VERSION, schemaVersion: 1,diff --git a/packages/shared/src/content.test.mjs b/packages/shared/src/content.test.mjsindex 0bdc0f6f..e4298a0c 100644--- a/packages/shared/src/content.test.mjs+++ b/packages/shared/src/content.test.mjs@@ -136,6 +136,25 @@ describe("PDF attachment Markdown compatibility", () => {});describe("file attachment Markdown compatibility", () => {+ test("keeps literal entities and escapes in attachment labels without changing resource URL suffixes", () => {+ const url = "/api/v1/resources/res_label/blob?download=1©=2#page=3";+ for (const [extension, type] of [["pdf", PDF_ATTACHMENT_NODE_TYPE], ["zip", FILE_ATTACHMENT_NODE_TYPE]]) {+ const label = `literal © [ \\[.${extension}`;+ const doc = { type: "doc", content: [{ type: "paragraph", content: [{ type, attrs: { url, label } }] }] };+ const reopened = markdownToDoc(docToMarkdown(doc)).content[0].content[0];+ expect(reopened).toMatchObject({ type, attrs: { label, url } });+ }+ });++ test("retains inline attachment labels containing an escaped opening bracket", () => {+ for (const [extension, type] of [["pdf", PDF_ATTACHMENT_NODE_TYPE], ["zip", FILE_ATTACHMENT_NODE_TYPE]]) {+ const markdown = `See [report\\[2026.${extension}](/api/v1/resources/res_bracket/blob)`;+ const parsed = markdownToDoc(markdown);+ expect(parsed.content[0].content.some(node => node.type === type)).toBe(true);+ expect(markdownToDoc(docToMarkdown(parsed)).content[0].content.some(node => node.type === type)).toBe(true);+ }+ });+test("parses a stored non-PDF resource link as an attachment card", () => {const markdown = "[Attachment: budget.xlsx](/api/v1/resources/res_sheet/blob)";const doc = markdownToDoc(markdown);diff --git a/packages/shared/src/content.ts b/packages/shared/src/content.tsindex 2920b829..2ee3d6a4 100644--- a/packages/shared/src/content.ts+++ b/packages/shared/src/content.ts@@ -15,7 +15,7 @@ import {import { projectNativeUnknownContentForMarkdown } from "./mobile-content-compatibility";import { PLUGIN_EMBED_NODE_TYPE } from "./plugin-embed";import { NEW_IMAGE_WIDTH_PERCENT, parseImageWidth } from "./image-display";-import { IMAGE_GALLERY_NODE_TYPE, groupConsecutiveImagesIntoGalleries, normalizeImageGalleries } from "./image-gallery";+import { IMAGE_GALLERY_NODE_TYPE, groupConsecutiveImagesIntoGalleries, normalizeImageGalleries, prepareImageListsForMarkdown } from "./image-gallery";import { EMPTY_EXTERNAL_LINK_NODE_TYPE } from "./empty-external-link";import { FRONT_MATTER_LANGUAGE, createFrontMatterNode, getFrontMatterSource, splitMarkdownFrontMatter } from "./front-matter";import { splitMarkdownParseChunks } from "./markdown-block-start";@@ -405,9 +405,9 @@ export const docToMarkdown = (doc: unknown): string => {const properties = getFrontMatterSource(doc as TiptapDoc);const body = properties === null ? doc : { ...doc, content: root.content.slice(1) };- const serializableDoc = protectLiteralDollarPairs(projectNativeUnknownContentForMarkdown(+ const serializableDoc = protectLiteralDollarPairs(prepareImageListsForMarkdown(projectNativeUnknownContentForMarkdown(stripEditorOnlyNodes(body) as TiptapDoc- ));+ )));const markdown = markdownManager.serialize(serializableDoc as Parameters<typeof markdownManager.serialize>[0]).replaceAll(LITERAL_DOLLAR_PLACEHOLDER, "\\$");diff --git a/packages/shared/src/document-extensions.ts b/packages/shared/src/document-extensions.tsindex 54dc2bf6..326eb3ef 100644--- a/packages/shared/src/document-extensions.ts+++ b/packages/shared/src/document-extensions.ts@@ -1,5 +1,6 @@import type { AnyExtension } from "@tiptap/core";import { EdgeEverImage as Image, ImageAlignmentAttributes } from "./image-alignment";+import { ImageLinkAttributes } from "./image-link";import { TaskList } from "@tiptap/extension-list";import { EdgeEverTableKit as TableKit } from "./markdown-table";import { Markdown } from "@tiptap/markdown";@@ -51,6 +52,7 @@ export const createEdgeEverDocumentExtensions = (options.table === undefined ? TableKit : TableKit.configure(options.table),...withOptional(options.image, Image),ImageAlignmentAttributes,+ ImageLinkAttributes,...withOptional(options.gallery, ImageGallery),...withOptional(options.pdf, PdfAttachment),...withOptional(options.file, FileAttachment),diff --git a/packages/shared/src/editor-link.ts b/packages/shared/src/editor-link.tsindex 4becdb78..7ad27848 100644--- a/packages/shared/src/editor-link.ts+++ b/packages/shared/src/editor-link.ts@@ -42,9 +42,13 @@ export const EdgeEverLink = Link.extend({attrs: { href: token.href, title: token.title || null },};}- return helpers.applyMark("link", helpers.parseInline(token.tokens || []), {+ const link = {href: token.href,title: token.title || null,- });+ };+ const content = helpers.parseInline(token.tokens || []).map(node =>+ node.type === "image" ? { ...node, attrs: { ...node.attrs, link } } : node,+ );+ return helpers.applyMark("link", content, link);},});diff --git a/packages/shared/src/file-attachment.ts b/packages/shared/src/file-attachment.tsindex 50da5a4a..d9678a1c 100644--- a/packages/shared/src/file-attachment.ts+++ b/packages/shared/src/file-attachment.ts@@ -1,8 +1,9 @@import { mergeAttributes, Node } from "@tiptap/core";import type { TiptapDoc, TiptapMark, TiptapNode, TiptapTextNode } from "./content";import { resolveAttachmentKind } from "./attachment-kind";-import { getAttachmentFilenameFromLabel } from "./resource-links";+import { getAttachmentFilenameFromLabel, resourceLinkToMarkdown } from "./resource-links";import { normalizeAttachmentByteSize } from "./attachment-metadata";+import { decodeString } from "micromark-util-decode-string";export const FILE_ATTACHMENT_NODE_TYPE = "edgeeverFileAttachment" as const;export const FILE_DISPLAY_MODES = ["compact", "inline"] as const;@@ -12,13 +13,10 @@ export type FileDisplayMode = (typeof FILE_DISPLAY_MODES)[number];export const resolveFileDisplayMode = (value: unknown): FileDisplayMode =>value === "compact" ? "compact" : "inline";-const MARKDOWN_LINK_PATTERN = /^\[([^\]]+)\]\(([^\s)]+)(?:\s+"[^"]*")?\)/;+const MARKDOWN_LINK_PATTERN = /^\[((?:\\.|[^[\]\\])+)\]\(([^\s)]+)(?:\s+"[^"]*")?\)/;const ATTACHMENT_LABEL_PATTERN = /^\s*(?:附件[::]|Attachment:)\s*/i;const RESOURCE_URL_PATTERN = /(?:\/api\/v1\/resources\/|edgeever-(?:resource|staged):\/\/)/i;-const escapeMarkdownLabel = (value: string) =>- value.replace(/\\/g, "\\\\").replace(/\[/g, "\\[").replace(/\]/g, "\\]");-export const isFileAttachmentLink = (label: string, url: string) => {if (resolveAttachmentKind(null, getAttachmentFilenameFromLabel(label)) === "pdf" || resolveAttachmentKind(null, url) === "pdf") {return false;@@ -106,7 +104,7 @@ export const FileAttachment = Node.create({},renderMarkdown: (node) =>- `[${escapeMarkdownLabel(String(node.attrs?.label || "Attachment"))}](${String(node.attrs?.url || "")})`,+ resourceLinkToMarkdown(String(node.attrs?.label || "Attachment"), String(node.attrs?.url || "")),markdownTokenizer: {name: FILE_ATTACHMENT_NODE_TYPE,@@ -116,11 +114,13 @@ export const FileAttachment = Node.create({},tokenize(source: string) {const match = MARKDOWN_LINK_PATTERN.exec(source);- if (!match || !isFileAttachmentLink(match[1], match[2])) return undefined;+ if (!match) return undefined;+ const label = decodeString(match[1]);+ if (!isFileAttachmentLink(label, match[2])) return undefined;return {type: FILE_ATTACHMENT_NODE_TYPE,raw: match[0],- label: match[1],+ label,url: match[2],};},diff --git a/packages/shared/src/front-matter.ts b/packages/shared/src/front-matter.tsindex bbfaf701..798c801a 100644--- a/packages/shared/src/front-matter.ts+++ b/packages/shared/src/front-matter.ts@@ -4,10 +4,11 @@ import type { TiptapDoc, TiptapNode } from "./content";// An existing schema node keeps properties readable in older clients.export const FRONT_MATTER_LANGUAGE = "edgeever-frontmatter";const MAX_PROPERTY_SOURCE_LENGTH = 64 * 1024;+type NotePropertiesParseOptions = { maxSourceLength?: number };-export const parseNoteProperties = (source: string) => {+export const parseNoteProperties = (source: string, options: NotePropertiesParseOptions = {}) => {try {- if (source.length > MAX_PROPERTY_SOURCE_LENGTH) throw new Error("Properties exceed 64 KiB.");+ if (source.length > (options.maxSourceLength ?? MAX_PROPERTY_SOURCE_LENGTH)) throw new Error("Properties exceed 64 KiB.");const document = parseDocument(source, { stringKeys: true, prettyErrors: false });if (document.errors.length || document.warnings.length) {throw new Error([...document.errors, ...document.warnings].map((error) => error.message).join("\n"));@@ -23,16 +24,16 @@ export const parseNoteProperties = (source: string) => {};/** Only a leading, closed property header is special; ordinary rules stay Markdown. */-export const splitMarkdownFrontMatter = (markdown: string) => {+export const splitMarkdownFrontMatter = (markdown: string, options: NotePropertiesParseOptions = {}) => {const legacy = /^(`{3,}|~{3,})edgeever-frontmatter[ \t]*\r?\n([\s\S]*?)^\1[ \t]*(?:\r?\n|$)/m.exec(markdown);if (legacy?.index === 0) {const source = legacy[2]!.replace(/\r?\n$/, "").replace(/\r\n?/g, "\n");- return { source, body: markdown.slice(legacy[0].length), ...parseNoteProperties(source) };+ return { source, body: markdown.slice(legacy[0].length), ...parseNoteProperties(source, options) };}const match = /^(?:\uFEFF)?---[ \t]*\r?\n([\s\S]*?)^(?:---|\.\.\.)[ \t]*(?:\r?\n|$)/m.exec(markdown);if (!match || match.index !== 0) return null;const source = match[1]!.replace(/\r?\n$/, "").replace(/\r\n?/g, "\n");- const parsed = parseNoteProperties(source);+ const parsed = parseNoteProperties(source, options);// Don't turn `---\nA paragraph\n---` into a properties panel.if (parsed.error && !source.includes(":")) return null;return { source, body: markdown.slice(match[0].length), ...parsed };diff --git a/packages/shared/src/i18n/en-US.ts b/packages/shared/src/i18n/en-US.tsindex 5b41313e..bbd96e27 100644--- a/packages/shared/src/i18n/en-US.ts+++ b/packages/shared/src/i18n/en-US.ts@@ -1,7 +1,9 @@+import { noteImportEnUS } from "./note-import";import { memoTemplateSeedTranslations } from "../memo-template-seeds";// Shared by the PWA and native mobile clients.export const enUS = {+ noteImport: noteImportEnUS,notebookAppearance: {inversion: "Invert",invertOff: "Off",diff --git a/packages/shared/src/i18n/ja.ts b/packages/shared/src/i18n/ja.tsindex 982660aa..a5af0536 100644--- a/packages/shared/src/i18n/ja.ts+++ b/packages/shared/src/i18n/ja.ts@@ -1,7 +1,9 @@+import { noteImportJa } from "./note-import";import { memoTemplateSeedTranslations } from "../memo-template-seeds";// Shared by the PWA and native mobile clients.export const ja = {+ noteImport: noteImportJa,notebookAppearance: {inversion: "反転",invertOff: "なし",diff --git a/packages/shared/src/i18n/note-import.ts b/packages/shared/src/i18n/note-import.tsnew file mode 100644index 00000000..f8f45b5b--- /dev/null+++ b/packages/shared/src/i18n/note-import.ts@@ -0,0 +1,359 @@+export const noteImportEnUS = {+ "title": "Import from other apps",+ "description": "Move notes and attachments from Evernote, Obsidian or Joplin.",+ "open": "Import notes",+ "viewProgress": "View import progress",+ "source": "Source app",+ "files": "Choose files",+ "folder": "Choose folder",+ "scanning": "Scanning your export…",+ "destination": "Import into",+ "topLevel": "Create top-level notebooks",+ "notebooks": "Notebooks",+ "notes": "Notes",+ "resources": "Attachments",+ "folderPreview": "New notebooks",+ "issueCounts": "{{errors}} errors · {{warnings}} warnings",+ "more": "Show more",+ "guide": "Migration guide",+ "cancel": "Cancel import",+ "close": "Close",+ "confirm": "Confirm import",+ "retry": "Retry unfinished steps",+ "another": "Import another export",+ "openNote": "Open note",+ "complete": "Imported {{completed}} notes",+ "incomplete": "Imported {{completed}} of {{total}} notes",+ "cancelled": "Cancelled · {{completed}} of {{total}} notes imported",+ "syncPending": "Imported data is saved on the server. Refresh the workspace to retry synchronization.",+ "issues": {+ "notesFormat": "Export .enex from Evernote instead of selecting .notes.",+ "outsideRoot": "The file path leaves the selected folder.",+ "duplicatePath": "The archive contains duplicate paths.",+ "symlink": "Symbolic links and special files cannot be imported.",+ "invalidProperties": "The note properties cannot be parsed.",+ "conflictingTimes": "Two properties specify different times.",+ "invalidTime": "The original time is invalid or has no timezone; it is preserved as source text.",+ "missingTimes": "Original timestamps are missing; import time will be used for missing fields.",+ "titleTooLong": "The note title exceeds 160 characters.",+ "ignoredTags": "Only the first 24 tags will be imported; {{count}} will be ignored.",+ "resourceSize": "The resource is empty or exceeds the upload limit.",+ "notebookName": "Notebook names must contain 1–80 characters.",+ "invalidEnex": "This is not a readable ENEX export.",+ "noNotes": "No supported notes were found.",+ "unusedResource": "This file is not attached to any imported note and will not be imported.",+ "missingTag": "A Joplin tag association has no tag record.",+ "invalidJoplin": "The Joplin notebook hierarchy is incomplete or cyclic.",+ "noteLink": "The original inter-note reference is kept without conversion.",+ "missingResource": "The referenced local attachment is missing.",+ "ambiguousResource": "More than one file matches this attachment reference.",+ "invalidArchive": "The archive cannot be read completely."+ },+ "errors": {+ "preflightErrors": "Fix the preflight errors before importing.",+ "unsupportedImportServer": "Update the EdgeEver server to use native import.",+ "missingParent": "The destination notebook is no longer available.",+ "destinationChanged": "A retry must use the original destination.",+ "uncertainNotebook": "Notebook creation is unconfirmed. Check the destination before starting another import.",+ "importConflict": "The imported note was edited elsewhere. It was not overwritten.",+ "uncertainResource": "The write result is unconfirmed. Check the note before uploading again.",+ "importIdentityChanged": "The account or instance changed. The import was stopped.",+ "importWorkerFailed": "The import worker stopped. Choose the source again.",+ "incomplete": "This note is incomplete.",+ "createFailed": "The note could not be created."+ },+ "phases": {+ "folders": "Creating notebooks",+ "notes": "Importing notes",+ "resources": "Uploading attachments",+ "refresh": "Synchronizing workspace"+ }+} as const;++export const noteImportZhCN = {+ "title": "从其他应用导入",+ "description": "迁移 Evernote、Obsidian 或 Joplin 的笔记与附件。",+ "open": "导入笔记",+ "viewProgress": "查看导入进度",+ "source": "来源应用",+ "files": "选择文件",+ "folder": "选择文件夹",+ "scanning": "正在扫描导出文件…",+ "destination": "导入位置",+ "topLevel": "创建为顶层笔记本",+ "notebooks": "笔记本",+ "notes": "笔记",+ "resources": "附件",+ "folderPreview": "将新增的笔记本",+ "issueCounts": "{{errors}} 项错误 · {{warnings}} 项提醒",+ "more": "显示更多",+ "guide": "迁移指南",+ "cancel": "取消导入",+ "close": "关闭",+ "confirm": "确认导入",+ "retry": "重试未完成步骤",+ "another": "导入另一份文件",+ "openNote": "打开笔记",+ "complete": "已导入 {{completed}} 篇笔记",+ "incomplete": "已导入 {{completed}} / {{total}} 篇笔记",+ "cancelled": "已取消 · 已导入 {{completed}} / {{total}} 篇笔记",+ "syncPending": "导入数据已保存在服务端,刷新工作区可重试同步。",+ "issues": {+ "notesFormat": "请从印象笔记导出为 .enex,不能直接导入 .notes。",+ "outsideRoot": "文件路径超出所选文件夹。",+ "duplicatePath": "归档包含重复路径,无法确定应使用哪个文件。",+ "symlink": "不能导入符号链接或特殊文件。",+ "invalidProperties": "无法解析笔记属性。",+ "conflictingTimes": "时间属性存在冲突,请修正后重试。",+ "invalidTime": "原时间无效或缺少时区,原始值保留在来源文本中。",+ "missingTimes": "来源未提供完整时间,缺失字段将使用导入时刻。",+ "titleTooLong": "笔记标题超过 160 字符。",+ "ignoredTags": "仅导入前 24 个标签,忽略其余 {{count}} 个。",+ "resourceSize": "附件为空或超过现有上传大小限制。",+ "notebookName": "笔记本名称须为 1–80 字符。",+ "invalidEnex": "这不是可读取的 ENEX 导出文件。",+ "noNotes": "没有找到可导入的笔记。",+ "unusedResource": "该文件未关联到任何待导入笔记,不会导入。",+ "missingTag": "Joplin 标签关联缺少对应标签记录。",+ "invalidJoplin": "Joplin 笔记本层级不完整或存在循环。",+ "noteLink": "保留原有笔记间引用,本次不转换。",+ "missingResource": "找不到正文引用的本地附件。",+ "ambiguousResource": "该附件引用匹配多个文件,无法确定目标。",+ "invalidArchive": "无法完整读取归档。"+ },+ "errors": {+ "preflightErrors": "请先修正预检错误。",+ "unsupportedImportServer": "请升级 EdgeEver 服务端后使用原生导入。",+ "missingParent": "目标笔记本已不可用。",+ "destinationChanged": "重试必须使用原来的导入位置。",+ "uncertainNotebook": "笔记本创建结果待确认,请先检查目标目录,不要重复导入。",+ "importConflict": "导入笔记已被修改,未覆盖该修改。",+ "uncertainResource": "写入结果待确认,请先检查笔记,不要重复上传。",+ "importIdentityChanged": "账号或实例已变化,导入已停止。",+ "importWorkerFailed": "导入进程已停止,请重新选择来源。",+ "incomplete": "该笔记尚未完整导入。",+ "createFailed": "无法创建笔记。"+ },+ "phases": {+ "folders": "正在创建笔记本",+ "notes": "正在导入笔记",+ "resources": "正在上传附件",+ "refresh": "正在同步工作区"+ }+} as const;++export const noteImportJa = {+ "title": "他のアプリからインポート",+ "description": "Evernote・Obsidian・Joplin のノートと添付ファイルを移行します。",+ "open": "ノートをインポート",+ "viewProgress": "進捗を表示",+ "source": "移行元のアプリ",+ "files": "ファイルを選択",+ "folder": "フォルダーを選択",+ "scanning": "エクスポートを確認中…",+ "destination": "インポート先",+ "topLevel": "最上位にノートブックを作成",+ "notebooks": "ノートブック",+ "notes": "ノート",+ "resources": "添付ファイル",+ "folderPreview": "作成されるノートブック",+ "issueCounts": "エラー {{errors}} 件・注意 {{warnings}} 件",+ "more": "さらに表示",+ "guide": "移行ガイド",+ "cancel": "インポートを中止",+ "close": "閉じる",+ "confirm": "インポートを開始",+ "retry": "未完了の処理を再試行",+ "another": "別のデータをインポート",+ "openNote": "ノートを開く",+ "complete": "{{completed}} 件をインポートしました",+ "incomplete": "{{total}} 件中 {{completed}} 件をインポート",+ "cancelled": "中止しました・{{total}} 件中 {{completed}} 件をインポート",+ "syncPending": "データはサーバーに保存済みです。ワークスペースを更新して同期してください。",+ "issues": {+ "notesFormat": "Evernote から .enex を書き出してください。.notes は読み込めません。",+ "outsideRoot": "パスが選択したフォルダーの外を参照しています。",+ "duplicatePath": "アーカイブに重複するパスがあります。",+ "symlink": "シンボリックリンクと特殊ファイルは読み込めません。",+ "invalidProperties": "ノートのプロパティを解析できません。",+ "conflictingTimes": "日時のプロパティが競合しています。",+ "invalidTime": "元の日時が無効かタイムゾーンがありません。元のテキストを保持します。",+ "missingTimes": "元の日時が不足しています。不足分にはインポート時刻を使用します。",+ "titleTooLong": "タイトルが160文字を超えています。",+ "ignoredTags": "先頭24件のタグのみをインポートし、残り{{count}}件は省略します。",+ "resourceSize": "添付が空か、アップロード上限を超えています。",+ "notebookName": "ノートブック名は1〜80文字にしてください。",+ "invalidEnex": "読み取り可能なENEXではありません。",+ "noNotes": "読み込めるノートがありません。",+ "unusedResource": "どのノートからも参照されていないため読み込みません。",+ "missingTag": "Joplinの関連タグの記録がありません。",+ "invalidJoplin": "Joplinの階層が不完全か循環しています。",+ "noteLink": "ノート間の参照は変換せず元のまま保持します。",+ "missingResource": "参照先のローカル添付ファイルがありません。",+ "ambiguousResource": "参照先に一致するファイルが複数あります。",+ "invalidArchive": "アーカイブを完全に読み取れません。"+ },+ "errors": {+ "preflightErrors": "事前確認のエラーを修正してください。",+ "unsupportedImportServer": "サーバーを更新するとインポートを利用できます。",+ "missingParent": "移行先のノートブックがありません。",+ "destinationChanged": "再試行では元の移行先を使用してください。",+ "uncertainNotebook": "ノートブックの作成結果を確認してから再操作してください。",+ "importConflict": "ノートが別の場所で変更されたため上書きしませんでした。",+ "uncertainResource": "書き込み結果が未確認です。再送信の前にノートを確認してください。",+ "importIdentityChanged": "アカウントまたはインスタンスが変わったため中止しました。",+ "importWorkerFailed": "処理が停止しました。移行元を選び直してください。",+ "incomplete": "このノートは未完了です。",+ "createFailed": "ノートを作成できませんでした。"+ },+ "phases": {+ "folders": "ノートブックを作成中",+ "notes": "ノートをインポート中",+ "resources": "添付をアップロード中",+ "refresh": "ワークスペースを同期中"+ }+} as const;++export const noteImportPl = {+ "title": "Importuj z innych aplikacji",+ "description": "Przenieś notatki i załączniki z Evernote, Obsidian lub Joplin.",+ "open": "Importuj notatki",+ "viewProgress": "Pokaż postęp importu",+ "source": "Aplikacja źródłowa",+ "files": "Wybierz pliki",+ "folder": "Wybierz folder",+ "scanning": "Skanowanie eksportu…",+ "destination": "Importuj do",+ "topLevel": "Utwórz notesy najwyższego poziomu",+ "notebooks": "Notesy",+ "notes": "Notatki",+ "resources": "Załączniki",+ "folderPreview": "Nowe notesy",+ "issueCounts": "Błędy: {{errors}} · Ostrzeżenia: {{warnings}}",+ "more": "Pokaż więcej",+ "guide": "Poradnik migracji",+ "cancel": "Anuluj import",+ "close": "Zamknij",+ "confirm": "Potwierdź import",+ "retry": "Ponów niedokończone kroki",+ "another": "Importuj kolejny eksport",+ "openNote": "Otwórz notatkę",+ "complete": "Zaimportowano notatek: {{completed}}",+ "incomplete": "Zaimportowano {{completed}} z {{total}} notatek",+ "cancelled": "Anulowano · Zaimportowano {{completed}} z {{total}} notatek",+ "syncPending": "Dane zapisano na serwerze. Odśwież obszar roboczy, aby ponowić synchronizację.",+ "issues": {+ "notesFormat": "Wyeksportuj .enex z Evernote zamiast wybierać .notes.",+ "outsideRoot": "Ścieżka wychodzi poza wybrany folder.",+ "duplicatePath": "Archiwum zawiera powtórzone ścieżki.",+ "symlink": "Nie można importować dowiązań ani plików specjalnych.",+ "invalidProperties": "Nie można odczytać właściwości notatki.",+ "conflictingTimes": "Właściwości określają sprzeczne daty.",+ "invalidTime": "Data jest nieprawidłowa lub nie ma strefy czasowej; zachowano oryginalny tekst.",+ "missingTimes": "Brak dat źródłowych; brakujące pola otrzymają czas importu.",+ "titleTooLong": "Tytuł przekracza 160 znaków.",+ "ignoredTags": "Zaimportowane zostaną pierwsze 24 tagi; pozostałe ({{count}}) zostaną pominięte.",+ "resourceSize": "Załącznik jest pusty lub przekracza limit wysyłania.",+ "notebookName": "Nazwa notesu musi mieć 1–80 znaków.",+ "invalidEnex": "To nie jest czytelny eksport ENEX.",+ "noNotes": "Nie znaleziono obsługiwanych notatek.",+ "unusedResource": "Plik nie jest powiązany z importowaną notatką i zostanie pominięty.",+ "missingTag": "Brak rekordu tagu powiązanego w Joplin.",+ "invalidJoplin": "Hierarchia Joplin jest niepełna lub zapętlona.",+ "noteLink": "Zachowano oryginalne odwołanie między notatkami bez konwersji.",+ "missingResource": "Brak wskazanego lokalnego załącznika.",+ "ambiguousResource": "Do odwołania pasuje więcej niż jeden plik.",+ "invalidArchive": "Nie można odczytać całego archiwum."+ },+ "errors": {+ "preflightErrors": "Najpierw popraw błędy skanowania.",+ "unsupportedImportServer": "Zaktualizuj serwer EdgeEver, aby użyć importu.",+ "missingParent": "Notes docelowy nie jest już dostępny.",+ "destinationChanged": "Ponowienie musi używać pierwotnego miejsca docelowego.",+ "uncertainNotebook": "Wynik tworzenia notesu jest nieznany. Sprawdź miejsce docelowe przed kolejnym importem.",+ "importConflict": "Notatka została zmieniona. Nie została nadpisana.",+ "uncertainResource": "Wynik zapisu jest nieznany. Sprawdź notatkę przed ponownym wysłaniem.",+ "importIdentityChanged": "Konto lub instancja uległy zmianie. Import zatrzymano.",+ "importWorkerFailed": "Proces importu zatrzymał się. Wybierz źródło ponownie.",+ "incomplete": "Import tej notatki jest niepełny.",+ "createFailed": "Nie udało się utworzyć notatki."+ },+ "phases": {+ "folders": "Tworzenie notesów",+ "notes": "Importowanie notatek",+ "resources": "Wysyłanie załączników",+ "refresh": "Synchronizowanie obszaru roboczego"+ }+} as const;++export const noteImportRu = {+ "title": "Импорт из других приложений",+ "description": "Перенесите заметки и вложения из Evernote, Obsidian или Joplin.",+ "open": "Импортировать заметки",+ "viewProgress": "Показать ход импорта",+ "source": "Исходное приложение",+ "files": "Выбрать файлы",+ "folder": "Выбрать папку",+ "scanning": "Сканирование экспорта…",+ "destination": "Импортировать в",+ "topLevel": "Создать блокноты верхнего уровня",+ "notebooks": "Блокноты",+ "notes": "Заметки",+ "resources": "Вложения",+ "folderPreview": "Новые блокноты",+ "issueCounts": "Ошибок: {{errors}} · Предупреждений: {{warnings}}",+ "more": "Показать ещё",+ "guide": "Руководство по переносу",+ "cancel": "Отменить импорт",+ "close": "Закрыть",+ "confirm": "Подтвердить импорт",+ "retry": "Повторить незавершённые шаги",+ "another": "Импортировать другой экспорт",+ "openNote": "Открыть заметку",+ "complete": "Импортировано заметок: {{completed}}",+ "incomplete": "Импортировано {{completed}} из {{total}} заметок",+ "cancelled": "Отменено · Импортировано {{completed}} из {{total}} заметок",+ "syncPending": "Данные сохранены на сервере. Обновите рабочее пространство для повторной синхронизации.",+ "issues": {+ "notesFormat": "Экспортируйте .enex из Evernote вместо выбора .notes.",+ "outsideRoot": "Путь выходит за пределы выбранной папки.",+ "duplicatePath": "Архив содержит повторяющиеся пути.",+ "symlink": "Символические ссылки и специальные файлы не поддерживаются.",+ "invalidProperties": "Не удалось разобрать свойства заметки.",+ "conflictingTimes": "Свойства содержат разные даты.",+ "invalidTime": "Исходное время неверно или без часового пояса; исходный текст сохранён.",+ "missingTimes": "Исходные даты отсутствуют; для пропущенных полей используется время импорта.",+ "titleTooLong": "Название заметки длиннее 160 символов.",+ "ignoredTags": "Будут импортированы первые 24 тега, остальные ({{count}}) будут пропущены.",+ "resourceSize": "Вложение пустое или превышает лимит загрузки.",+ "notebookName": "Название блокнота должно содержать 1–80 символов.",+ "invalidEnex": "Не удалось прочитать экспорт ENEX.",+ "noNotes": "Поддерживаемые заметки не найдены.",+ "unusedResource": "Файл не связан с импортируемыми заметками и будет пропущен.",+ "missingTag": "В Joplin отсутствует связанный тег.",+ "invalidJoplin": "Иерархия Joplin неполна или содержит цикл.",+ "noteLink": "Исходная ссылка между заметками сохранена без преобразования.",+ "missingResource": "Не найдено локальное вложение по ссылке.",+ "ambiguousResource": "Ссылке соответствует несколько файлов.",+ "invalidArchive": "Не удалось полностью прочитать архив."+ },+ "errors": {+ "preflightErrors": "Сначала исправьте ошибки проверки.",+ "unsupportedImportServer": "Обновите сервер EdgeEver для использования импорта.",+ "missingParent": "Целевой блокнот больше недоступен.",+ "destinationChanged": "При повторе используйте исходное назначение.",+ "uncertainNotebook": "Создание блокнота не подтверждено. Проверьте назначение перед новым импортом.",+ "importConflict": "Заметка изменена в другом месте и не была перезаписана.",+ "uncertainResource": "Запись не подтверждена. Проверьте заметку перед повторной загрузкой.",+ "importIdentityChanged": "Аккаунт или экземпляр изменился. Импорт остановлен.",+ "importWorkerFailed": "Процесс импорта остановлен. Выберите источник заново.",+ "incomplete": "Эта заметка импортирована не полностью.",+ "createFailed": "Не удалось создать заметку."+ },+ "phases": {+ "folders": "Создание блокнотов",+ "notes": "Импорт заметок",+ "resources": "Загрузка вложений",+ "refresh": "Синхронизация рабочего пространства"+ }+} as const;diff --git a/packages/shared/src/i18n/pl.ts b/packages/shared/src/i18n/pl.tsindex d487916a..fe07d6ff 100644--- a/packages/shared/src/i18n/pl.ts+++ b/packages/shared/src/i18n/pl.ts@@ -1,7 +1,9 @@+import { noteImportPl } from "./note-import";import { memoTemplateSeedTranslations } from "../memo-template-seeds";// Shared by the PWA and native mobile clients.export const pl = {+ noteImport: noteImportPl,notebookAppearance: {inversion: "Odwrócenie",invertOff: "Wyłączone",diff --git a/packages/shared/src/i18n/ru.ts b/packages/shared/src/i18n/ru.tsindex 3375eb16..bcbe1f1c 100644--- a/packages/shared/src/i18n/ru.ts+++ b/packages/shared/src/i18n/ru.ts@@ -1,7 +1,9 @@+import { noteImportRu } from "./note-import";import { memoTemplateSeedTranslations } from "../memo-template-seeds";// Shared by the PWA and native mobile clients.export const ru = {+ noteImport: noteImportRu,notebookAppearance: {inversion: "Инверсия",invertOff: "Выключена",diff --git a/packages/shared/src/i18n/zh-CN.ts b/packages/shared/src/i18n/zh-CN.tsindex f8c1b3ee..e575da45 100644--- a/packages/shared/src/i18n/zh-CN.ts+++ b/packages/shared/src/i18n/zh-CN.ts@@ -1,7 +1,9 @@+import { noteImportZhCN } from "./note-import";import { memoTemplateSeedTranslations } from "../memo-template-seeds";// Shared by the PWA and native mobile clients.export const zhCN = {+ noteImport: noteImportZhCN,notebookAppearance: {inversion: "反转",invertOff: "关闭",diff --git a/packages/shared/src/image-alignment.ts b/packages/shared/src/image-alignment.tsindex b9398af9..4c2cf82c 100644--- a/packages/shared/src/image-alignment.ts+++ b/packages/shared/src/image-alignment.ts@@ -1,7 +1,10 @@import { Extension, type JSONContent } from "@tiptap/core";import Image from "@tiptap/extension-image";+import { isAllowedUri } from "@tiptap/extension-link";+import { resourceNodeToMarkdown } from "./resource-links";import { parseImageWidth } from "./image-display";import { openBlockSource } from "./markdown-block-start";+import { getImageLink } from "./image-link";export const IMAGE_ALIGNMENTS = ["left", "center", "right"] as const;export type ImageAlignment = (typeof IMAGE_ALIGNMENTS)[number];@@ -75,6 +78,7 @@ const supportedImageSource = (source: string) => {if (!source || /[\u0000-\u0020]/.test(source)) return false;return /^(?:https?:\/\/|edgeever-resource:\/\/|edgeever-staged:\/\/|\/(?!\/))/i.test(source);};+const LINKED_IMAGE_HTML = /<a\b((?:[^"'>]|"[^"]*"|'[^']*')*)>\s*(<img\b(?:[^"'>]|"[^"]*"|'[^']*')*\/?>)\s*<\/a>[ \t]*(?:\n|$)/i;/** HTML keeps the alignment useful in exports outside EdgeEver, too. */export const alignedImageToHtml = (node: JSONContent): string => {@@ -83,38 +87,64 @@ export const alignedImageToHtml = (node: JSONContent): string => {const width = parseImageWidth(attrs.width);const attribute = (key: string, value: unknown) => `${key}="${escapeAttribute(String(value ?? ""))}"`;const values = [attribute("src", attrs.src), attribute("alt", attrs.alt)];+ const link = getImageLink(attrs.link);+ if (link) {+ values.push(attribute("data-image-link-href", link.href));+ if (link.title) values.push(attribute("data-image-link-title", link.title));+ }if (attrs.title) values.push(attribute("title", attrs.title));values.push(attribute("data-image-align", alignment));if (width !== null) values.push(attribute("width", `${width}%`));const style = imageAlignmentStyle(alignment, width);values.push(attribute("style", style));- return `<img ${values.join(" ")} />`;+ const image = `<img ${values.join(" ")} />`;+ return link && isAllowedUri(link.href)+ ? `<a ${attribute("href", link.href)}${link.title ? ` ${attribute("title", link.title)}` : ""}>${image}</a>`+ : image;};export const EdgeEverImage = Image.extend({- renderMarkdown(node, helpers, context) {- return parseImageAlignment(node.attrs?.alignment)- ? alignedImageToHtml(node)- : this.parent?.(node, helpers, context) ?? "";+ renderMarkdown(node) {+ if (parseImageAlignment(node.attrs?.alignment)) return alignedImageToHtml(node);+ const link = getImageLink(node.attrs?.link);+ const image = { type: "image" as const, url: String(node.attrs?.src ?? ""), alt: String(node.attrs?.alt ?? ""), title: typeof node.attrs?.title === "string" ? node.attrs.title : null };+ return resourceNodeToMarkdown(link ? { type: "link", url: link.href, title: link.title, children: [image] } : image);},parseMarkdown(token, helpers) {- return token.imageAttributes- ? helpers.createNode("image", token.imageAttributes)- : this.parent?.(token, helpers) ?? [];+ if (token.imageAttributes) return helpers.createNode("image", token.imageAttributes);+ // Marked already unescapes brackets, but leaves backslash escapes in text.+ // Read literal lexer tokens; parsing alt as rich content would consume math.+ const alt = token.tokens?.map(part => part.type === "escape" ? part.text : part.raw ?? part.text ?? "").join("") ?? token.text;+ return helpers.createNode("image", {+ src: token.href, title: token.title,+ alt,+ });},markdownTokenizer: {name: "image",level: "block",- start: (source) => openBlockSource(source).search(/<img\b/i),+ start: (source) => {+ const open = openBlockSource(source);+ const image = open.search(/<img\b/i);+ const linked = LINKED_IMAGE_HTML.exec(open)?.index ?? -1;+ return image === -1 ? linked : linked === -1 ? image : Math.min(image, linked);+ },tokenize(source) {- const match = /^<img\b(?:[^"'>]|"[^"]*"|'[^']*')*\/?>[ \t]*(?:\n|$)/i.exec(source);+ const candidate = LINKED_IMAGE_HTML.exec(source);+ const wrapped = candidate?.index === 0 ? candidate : null;+ const match = wrapped ?? /^<img\b(?:[^"'>]|"[^"]*"|'[^']*')*\/?>[ \t]*(?:\n|$)/i.exec(source);if (!match) return undefined;- const tag = match[0];+ const tag = wrapped?.[2] ?? match[0];const alignment = parseImageAlignment(readImageHtmlAttribute(tag, "data-image-align"));const src = readImageHtmlAttribute(tag, "src");- if (!alignment || !supportedImageSource(src)) return undefined;- return { type: "image", raw: tag, imageAttributes: {+ const link = getImageLink({+ href: readImageHtmlAttribute(tag, "data-image-link-href") || (wrapped ? readImageHtmlAttribute(wrapped[1]!, "href") : ""),+ title: readImageHtmlAttribute(tag, "data-image-link-title") || (wrapped ? readImageHtmlAttribute(wrapped[1]!, "title") : "") || null,+ });+ if ((!alignment && !link) || !supportedImageSource(src)) return undefined;+ return { type: "image", raw: match[0], imageAttributes: {src, alignment,+ ...(link ? { link } : {}),alt: readImageHtmlAttribute(tag, "alt") || null,title: readImageHtmlAttribute(tag, "title") || null,width: parseImageWidth(readImageHtmlAttribute(tag, "width")),diff --git a/packages/shared/src/image-gallery.ts b/packages/shared/src/image-gallery.tsindex c0ce79fe..cdefe80e 100644--- a/packages/shared/src/image-gallery.ts+++ b/packages/shared/src/image-gallery.ts@@ -24,6 +24,40 @@ export const resolveImageGalleryLayout = (value: unknown): ImageGalleryLayout =>? (value as ImageGalleryLayout): "auto";+/** The shared schema uses block images, including inside lists and quotes. */+const normalizeImageParagraphs = (nodes: TiptapNode[]): TiptapNode[] => nodes.flatMap(node => {+ const content = node.content ? normalizeImageParagraphs(node.content) : undefined;+ if (!content) return [node];+ if (node.type === "paragraph" && content.some(child => child.type === "image")) {+ const blocks: TiptapNode[] = [];+ let inline: TiptapNode[] = [];+ const flush = () => { if (inline.length) blocks.push({ ...node, content: inline }); inline = []; };+ for (const child of content) {+ if (child.type === "image") { flush(); blocks.push(child); }+ else inline.push(child);+ }+ flush();+ return blocks;+ }+ if ((node.type === "listItem" || node.type === "taskItem") && node.content?.[0]?.type === "paragraph" && content[0]?.type !== "paragraph") content.unshift({ type: "paragraph" });+ return [{ ...node, content }];+});++/** A list's required empty first paragraph must not separate its marker from an image. */+export const prepareImageListsForMarkdown = (doc: TiptapDoc): TiptapDoc => {+ const visit = (node: TiptapNode): TiptapNode => {+ if (!node.content) return node;+ let content = node.content.map(child => child.type === "text" ? child : visit(child));+ if ((node.type === "listItem" || node.type === "taskItem")+ && content[0]?.type === "paragraph" && !content[0].content?.length+ && content[1]?.type === "image") {+ content = content.slice(1);+ }+ return { ...node, content };+ };+ return { ...doc, content: doc.content.map(visit) };+};+/*** Width-fit galleries retain the ordinary sequential-image Markdown projection.* Height-fit galleries use invisible comments to keep their presentation when@@ -86,7 +120,7 @@ export const ImageGallery = Node.create({},parseMarkdown(token, helpers) {- const content = helpers.parseChildren(token.tokens ?? []);+ const content = normalizeImageParagraphs(helpers.parseChildren(token.tokens ?? []) as TiptapNode[]);// A source edit can split a gallery with text. Keep all edited content,// rather than forcing non-images into an image-only schema.if (content.length < 2 || content.some((child) => child.type !== "image")) return content;@@ -136,7 +170,7 @@ export const groupConsecutiveImagesIntoGalleries = (nodes: TiptapNode[]): Tiptapimages = [];};- for (const node of nodes) {+ for (const node of normalizeImageParagraphs(nodes)) {// Explicitly positioned images remain standalone during Markdown import.if (node.type === "image" && parseImageAlignment(node.attrs?.alignment)) {flushImages();diff --git a/packages/shared/src/image-link.test.mjs b/packages/shared/src/image-link.test.mjsnew file mode 100644index 00000000..73387adc--- /dev/null+++ b/packages/shared/src/image-link.test.mjs@@ -0,0 +1,120 @@+import { describe, expect, test } from "bun:test";+import { getSchema } from "@tiptap/core";+import Image from "@tiptap/extension-image";+import { DOMParser, DOMSerializer } from "@tiptap/pm/model";+import { parseHTML } from "linkedom";+import { docToMarkdown, docToText, markdownToDoc } from "./content";+import { createEdgeEverDocumentExtensions } from "./document-extensions";+import { createEdgeEverMarkdownMathematics } from "./mathematics-markdown";++const imageUrl = "/api/v1/resources/image/blob";+const link = { href: "/api/v1/resources/manual/blob", title: "Document" };+const images = doc => {+ const result = [];+ const visit = node => { if (node.type === "image") result.push(node); node.content?.forEach(visit); };+ visit(doc); return result;+};+const linked = { type: "image", attrs: { src: imageUrl, alt: "thumb", title: "Preview", width: 72, link } };++describe("linked image persistence", () => {+ test("retains separate image and outer-link targets/titles through Markdown", () => {+ const source = `[](${link.href} "Document")`;+ const doc = markdownToDoc(source);+ expect(doc.content[0].type).toBe("image");+ expect(images(doc)[0].attrs).toMatchObject(linked.attrs);+ expect(docToMarkdown(doc)).toBe(source);+ });++ test("preserves escaped outer targets and titles without changing text links", () => {+ const doc = { type: "doc", content: [{ ...linked, attrs: { ...linked.attrs, link: { href: "https://example.test/a(b)?x=©", title: 'A "quoted" \\ title' } } }, { type: "paragraph", content: [{ type: "text", text: "ordinary", marks: [{ type: "link", attrs: { href: "https://example.test" } }] }] }] };+ const restored = markdownToDoc(docToMarkdown(doc));+ expect(images(restored)[0].attrs).toMatchObject(doc.content[0].attrs);+ expect(docToMarkdown(restored)).toContain("[ordinary](https://example.test)");+ });++ test("keeps image labels literal rather than interpreting them as rich text", () => {+ const alt = "图 [ ] \\ $x$ ©";+ const doc = { type: "doc", content: [{ type: "image", attrs: { src: imageUrl, alt } }] };+ expect(images(markdownToDoc(docToMarkdown(doc)))[0].attrs.alt).toBe(alt);+ });++ test("retains escaped unmatched brackets inside inline and reference image links", () => {+ for (const alt of ["thumb ]", "thumb [", "thumb ] \\ $x$"]) {+ const doc = { type: "doc", content: [{ ...linked, attrs: { ...linked.attrs, alt } }] };+ const markdown = docToMarkdown(doc);+ const reference = markdown.replace(`(${link.href} "Document")`, "[manual]") + `\n\n[manual]: ${link.href} "Document"`;+ for (const source of [markdown, reference]) {+ expect(images(markdownToDoc(source))[0]?.attrs).toMatchObject({ alt, src: imageUrl, link });+ }+ }+ });++ test("custom image schemas retain link metadata through JSON and HTML clipboard", () => {+ const schema = getSchema(createEdgeEverDocumentExtensions({ image: Image, mathematics: createEdgeEverMarkdownMathematics() }));+ const doc = schema.nodeFromJSON({ type: "doc", content: [linked, { type: "paragraph", content: [{ type: "text", text: "Edited text" }] }] });+ doc.check();+ const { document } = parseHTML("<html><body></body></html>");+ document.body.appendChild(DOMSerializer.fromSchema(schema).serializeFragment(doc.content, { document }));+ const restored = DOMParser.fromSchema(schema).parse(document.body).toJSON();+ expect(images(restored)[0].attrs.link).toEqual(link);+ expect(images(markdownToDoc(docToMarkdown(restored)))[0].attrs.link).toEqual(link);+ });++ test("gallery fitting and individual alignment retain image links", () => {+ const gallery = { type: "doc", content: [{ type: "edgeeverImageGallery", attrs: { fit: "height", layout: "2" }, content: [linked, { type: "image", attrs: { src: "/other.png", alt: "other", width: 72 } }] }] };+ const restored = markdownToDoc(docToMarkdown(gallery));+ expect(restored.content[0]).toMatchObject(gallery.content[0]);+ const aligned = { type: "doc", content: [{ ...linked, attrs: { ...linked.attrs, alignment: "right", width: 35 } }] };+ expect(images(markdownToDoc(docToMarkdown(aligned)))[0]).toMatchObject(aligned.content[0]);+ });++ test("does not activate unsafe targets in aligned HTML exports", () => {+ const unsafe = { type: "doc", content: [{ ...linked, attrs: { ...linked.attrs, alignment: "right", link: { href: "javascript:alert(1)", title: "Source" } } }] };+ const markdown = docToMarkdown(unsafe);+ expect(markdown).not.toContain('<a href="javascript:');+ expect(images(markdownToDoc(markdown))[0].attrs.link).toEqual(unsafe.content[0].attrs.link);+ });++ test("keeps linked images valid inside lists, quotes and mixed paragraphs", () => {+ const schema = getSchema(createEdgeEverDocumentExtensions({ mathematics: createEdgeEverMarkdownMathematics() }));+ const source = `[](${link.href} "Document")`;+ for (const markdown of [`- ${source}`, `3. ${source}`, `- [x] ${source}`, `> ${source}`, `Before ${source} after`]) {+ const doc = markdownToDoc(markdown);+ schema.nodeFromJSON(doc).check();+ const reopened = markdownToDoc(docToMarkdown(doc));+ schema.nodeFromJSON(reopened).check();+ expect(reopened.content[0].type).toBe(doc.content[0].type);+ expect(images(reopened)[0].attrs.link).toEqual(link);+ if (markdown.startsWith("Before")) expect(docToText(reopened)).toContain("Before");+ }+ });++ test("keeps existing image-only list JSON and nested siblings after exporting and reopening", () => {+ const schema = getSchema(createEdgeEverDocumentExtensions({ mathematics: createEdgeEverMarkdownMathematics() }));+ const paragraph = text => ({ type: "paragraph", content: [{ type: "text", text }] });+ const variants = [["bulletList", "listItem", {}], ["orderedList", "listItem", { start: 3 }], ["taskList", "taskItem", {}]];+ for (const [type, itemType, attrs] of variants) {+ const item = itemType === "taskItem" ? { type: itemType, attrs: { checked: true } } : { type: itemType };+ const doc = { type: "doc", content: [{+ type, ...(type === "orderedList" ? { attrs } : {}), content: [+ { ...item, content: [+ { type: "paragraph" }, linked, paragraph("After image"),+ { type: "bulletList", content: [{ type: "listItem", content: [paragraph("Nested")] }] },+ ] },+ { ...item, content: [paragraph("Sibling")] },+ ],+ }] };+ schema.nodeFromJSON(doc).check();+ const original = JSON.stringify(doc);+ const reopened = markdownToDoc(docToMarkdown(doc));+ schema.nodeFromJSON(reopened).check();+ expect(reopened).toMatchObject(doc);+ expect(JSON.stringify(doc)).toBe(original);+ }+ });++ test("ordinary inline HTML anchors do not start an image block", () => {+ const source = 'Hello <a href="https://example.com">site</a> tail';+ expect(markdownToDoc(source).content[0].content.map(node => node.text ?? "").join("")).toBe(source);+ });+});diff --git a/packages/shared/src/image-link.ts b/packages/shared/src/image-link.tsnew file mode 100644index 00000000..464dea75--- /dev/null+++ b/packages/shared/src/image-link.ts@@ -0,0 +1,35 @@+import { Extension } from "@tiptap/core";++type ImageLink = { href: string; title: string | null };++export const getImageLink = (value: unknown): ImageLink | null => {+ if (!value || typeof value !== "object" || !("href" in value) || typeof value.href !== "string" || !value.href) return null;+ return { href: value.href, title: "title" in value && typeof value.title === "string" ? value.title : null };+};++// Block images cannot carry link marks in the document schema. Register the+// same optional metadata on custom Web/native image implementations as well.+export const ImageLinkAttributes = Extension.create({+ name: "imageLink",+ addGlobalAttributes() {+ return [{ types: ["image"], attributes: {+ link: {+ default: null,+ parseHTML: (element) => {+ const anchor = element.closest("a[href]");+ return getImageLink({+ href: element.getAttribute("data-image-link-href") ?? anchor?.getAttribute("href"),+ title: element.getAttribute("data-image-link-title") ?? anchor?.getAttribute("title"),+ });+ },+ renderHTML: (attributes) => {+ const link = getImageLink(attributes.link);+ return link ? {+ "data-image-link-href": link.href,+ ...(link.title ? { "data-image-link-title": link.title } : {}),+ } : {};+ },+ },+ } }];+ },+});diff --git a/packages/shared/src/markdown-options.ts b/packages/shared/src/markdown-options.tsindex 53b57f57..596ccf98 100644--- a/packages/shared/src/markdown-options.ts+++ b/packages/shared/src/markdown-options.ts@@ -1,4 +1,18 @@-import { Marked, type marked } from "marked";+import { Marked, Tokenizer, type marked } from "marked";++const preserveImageLabelEscapes = <T extends ReturnType<Tokenizer["reflink"]>>(token: T, tokenizer: Tokenizer): T => {+ if (token?.type !== "link" || !token.raw.includes("![")) return token;+ const label = [tokenizer.rules.inline.link, tokenizer.rules.inline.reflink, tokenizer.rules.inline.nolink]+ .map(rule => rule.exec(token.raw)?.[1]).find(value => value !== undefined);+ if (!label || !(label.includes("\\[") || label.includes("\\]"))) return token;+ // Marked unescapes the outer label before lexing it, which can turn an+ // escaped image bracket into a delimiter. Re-lex only that matched label.+ const inLink = tokenizer.lexer.state.inLink;+ tokenizer.lexer.state.inLink = true;+ try { token.tokens = tokenizer.lexer.inlineTokens(label); }+ finally { tokenizer.lexer.state.inLink = inLink; }+ return token;+};/** EdgeEver uses double tildes for strike; single tildes belong to prose. */// TipTap types the injected instance as its callable singleton, but uses the@@ -6,6 +20,12 @@ import { Marked, type marked } from "marked";export const createEdgeEverMarked = () => new Marked({gfm: true,tokenizer: {+ link(src) {+ return preserveImageLabelEscapes(Tokenizer.prototype.link.call(this, src), this);+ },+ reflink(src, links) {+ return preserveImageLabelEscapes(Tokenizer.prototype.reflink.call(this, src, links), this);+ },del(src: string) {// undefined rejects this token; false delegates to marked's tokenizer.if (/^~(?!~)/.test(src)) return undefined;diff --git a/packages/shared/src/note-import/evernote.ts b/packages/shared/src/note-import/evernote.tsnew file mode 100644index 00000000..d7db1d51--- /dev/null+++ b/packages/shared/src/note-import/evernote.ts@@ -0,0 +1,110 @@+import sax from "sax";+import SparkMD5 from "spark-md5";+import { IMPORT_CHUNK_BYTES, type ImportIndex, type ImportResource } from "./types";++export type EnexNote = { ordinal: number; title: string; content: string; tags: string[]; created: string; updated: string; resources: ImportResource[] };+/** SAX flushes text after every bounded input chunk, including huge base64 fields. */+export async function* readEnex(+ source: AsyncIterable<Uint8Array<ArrayBuffer>>, path: string, index?: ImportIndex, stageFrom = Infinity,+): AsyncGenerator<EnexNote> {+ const parser = sax.parser(true, { trim: false, normalize: false });+ const stack: string[] = [];+ let ordinal = -1;+ let root = false;+ let note: EnexNote | undefined;+ let resource: ImportResource | undefined;+ let digest: SparkMD5.ArrayBuffer | undefined;+ let carry = "";+ let chunk = new Uint8Array(IMPORT_CHUNK_BYTES);+ let used = 0;+ let tagText = "";+ let resourceNumber = 0;+ const ready: EnexNote[] = [];+ let writes: Promise<void>[] = [];+ const stage = (bytes: Uint8Array<ArrayBuffer>) => {+ if (!resource || !index || ordinal < stageFrom) return;+ let offset = 0;+ while (offset < bytes.length) {+ const length = Math.min(chunk.length - used, bytes.length - offset);+ chunk.set(bytes.subarray(offset, offset + length), used);+ used += length; offset += length;+ if (used === chunk.length) {+ writes.push(index.put("chunk", `${resource.key}:${resource.chunks ?? 0}`, chunk));+ resource.chunks = (resource.chunks ?? 0) + 1;+ chunk = new Uint8Array(IMPORT_CHUNK_BYTES); used = 0;+ }+ }+ };+ const decode = (text: string) => {+ const clean = text.replace(/\s/g, "");+ if (/[^A-Za-z0-9+/=]/.test(clean)) throw new Error("invalidBase64");+ carry += clean;+ const length = carry.length - carry.length % 4;+ if (!length) return;+ const value = atob(carry.slice(0, length));+ carry = carry.slice(length);+ const bytes = new Uint8Array(value.length);+ for (let i = 0; i < value.length; i++) bytes[i] = value.charCodeAt(i);+ resource!.byteSize += bytes.length;+ digest!.append(bytes.buffer);+ stage(bytes);+ };+ parser.onopentag = tag => {+ stack.push(tag.name);+ if (stack.length === 1) { if (tag.name !== "en-export") throw new Error("invalidEnex"); root = true; }+ if (tag.name === "note" && stack.length === 2) {+ ordinal += 1; resourceNumber = 0;+ note = { ordinal, title: "", content: "", tags: [], created: "", updated: "", resources: [] };+ }+ if (note && tag.name === "resource" && stack.length === 3) {+ resource = { key: `enex:${path}:${ordinal}:${resourceNumber++}`, filename: "", mimeType: "application/octet-stream", byteSize: 0, enex: true, chunks: 0 };+ digest = new SparkMD5.ArrayBuffer(); carry = ""; used = 0; chunk = new Uint8Array(IMPORT_CHUNK_BYTES);+ }+ if (resource && tag.name === "data" && tag.attributes.encoding !== "base64") throw new Error("unsupportedEnexEncoding");+ if (tag.name === "tag") tagText = "";+ };+ const text = (value: string) => {+ if (!note) return;+ const name = stack.at(-1);+ if (resource) {+ if (name === "data") decode(value);+ else if (name === "mime") resource.mimeType = (resource.mimeType === "application/octet-stream" ? "" : resource.mimeType) + value;+ else if (name === "file-name") resource.filename += value;+ } else if (name === "title") note.title += value;+ else if (name === "created") note.created += value;+ else if (name === "updated") note.updated += value;+ else if (name === "tag") tagText += value;+ else if (name === "content") note.content += value;+ };+ parser.ontext = text;+ parser.oncdata = text;+ parser.onclosetag = name => {+ if (name === "data" && resource) {+ if (carry) throw new Error("invalidBase64");+ if (index && ordinal >= stageFrom && used) {+ writes.push(index.put("chunk", `${resource.key}:${resource.chunks ?? 0}`, chunk.slice(0, used)));+ resource.chunks = (resource.chunks ?? 0) + 1;+ }+ resource.hash = digest!.end();+ }+ if (name === "resource" && resource) {+ resource.filename ||= `attachment-${resourceNumber}`;+ note!.resources.push(resource); resource = undefined; digest = undefined;+ }+ if (name === "tag" && note) note.tags.push(tagText.trim());+ if (name === "note" && note) { ready.push(note); note = undefined; }+ stack.pop();+ };+ const decoder = new TextDecoder();+ for await (const bytes of source) {+ for (let at = 0; at < bytes.length; at += IMPORT_CHUNK_BYTES) {+ parser.write(decoder.decode(bytes.subarray(at, at + IMPORT_CHUNK_BYTES), { stream: true })).flush();+ await Promise.all(writes); writes = [];+ while (ready.length) yield ready.shift()!;+ }+ }+ parser.write(decoder.decode()).close();+ await Promise.all(writes);+ if (!root) throw new Error("invalidEnex");+ while (ready.length) yield ready.shift()!;+}diff --git a/packages/shared/src/note-import/index-store.ts b/packages/shared/src/note-import/index-store.tsnew file mode 100644index 00000000..3167c1ab--- /dev/null+++ b/packages/shared/src/note-import/index-store.ts@@ -0,0 +1,26 @@+import type { ImportIndex, ImportKind } from "./types";++/** Tests and non-browser callers can supply this without changing parsing rules. */+export class MemoryImportIndex implements ImportIndex {+ private data = new Map<ImportKind, Map<string, unknown>>();+ async put<T>(kind: ImportKind, key: string, value: T) {+ let table = this.data.get(kind);+ if (!table) this.data.set(kind, table = new Map());+ table.set(key, value);+ }+ async get<T>(kind: ImportKind, key: string) { return this.data.get(kind)?.get(key) as T | undefined; }+ async page<T>(kind: ImportKind, offset = 0, limit = 50) {+ return [...(this.data.get(kind)?.entries() ?? [])].sort(([a], [b]) => a < b ? -1 : a > b ? 1 : 0).slice(offset, offset + limit).map(([, value]) => value as T);+ }+ async count(kind: ImportKind) { return this.data.get(kind)?.size ?? 0; }+ async remove(kind: ImportKind, key: string) { this.data.get(kind)?.delete(key); }+ async clearKind(kind: ImportKind) { this.data.delete(kind); }+ async clear() { this.data.clear(); }+}+export async function* indexItems<T>(index: ImportIndex, kind: ImportKind) {+ for (let offset = 0; ; offset += 50) {+ const page = await index.page<T>(kind, offset, 50);+ if (!page.length) break;+ yield* page;+ }+}diff --git a/packages/shared/src/note-import/index.ts b/packages/shared/src/note-import/index.tsnew file mode 100644index 00000000..846a8517--- /dev/null+++ b/packages/shared/src/note-import/index.ts@@ -0,0 +1,4 @@+export * from "./types";+export { MemoryImportIndex, indexItems } from "./index-store";+export { NoteImportSession } from "./session";+export { findImportReferences, rewriteImportMarkdown } from "./references";diff --git a/packages/shared/src/note-import/metadata.ts b/packages/shared/src/note-import/metadata.tsnew file mode 100644index 00000000..5f54a07d--- /dev/null+++ b/packages/shared/src/note-import/metadata.ts@@ -0,0 +1,52 @@+import { getMappedNoteProperties, splitMarkdownFrontMatter } from "../front-matter";+import type { ImportIndex, ImportNote } from "./types";+import { addIssue } from "./sources";++export const importTime = (value: unknown): string | undefined => {+ if (typeof value !== "string" || !value.trim()) return;+ let text = value.trim().replace(/^(\d{4}-\d{2}-\d{2}) (?=\d{2}:)/, "$1T");+ const compact = /^(\d{4})(\d\d)(\d\d)T(\d\d)(\d\d)(\d\d)Z$/.exec(text);+ if (compact) text = `${compact[1]}-${compact[2]}-${compact[3]}T${compact[4]}:${compact[5]}:${compact[6]}Z`;+ if (!/^\d{4}-\d{2}-\d{2}T\d{2}:\d{2}(?::\d{2}(?:\.\d+)?)?(?:Z|[+-]\d{2}:\d{2})$/i.test(text)) return;+ const day = text.slice(0, 10);+ const calendar = new Date(`${day}T00:00:00Z`);+ if (Number.isNaN(calendar.getTime()) || calendar.toISOString().slice(0, 10) !== day) return;+ const date = new Date(text);+ if (Number.isNaN(date.getTime())) return;+ return date.toISOString();+};+export const readMarkdownMetadata = async (markdown: string, title: string, index: ImportIndex, path: string) => {+ // The editor's interactive parsing budget is not an import/storage limit.+ const header = splitMarkdownFrontMatter(markdown, { maxSourceLength: Infinity });+ if (header?.error) await addIssue(index, "invalidProperties", path, "error", header.error);+ const values = header?.values ?? {};+ const mapped = getMappedNoteProperties(values);+ // Do not let the ordinary single-file import's title fallback hide a limit violation.+ const rawTitle = typeof values.title === "string" ? values.title.trim() : mapped.title ?? title;+ const times: { createdAt?: string; updatedAt?: string } = {};+ for (const [a, b] of [["created", "createdAt"], ["updated", "updatedAt"]] as const) {+ const first = importTime(values[a]);+ const second = importTime(values[b]);+ if (values[a] !== undefined && values[b] !== undefined && (first !== second || !first)) await addIssue(index, "conflictingTimes", path);+ const raw = values[b] ?? values[a];+ times[b] = importTime(raw);+ if (raw !== undefined && !times[b]) await addIssue(index, "invalidTime", path, "warning", String(raw));+ }+ return { title: rawTitle, tags: mapped.tags ?? [], ...times };+};+export const prepareImportNote = async (index: ImportIndex, note: ImportNote) => {+ const tags = [...new Set(note.tags.map(tag => tag.trim().replace(/^#+/, "")).filter(Boolean))];+ note.tags = tags.slice(0, 24);+ if (tags.length > 24) await addIssue(index, "ignoredTags", note.path, "warning", String(tags.length - 24));+ if (note.title.length > 160) await addIssue(index, "titleTooLong", note.path);+ if (!note.createdAt || !note.updatedAt) await addIssue(index, "missingTimes", note.path, "warning");+ for (const resource of note.resources) {+ const maximum = resource.mimeType.startsWith("image/") ? 100 * 1024 * 1024 : 1024 * 1024 * 1024;+ if (resource.byteSize <= 0 || resource.byteSize > maximum) await addIssue(index, "resourceSize", note.path, "error", resource.filename);+ }+};+export const mimeFromName = (name: string) => {+ const extension = name.split(".").at(-1)?.toLowerCase();+ const types: Record<string, string> = { png: "image/png", jpg: "image/jpeg", jpeg: "image/jpeg", gif: "image/gif", webp: "image/webp", svg: "image/svg+xml", avif: "image/avif", bmp: "image/bmp", pdf: "application/pdf", txt: "text/plain", csv: "text/csv", md: "text/markdown", mp3: "audio/mpeg", mp4: "video/mp4", json: "application/json", zip: "application/zip" };+ return extension && Object.hasOwn(types, extension) ? types[extension]! : "application/octet-stream";+};diff --git a/packages/shared/src/note-import/references.test.mjs b/packages/shared/src/note-import/references.test.mjsnew file mode 100644index 00000000..1c7ac7d6--- /dev/null+++ b/packages/shared/src/note-import/references.test.mjs@@ -0,0 +1,32 @@+import { expect, test } from "bun:test";+import { findImportReferences, rewriteImportMarkdown } from "./references";++const cases = [+ ["nested labels", "[section [outer [inner]]](file.txt)", ["file.txt"]],+ ["blockquote label", "> [two\r\n> lines](file.txt)", ["file.txt"]],+ ["list label", "- [two\n lines](file.txt)", ["file.txt"]],+ ["blockquote image", "> ", ["image.png"]],+ ["container reference", "> [one][my\n> label]\n>\n> [my label]: file.txt", ["file.txt"]],+ ["full, collapsed and shortcut uses", "[one][id]\n\n![two][ID]\n\n[id][]\n\n[id]\n\n[id]: <image name.png> \"title\"\n[unused]: missing.png", ["image name.png"]],+ ["unused definition", "[missing]\n\n[unused]: hidden.png", []],+ ["empty inline destination", "[id]()\n\n[id]: unused.png", []],+ ["empty first definition", "[id]\n\n[id]: <>\n[id]: ignored.png", []],+ ["first definition wins", "[id]\n\n[id]: first.txt\n[id]: second.txt", ["first.txt"]],+ ["escaped reference", "[\\*]\n\n[\\*]: escaped.txt", ["escaped.txt"]],+ ["URI escapes and entities", "[named](file%20name.txt#one\\)two&three)", ["file%20name.txt#one)two&three"]],+ ["inert Markdown", "``\n\n```md\n\n```\n\n \n\n<div>\n\n</div>\n\n\\![[escaped.png]]", []],+ ["Wiki in a title", '[x](real.svg "![[not-an-embed.svg]]")', ["real.svg"]],+ ["Wiki in a URL", "[x](https://example.com/![[not-an-embed.svg]])", ["https://example.com/![[not-an-embed.svg]]"]],+ ["Wiki in an autolink", "<https://example.com/![[not-an-embed.svg]]>", ["https://example.com/![[not-an-embed.svg]]"]],+ ["Wiki in a bare GFM URL", "https://example.com/![[not-an-embed.svg]]", []],+ ["GFM task marker", "- [x] checkbox\n\n[x]: unused.png", []],+];+for (const [name, source, hrefs] of cases) test(`destination spans preserve ${name}`, () => {+ expect(findImportReferences(source).map(ref => ref.href)).toEqual(hrefs);+});++test("target edits retain container prefixes and multiline labels", () => {+ const source = '> [two\r\n> lines](file.txt "title")';+ const refs = findImportReferences(source).map(ref => ({ ...ref, resourceKey: "file" }));+ expect(rewriteImportMarkdown(source, refs, { file: "/resource" })).toBe('> [two\r\n> lines](/resource "title")');+});diff --git a/packages/shared/src/note-import/references.ts b/packages/shared/src/note-import/references.tsnew file mode 100644index 00000000..8541b625--- /dev/null+++ b/packages/shared/src/note-import/references.ts@@ -0,0 +1,83 @@+import { parse, postprocess, preprocess } from "micromark";+import { gfm } from "micromark-extension-gfm";+import { decodeString } from "micromark-util-decode-string";+import { normalizeIdentifier } from "micromark-util-normalize-identifier";+import { splitMarkdownFrontMatter } from "../front-matter";+import type { ImportReference } from "./types";++type ParseEvent = ReturnType<typeof postprocess>[number];+type Frame = { kind: string; label?: string; reference?: string; inline?: boolean; destination?: ImportReference };+const extensions = [gfm()];+const parseEvents = (body: string) => postprocess(parse({ extensions }).document().write(preprocess()(body, undefined, true)));+const protectedTypes = new Set(["codeFenced", "codeIndented", "codeText", "htmlFlow", "htmlText", "characterEscape", "autolink", "literalAutolink", "resourceDestination", "definitionDestination", "resourceTitle", "definitionTitle"]);++/** Use CommonMark's concrete destination tokens, not whole-link string matches. */+export const findImportReferences = (markdown: string): ImportReference[] => {+ const body = splitMarkdownFrontMatter(markdown)?.body ?? markdown;+ const offset = markdown.length - body.length;+ let events = parseEvents(body);+ const protectedRanges = events.filter(([action, token]) => action === "enter" && protectedTypes.has(token.type)).map(([, token]) => [token.start.offset, token.end.offset]);+ const result = new Map<number, ImportReference>();+ const wikis: Array<[number, number]> = [];+ for (const match of body.matchAll(/!\[\[([^\]\r\n]+)\]\]/g)) {+ const start = match.index, end = start + match[0].length;+ if (protectedRanges.some(([from, to]) => start < to! && end > from!)) continue;+ const [href, label] = match[1]!.split("|");+ wikis.push([start, end]);+ result.set(start + offset, { start: start + offset, end: end + offset, href: href!, label: label || href!, image: true, wiki: true });+ }+ // Wiki embeds are a separate syntax. Hide their contents from CommonMark+ // without changing offsets or introducing whitespace/block constructs.+ if (wikis.length) {+ const parts: string[] = [];+ let from = 0;+ for (const [start, end] of wikis) { parts.push(body.slice(from, start), "x".repeat(end - start)); from = end; }+ parts.push(body.slice(from));+ events = parseEvents(parts.join(""));+ }+ const destination = (token: ParseEvent[1], context: ParseEvent[2], image: boolean, email = false): ImportReference | undefined => {+ const href = (email ? "mailto:" : "") + decodeString(context.sliceSerialize(token));+ if (!href) return;+ return { start: offset + token.start.offset!, end: offset + token.end.offset!, href, label: "", image, wiki: false, targetOnly: true };+ };+ const frames: Frame[] = [];+ const definitions = new Map<string, ImportReference | undefined>(), used = new Set<string>();+ for (const [action, token, context] of events) {+ if (action === "enter") {+ if (token.type === "link" || token.type === "image" || token.type === "definition") frames.push({ kind: token.type });+ const frame = frames.at(-1);+ if (frame && token.type === "resource") frame.inline = true;+ if (frame && (token.type === "resourceDestinationString" || token.type === "definitionDestinationString")) frame.destination = destination(token, context, frame.kind === "image");+ if (token.type === "autolinkProtocol" || token.type === "autolinkEmail") {+ const ref = destination(token, context, false, token.type === "autolinkEmail");+ if (ref) result.set(ref.start, ref);+ }+ } else {+ const frame = frames.at(-1);+ if (!frame) continue;+ if (token.type === "labelText" || token.type === "definitionLabelString") frame.label = normalizeIdentifier(context.sliceSerialize(token));+ if (token.type === "referenceString") frame.reference = normalizeIdentifier(context.sliceSerialize(token));+ if (token.type === "link" || token.type === "image" || token.type === "definition") {+ frames.pop();+ if (frame.kind === "definition") {+ if (frame.label && !definitions.has(frame.label)) definitions.set(frame.label, frame.destination);+ } else if (frame.destination) result.set(frame.destination.start, frame.destination);+ else if (!frame.inline && (frame.reference || frame.label)) used.add((frame.reference || frame.label)!);+ }+ }+ }+ for (const id of used) { const ref = definitions.get(id); if (ref) result.set(ref.start, ref); }+ return [...result.values()].sort((a, b) => a.start - b.start);+};+export const rewriteImportMarkdown = (markdown: string, refs: ImportReference[], urls: Record<string, string>) => {+ for (const ref of [...refs].sort((a, b) => b.start - a.start)) {+ const url = ref.resourceKey && Object.hasOwn(urls, ref.resourceKey) ? urls[ref.resourceKey] : undefined;+ if (!url) continue;+ const suffix = (ref.suffix ?? "").replace(/[()]/g, char => char === "(" ? "%28" : "%29");+ const target = `${url}${suffix}`;+ const label = ref.label.replace(/(?<!\\)\]/g, "\\]");+ const replacement = ref.targetOnly ? target : `${ref.image ? "!" : ""}[${label}](${target})`;+ markdown = markdown.slice(0, ref.start) + replacement + markdown.slice(ref.end);+ }+ return markdown;+};diff --git a/packages/shared/src/note-import/session.ts b/packages/shared/src/note-import/session.tsnew file mode 100644index 00000000..9ebd0a14--- /dev/null+++ b/packages/shared/src/note-import/session.ts@@ -0,0 +1,330 @@+import type { ImportDocument, ImportFile, ImportFormat, ImportIndex, ImportIssue, ImportNote, ImportNotebook, ImportReference, ImportResource, ImportSummary, SourceEntry } from "./types";+import { IMPORT_CHUNK_BYTES } from "./types";+import { indexItems } from "./index-store";+import { addIssue, basename, dirname, ImportSources, normalPath, stem } from "./sources";+import { importTime, mimeFromName, readMarkdownMetadata, prepareImportNote } from "./metadata";+import { findImportReferences } from "./references";+import { readEnex, type EnexNote } from "./evernote";++type RawItem = { key: string; entryKey: string; title: string; props: Record<string, string> };+const readRaw = (text: string) => {+ const marker = /(?:^|\r?\n\r?\n)(?=id: [a-f0-9]{32}\r?\n)/ig;+ const matches = [...text.matchAll(marker)];+ const last = matches.at(-1);+ if (!last || last.index === undefined) return;+ const props: Record<string, string> = {};+ for (const line of text.slice(last.index + last[0].length).trimEnd().split(/\r?\n/)) {+ const match = /^([a-z_]+): ?(.*)$/.exec(line);+ if (!match) return;+ props[match[1]!] = match[2]!;+ }+ if (!/^[0-9a-f]{32}$/i.test(props.id ?? "") || !/^\d+$/.test(props.type_ ?? "")) return;+ const content = text.slice(0, last.index);+ const firstLine = content.indexOf("\n");+ return { props, title: (firstLine === -1 ? content : content.slice(0, firstLine)).trim(), body: firstLine === -1 ? "" : content.slice(firstLine + 1).replace(/^\r?\n/, "") };+};++export class NoteImportSession {+ readonly id: string;+ readonly sources: ImportSources;+ private enexReaders = new Map<string, { iterator: AsyncGenerator<EnexNote>; ordinal: number }>();+ private prepared?: ImportDocument;+ private staged = new Set<string>();+ private nextNote = 0;+ constructor(readonly format: ImportFormat, files: ImportFile[], readonly index: ImportIndex, id: string = crypto.randomUUID()) {+ this.id = id;+ this.sources = new ImportSources(files, index);+ }+ private async notebook(path: string) {+ path ||= this.format === "obsidian" ? "Obsidian" : "Joplin";+ let current = "";+ for (const name of path.split("/")) {+ const parent = current;+ current = parent ? `${parent}/${name}` : name;+ if (!await this.index.get("notebook", current)) {+ await this.index.put<ImportNotebook>("notebook", current, { key: current, name, parentKey: parent || null });+ if (!name.trim() || name.length > 80) await addIssue(this.index, "notebookName", current);+ }+ }+ return path;+ }+ private async saveNote(note: ImportNote) {+ note.key = `note-${String(this.nextNote++).padStart(12, "0")}`;+ await prepareImportNote(this.index, note);+ await this.index.put("note", note.key, note);+ await this.index.put("alias", `note:${note.entryKey}`, note.key);+ }+ private async rawBinaryId(entry: SourceEntry) {+ const id = /^([0-9a-f]{32})(?:\.[^.]+)?$/i.exec(basename(entry.path))?.[1];+ return id && (await this.index.get<SourceEntry>("rawBinary", id))?.key === entry.key ? id : undefined;+ }+ private async isRawPayload(entry: SourceEntry) {+ const id = await this.rawBinaryId(entry);+ const descriptor = id ? await this.index.get<RawItem>("raw", id) : undefined;+ return descriptor?.props.type_ === "4" && descriptor.entryKey !== entry.key;+ }+ private async indexRaw(entry: SourceEntry, signal?: AbortSignal) {+ const raw = readRaw(await this.sources.text(entry, signal));+ if (raw) {+ await this.index.put<RawItem>("raw", raw.props.id!, { key: raw.props.id!, entryKey: entry.key, title: raw.title, props: raw.props });+ await this.index.put("alias", `raw:${entry.key}`, true);+ }+ }+ async scan(signal?: AbortSignal, progress?: (path: string) => void): Promise<ImportSummary> {+ try {+ await this.sources.scan(signal, progress);+ for await (const entry of indexItems<SourceEntry>(this.index, "entry")) {+ signal?.throwIfAborted(); progress?.(entry.path);+ if (this.format === "evernote") {+ if (/\.notes$/i.test(entry.path)) { await addIssue(this.index, "notesFormat", entry.path); continue; }+ if (!/\.enex$/i.test(entry.path)) continue;+ const notebookKey = await this.notebook(entry.path.replace(/\.enex$/i, ""));+ try {+ for await (const raw of readEnex(this.sources.bytes(entry, signal), entry.key)) {+ const resources = raw.resources.filter((res, i, all) => all.findIndex(other => other.hash === res.hash) === i);+ const note: ImportNote = { key: "", path: `${entry.path} / ${raw.title}`, notebookKey, title: raw.title.trim() || stem(entry.path), tags: [...new Set(raw.tags.filter(Boolean))], createdAt: importTime(raw.created), updatedAt: importTime(raw.updated), resources, entryKey: entry.key, ordinal: raw.ordinal };+ for (const match of raw.content.matchAll(/<en-media\b[^>]*\bhash=["']([^"']+)["']/g)) if (!resources.some(res => res.hash === match[1])) await addIssue(this.index, "missingResource", note.path, "error", match[1]);+ await this.saveNote(note);+ }+ } catch (error) { signal?.throwIfAborted(); await addIssue(this.index, "invalidEnex", entry.path, "error", String(error)); }+ } else if (/\.(?:md|markdown)$/i.test(entry.path)) {+ if (this.format === "joplin") {+ if (!await this.rawBinaryId(entry)) await this.indexRaw(entry, signal);+ } else {+ const markdown = await this.sources.text(entry, signal);+ const meta = await readMarkdownMetadata(markdown, stem(entry.path), this.index, entry.path);+ const note: ImportNote = { key: "", path: entry.path, entryKey: entry.key, notebookKey: await this.notebook(dirname(entry.path)), resources: [], ...meta };+ await this.saveNote(note);+ }+ }+ }+ if (this.format === "joplin") {+ // Resource-folder Markdown may be an attachment, a renamed RAW record,+ // or an ordinary note. Read it only after other RAW records identify payloads.+ for await (const entry of indexItems<SourceEntry>(this.index, "entry")) {+ signal?.throwIfAborted();+ if (/\.(?:md|markdown)$/i.test(entry.path) && await this.rawBinaryId(entry) && !await this.isRawPayload(entry)) await this.indexRaw(entry, signal);+ }+ await this.scanRaw(signal);+ for await (const entry of indexItems<SourceEntry>(this.index, "entry")) {+ signal?.throwIfAborted();+ if (!/\.(?:md|markdown)$/i.test(entry.path)) continue;+ if (await this.index.get("alias", `raw:${entry.key}`) || await this.isRawPayload(entry)) continue;+ const markdown = await this.sources.text(entry, signal);+ const meta = await readMarkdownMetadata(markdown, stem(entry.path), this.index, entry.path);+ await this.saveNote({ key: "", path: entry.path, entryKey: entry.key, notebookKey: await this.notebook(dirname(entry.path)), resources: [], ...meta });+ }+ }+ if (this.format !== "evernote") {+ for await (const note of indexItems<ImportNote>(this.index, "note")) {+ signal?.throwIfAborted();+ const doc = await this.readNote(note.key, signal);+ const refs = findImportReferences(doc.contentMarkdown);+ const resources = new Map<string, ImportResource>();+ for (const ref of refs) {+ const resource = await this.resolveReference(note, ref);+ if (resource) {+ ref.resourceKey = resource.key; resources.set(resource.key, resource);+ if (ref.wiki && !resource.mimeType.startsWith("image/")) {+ ref.image = false;+ if (ref.label === ref.href) ref.label = resource.filename;+ }+ }+ }+ note.resources = [...resources.values()]; note.references = refs;+ // Validate resources here; metadata was validated before reference resolution.+ for (const resource of note.resources) {+ const max = resource.mimeType.startsWith("image/") ? 100 * 1024 * 1024 : 1024 * 1024 * 1024;+ if (resource.byteSize <= 0 || resource.byteSize > max) await addIssue(this.index, "resourceSize", note.path, "error", resource.filename);+ await this.index.put("alias", `used:${resource.entryKey}`, true);+ }+ await this.index.put("note", note.key, note);+ }+ }+ if (!await this.index.count("note")) await addIssue(this.index, "noNotes", "");+ if (this.format !== "evernote") for await (const entry of indexItems<SourceEntry>(this.index, "entry")) {+ if (!/\.(?:md|markdown)$/i.test(entry.path) && !await this.index.get("alias", `used:${entry.key}`)) await addIssue(this.index, "unusedResource", entry.path, "warning");+ }+ } catch (error) {+ signal?.throwIfAborted();+ await addIssue(this.index, "invalidArchive", "", "error", error instanceof Error ? error.message : String(error));+ }+ let resources = 0, errors = 0, warnings = 0;+ for await (const note of indexItems<ImportNote>(this.index, "note")) resources += note.resources.length;+ for await (const issue of indexItems<ImportIssue>(this.index, "issue")) {+ if (issue.severity === "error") errors++;+ else warnings++;+ }+ return { id: this.id, format: this.format, notes: await this.index.count("note"), notebooks: await this.index.count("notebook"), resources, errors, warnings };+ }+ private async rawFolder(id: string) {+ if (!id) return this.notebook("");+ const chain: RawItem[] = [];+ const visited = new Set<string>();+ while (id) {+ if (visited.has(id)) throw new Error("cyclicNotebook");+ visited.add(id);+ const item = await this.index.get<RawItem>("raw", id);+ // Selected-note/subtree exports retain IDs of excluded ancestors.+ if (!item) break;+ if (item.props.type_ !== "2") throw new Error("missingNotebook");+ chain.push(item); id = item.props.parent_id ?? "";+ }+ let parentKey: string | null = null;+ for (const item of chain.reverse()) {+ // Hierarchical ID keys preserve distinct names and parent-first pagination.+ const key: string = parentKey ? `${parentKey}/${item.key}` : `joplin:${item.key}`;+ if (!await this.index.get("notebook", key)) {+ await this.index.put<ImportNotebook>("notebook", key, { key, name: item.title, parentKey });+ if (!item.title.trim() || item.title.length > 80) await addIssue(this.index, "notebookName", item.title);+ }+ parentKey = key;+ }+ return parentKey ?? this.notebook("");+ }+ private async scanRaw(signal?: AbortSignal) {+ for await (const item of indexItems<RawItem>(this.index, "raw")) {+ signal?.throwIfAborted();+ if (item.props.type_ === "6") {+ const tag = await this.index.get<RawItem>("raw", item.props.tag_id!);+ if (!tag) { await addIssue(this.index, "missingTag", item.entryKey); continue; }+ const tags = await this.index.get<string[]>("rawTag", item.props.note_id!) ?? [];+ await this.index.put("rawTag", item.props.note_id!, [...tags, tag.title]);+ }+ }+ for await (const item of indexItems<RawItem>(this.index, "raw")) {+ signal?.throwIfAborted();+ try {+ if (item.props.type_ === "2") await this.rawFolder(item.key);+ if (item.props.type_ !== "1") continue;+ const note: ImportNote = {+ key: "", path: item.title || item.entryKey, entryKey: item.entryKey,+ notebookKey: await this.rawFolder(item.props.parent_id ?? ""), title: item.title,+ tags: await this.index.get<string[]>("rawTag", item.key) ?? [],+ createdAt: importTime(item.props.user_created_time || item.props.created_time),+ updatedAt: importTime(item.props.user_updated_time || item.props.updated_time),+ resources: [], raw: true,+ };+ await this.saveNote(note);+ } catch (error) { await addIssue(this.index, "invalidJoplin", item.entryKey, "error", String(error)); }+ }+ }+ private async resolveReference(note: ImportNote, ref: ImportReference): Promise<ImportResource | undefined> {+ let href = ref.href;+ if (/^(?:https?:|mailto:|tel:|data:|#|\/\/)/i.test(href)) return;+ if (this.format === "joplin" && /^:\/[0-9a-f]{32}/i.test(href)) {+ const suffixAt = ref.wiki ? href.indexOf("#") : href.search(/[?#]/);+ ref.suffix = suffixAt === -1 ? "" : href.slice(suffixAt);+ const id = href.slice(2, suffixAt === -1 ? undefined : suffixAt);+ const item = await this.index.get<RawItem>("raw", id);+ if (item?.props.type_ === "1") { await addIssue(this.index, "noteLink", note.path, "warning", href); return; }+ const binary = await this.index.get<SourceEntry>("rawBinary", id);+ if (!binary) { await addIssue(this.index, "missingResource", note.path, "error", href); return; }+ return { key: binary.key, entryKey: binary.key, filename: item?.title || basename(binary.path), mimeType: item?.props.mime || mimeFromName(binary.path), byteSize: binary.size };+ }+ try {+ const suffixAt = ref.wiki ? href.indexOf("#") : href.search(/[?#]/);+ ref.suffix = suffixAt === -1 ? "" : href.slice(suffixAt);+ href = suffixAt === -1 ? href : href.slice(0, suffixAt);+ if (!ref.wiki) href = decodeURIComponent(href.replace(/%(?![0-9a-f]{2})/ig, "%25"));+ if (!href || /^[a-z][a-z0-9+.-]*:/i.test(href)) throw new Error("outsideRoot");+ const relative = normalPath(href.startsWith("/") ? href.slice(1) : [dirname(note.entryKey), href].filter(Boolean).join("/"));+ const explicitRelative = /^\.\.?\//.test(href);+ const source = this.format === "obsidian" ? await this.index.get<SourceEntry>("entry", note.entryKey) : undefined;+ const prefix = source ? this.sources.archiveRoot(source) : "";+ const root = !explicitRelative && (ref.wiki || this.format === "obsidian") ? normalPath([prefix, href.replace(/^\//, "")].filter(Boolean).join("/")) : undefined;+ const rootFirst = this.format === "obsidian" && !explicitRelative && href.includes("/");+ let entry = await this.index.get<SourceEntry>("entry", rootFirst ? root! : relative);+ if (!entry && root) entry = await this.index.get<SourceEntry>("entry", rootFirst ? relative : root);+ if (!entry && ref.wiki && !href.includes("/")) {+ const candidates = await this.index.get<string[]>("alias", href) ?? [];+ if (candidates.length > 1) { await addIssue(this.index, "ambiguousResource", note.path, "error", href); return; }+ if (candidates[0]) entry = await this.index.get<SourceEntry>("entry", candidates[0]);+ }+ if (!entry && ref.wiki && !/\.[^/]+$/.test(href)) {+ await addIssue(this.index, "noteLink", note.path, "warning", href); return;+ }+ if (!entry && /\.(?:md|markdown)$/i.test(href)) { await addIssue(this.index, "noteLink", note.path, "warning", href); return; }+ if (!entry) { await addIssue(this.index, "missingResource", note.path, "error", ref.href); return; }+ if (await this.index.get("alias", `note:${entry.key}`)) { await addIssue(this.index, "noteLink", note.path, "warning", href); return; }+ return { key: entry.key, entryKey: entry.key, filename: basename(entry.path), mimeType: mimeFromName(entry.path), byteSize: entry.size };+ } catch { await addIssue(this.index, "outsideRoot", note.path, "error", ref.href); }+ }+ async readNote(key: string, signal?: AbortSignal): Promise<ImportDocument> {+ signal?.throwIfAborted();+ if (this.prepared?.note.key === key) return this.prepared;+ const note = await this.index.get<ImportNote>("note", key);+ if (!note) throw new Error("missingNote");+ const entry = await this.index.get<SourceEntry>("entry", note.entryKey);+ if (!entry) throw new Error("missingNote");+ if (this.format === "evernote") {+ let reader = this.enexReaders.get(entry.key);+ if (!reader || reader.ordinal >= note.ordinal!) {+ await reader?.iterator.return(undefined);+ reader = { iterator: readEnex(this.sources.bytes(entry, signal), entry.key, this.index, note.ordinal), ordinal: -1 };+ this.enexReaders.set(entry.key, reader);+ }+ let raw: EnexNote | undefined;+ while (reader.ordinal < note.ordinal!) {+ const next = await reader.iterator.next();+ if (next.done) throw new Error("missingNote");+ raw = next.value; reader.ordinal = raw.ordinal;+ }+ if (!raw) throw new Error("missingNote");+ const retained = new Set(note.resources.map(resource => resource.key));+ for (const resource of raw.resources) if (!retained.has(resource.key)) {+ for (let i = 0; i < (resource.chunks ?? 0); i++) await this.index.remove("chunk", `${resource.key}:${i}`);+ }+ this.prepared = { note: { ...note, resources: note.resources.map(res => ({ ...res, chunks: raw!.resources.find(item => item.key === res.key)?.chunks ?? 0 })) }, contentMarkdown: "", enml: raw.content };+ return this.prepared;+ }+ const text = await this.sources.text(entry, signal);+ return { note, contentMarkdown: note.raw ? readRaw(text)!.body : text };+ }+ async readResourcePart(resource: ImportResource, start: number, end: number, signal?: AbortSignal): Promise<Uint8Array<ArrayBuffer>> {+ signal?.throwIfAborted();+ if (!resource.enex && !this.staged.has(resource.key)) {+ const entry = await this.index.get<SourceEntry>("entry", resource.entryKey!);+ if (!entry) throw new Error("missingResource");+ let chunk = new Uint8Array(IMPORT_CHUNK_BYTES), used = 0, number = 0;+ for await (const bytes of this.sources.bytes(entry, signal)) {+ let at = 0;+ while (at < bytes.length) {+ const length = Math.min(chunk.length - used, bytes.length - at);+ chunk.set(bytes.subarray(at, at + length), used); at += length; used += length;+ if (used === chunk.length) { await this.index.put("chunk", `${resource.key}:${number++}`, chunk); chunk = new Uint8Array(IMPORT_CHUNK_BYTES); used = 0; }+ }+ }+ if (used) await this.index.put("chunk", `${resource.key}:${number++}`, chunk.slice(0, used));+ this.staged.add(resource.key);+ }+ const result = new Uint8Array(end - start);+ let cursor = start;+ while (cursor < end) {+ const number = Math.floor(cursor / IMPORT_CHUNK_BYTES);+ const chunk = await this.index.get<Uint8Array>("chunk", `${resource.key}:${number}`);+ if (!chunk) throw new Error("missingResource");+ const at = cursor % IMPORT_CHUNK_BYTES;+ const length = Math.min(chunk.length - at, end - cursor);+ if (length <= 0) throw new Error("missingResource");+ result.set(chunk.subarray(at, at + length), cursor - start); cursor += length;+ }+ return result;+ }+ async releaseNote(note: ImportNote) {+ for (const res of note.resources) {+ for (let i = 0; i < Math.ceil(res.byteSize / IMPORT_CHUNK_BYTES); i++) await this.index.remove("chunk", `${res.key}:${i}`);+ this.staged.delete(res.key);+ }+ if (this.prepared?.note.key === note.key) this.prepared = undefined;+ }+ async finishReading() {+ for (const reader of this.enexReaders.values()) await reader.iterator.return(undefined).catch(() => {});+ this.enexReaders.clear(); this.prepared = undefined; this.staged.clear();+ await this.index.clearKind("chunk");+ }+ async close() {+ await this.finishReading();+ await this.sources.close(); await this.index.clear();+ }+}diff --git a/packages/shared/src/note-import/sources.ts b/packages/shared/src/note-import/sources.tsnew file mode 100644index 00000000..15f2e6fd--- /dev/null+++ b/packages/shared/src/note-import/sources.ts@@ -0,0 +1,141 @@+import { BlobReader, ZipReader, type Entry } from "@zip.js/zip.js/index-native.js";+import { extract } from "it-tar";+import type { ImportFile, ImportIndex, ImportIssue, SourceEntry } from "./types";+import { IMPORT_CHUNK_BYTES } from "./types";++export const basename = (path: string) => path.split("/").at(-1) ?? path;+export const dirname = (path: string) => path.includes("/") ? path.slice(0, path.lastIndexOf("/")) : "";+export const stem = (path: string) => basename(path).replace(/\.[^.]+$/, "");+export const normalPath = (path: string) => {+ if (/^(?:[a-z]:|[\\/])/i.test(path) || path.includes("\0")) throw new Error("outsideRoot");+ const parts: string[] = [];+ for (const part of path.replaceAll("\\", "/").split("/")) {+ if (!part || part === ".") continue;+ if (part === "..") { if (!parts.length) throw new Error("outsideRoot"); parts.pop(); }+ else parts.push(part);+ }+ return parts.join("/");+};+const ignored = (path: string) => path.split("/").some(part => [".obsidian", ".git", ".trash", "__MACOSX", ".DS_Store", "Thumbs.db"].includes(part));+export const addIssue = async (index: ImportIndex, code: string, path: string, severity: ImportIssue["severity"] = "error", detail?: string) => {+ const key = crypto.randomUUID();+ await index.put("issue", key, { key, code, path, severity, ...(detail ? { detail } : {}) });+};++export class ImportSources {+ private zips = new Map<number, { reader: ZipReader<Blob>; entries: Map<string, Entry> }>();+ private zipRoots = new Map<number, string>();+ constructor(private files: ImportFile[], private index: ImportIndex) {}+ private async add(entry: SourceEntry) {+ let path: string;+ try { path = normalPath(entry.path); } catch { await addIssue(this.index, "outsideRoot", entry.path); return; }+ if (!path) return;+ // A Vault's configuration locates its root without importing config files.+ if (ignored(path)) return path.split("/").includes(".obsidian") ? path : undefined;+ if (await this.index.get("entry", path)) { await addIssue(this.index, "duplicatePath", path); return; }+ entry = { ...entry, key: path, path };+ await this.index.put("entry", path, entry);+ const name = basename(path);+ const aliases = await this.index.get<string[]>("alias", name) ?? [];+ await this.index.put("alias", name, [...aliases, path]);+ const id = /^([0-9a-f]{32})(?:\.[^.]+)?$/i.exec(name)?.[1];+ if (id && (!/\.md$/i.test(name) || /(?:^|\/)_?resources\//i.test(path))) await this.index.put("rawBinary", id, entry);+ return path;+ }+ archiveRoot(entry: SourceEntry) { return this.zipRoots.get(entry.fileIndex) ?? ""; }+ async scan(signal?: AbortSignal, progress?: (path: string) => void) {+ for (const [fileIndex, input] of this.files.entries()) {+ signal?.throwIfAborted();+ progress?.(input.path);+ if (input.expandArchive !== false && /\.zip$/i.test(input.path)) {+ const reader = new ZipReader(new BlobReader(input.file), { useWebWorkers: false });+ const entries = new Map<string, Entry>();+ this.zips.set(fileIndex, { reader, entries });+ let root: string | undefined;+ for await (const entry of reader.getEntriesGenerator()) {+ signal?.throwIfAborted();+ if (entry.directory) continue;+ if (entry.unixMode && (entry.unixMode & 0xf000) === 0xa000) { await addIssue(this.index, "symlink", entry.filename); continue; }+ entries.set(entry.filename, entry);+ const path = await this.add({ key: entry.filename, path: entry.filename, size: entry.uncompressedSize, fileIndex, archivePath: entry.filename });+ if (path) {+ const top = path.includes("/") ? path.split("/")[0]! : "";+ root = root === undefined ? top : root === top ? root : "";+ }+ }+ this.zipRoots.set(fileIndex, root ?? "");+ } else if (input.expandArchive !== false && /\.jex$/i.test(input.path)) {+ // Feed 512-byte records until it-tar has decoded the header and+ // prefetched its first 512 payload bytes. Then drain the body in large,+ // record-aligned chunks. The cursor gives a lazy Blob range without+ // duplicating the codec or spooling the entire JEX to disk.+ let cursor = 0;+ let chunkSize = 512;+ let boundary = input.file.size;+ const source = async function* () {+ while (cursor < input.file.size) {+ signal?.throwIfAborted();+ const end = Math.min(input.file.size, cursor + chunkSize, boundary);+ if (end <= cursor) throw new Error("invalidArchive");+ const bytes = new Uint8Array(await input.file.slice(cursor, end).arrayBuffer());+ cursor = end;+ yield bytes;+ }+ };+ for await (const entry of extract({ highWaterMark: 512 })(source())) {+ const size = entry.header.size ?? 0;+ const offset = size > 0 ? cursor - 512 : cursor;+ chunkSize = 1024 * 1024;+ boundary = offset + Math.ceil(size / 512) * 512;+ let received = 0;+ for await (const chunk of entry.body) received += chunk.length;+ if (received !== size) throw new Error("invalidArchive");+ chunkSize = 512;+ boundary = input.file.size;+ if (entry.header.type === "directory") continue;+ if (entry.header.type !== "file") { await addIssue(this.index, "symlink", entry.header.name); continue; }+ await this.add({ key: entry.header.name, path: entry.header.name, size, fileIndex, offset });+ }+ } else await this.add({ key: input.path, path: input.path, size: input.file.size, fileIndex });+ }+ }+ async *bytes(entry: SourceEntry, signal?: AbortSignal): AsyncGenerator<Uint8Array<ArrayBuffer>> {+ signal?.throwIfAborted();+ if (entry.archivePath !== undefined) {+ const found = this.zips.get(entry.fileIndex)?.entries.get(entry.archivePath);+ if (!found || found.directory) throw new Error("missingResource");+ let output!: TransformStreamDefaultController<Uint8Array>;+ const stream = new TransformStream<Uint8Array, Uint8Array>({ start(controller) { output = controller; } });+ const abort = () => output.error(signal!.reason);+ signal?.addEventListener("abort", abort, { once: true });+ const writing = found.getData(stream.writable, { signal }).then(() => ({ error: null as unknown })).catch(error => { output.error(error); return { error }; });+ const reader = stream.readable.getReader();+ try {+ while (true) {+ signal?.throwIfAborted();+ const next = await reader.read();+ if (next.done) break;+ // Normalize buffers at the worker/Blob boundary.+ yield new Uint8Array(next.value);+ }+ const result = await writing;+ if (result.error) throw result.error;+ } finally { signal?.removeEventListener("abort", abort); await reader.cancel().catch(() => {}); reader.releaseLock(); await writing; }+ } else {+ const file = this.files[entry.fileIndex]!.file;+ for (let start = 0; start < entry.size; start += IMPORT_CHUNK_BYTES) {+ signal?.throwIfAborted();+ const offset = entry.offset ?? 0;+ yield new Uint8Array(await file.slice(offset + start, offset + Math.min(entry.size, start + IMPORT_CHUNK_BYTES)).arrayBuffer());+ }+ }+ }+ async text(entry: SourceEntry, signal?: AbortSignal) {+ const decoder = new TextDecoder();+ const parts: string[] = [];+ for await (const bytes of this.bytes(entry, signal)) parts.push(decoder.decode(bytes, { stream: true }));+ parts.push(decoder.decode());+ return parts.join("").replace(/^\uFEFF/, "");+ }+ async close() { await Promise.all([...this.zips.values()].map(zip => zip.reader.close())); this.zips.clear(); this.zipRoots.clear(); }+}diff --git a/packages/shared/src/note-import/types.ts b/packages/shared/src/note-import/types.tsnew file mode 100644index 00000000..7ffd4f29--- /dev/null+++ b/packages/shared/src/note-import/types.ts@@ -0,0 +1,47 @@+import type { MemoDetail } from "../types";++export type ImportFormat = "evernote" | "obsidian" | "joplin";+export type ImportFile = { path: string; file: File; expandArchive?: boolean };+export type ImportIssue = { key: string; severity: "error" | "warning"; code: string; path: string; detail?: string };+export type SourceEntry = { key: string; path: string; size: number; fileIndex: number; archivePath?: string; offset?: number };+export type ImportNotebook = { key: string; name: string; parentKey: string | null };+export type ImportReference = { start: number; end: number; href: string; label: string; image: boolean; wiki: boolean; resourceKey?: string; suffix?: string; targetOnly?: boolean };+export type ImportResource = { key: string; filename: string; mimeType: string; byteSize: number; entryKey?: string; enex?: boolean; hash?: string; chunks?: number };+export type ImportNote = {+ key: string; path: string; notebookKey: string; title: string; tags: string[];+ createdAt?: string; updatedAt?: string; entryKey: string; ordinal?: number;+ resources: ImportResource[]; references?: ImportReference[]; raw?: boolean;+};+export type ImportDocument = { note: ImportNote; contentMarkdown: string; enml?: string };+export type ImportSummary = { id: string; format: ImportFormat; notebooks: number; notes: number; resources: number; errors: number; warnings: number };+export type ImportKind = "entry" | "alias" | "note" | "notebook" | "issue" | "raw" | "rawTag" | "rawBinary" | "chunk" | "result" | "folderResult";+export interface ImportIndex {+ put<T>(kind: ImportKind, key: string, value: T): Promise<void>;+ get<T>(kind: ImportKind, key: string): Promise<T | undefined>;+ page<T>(kind: ImportKind, offset?: number, limit?: number): Promise<T[]>;+ count(kind: ImportKind): Promise<number>;+ remove(kind: ImportKind, key: string): Promise<void>;+ clearKind(kind: ImportKind): Promise<void>;+ clear(): Promise<void>;+}+export interface ImportReader {+ id: string; format: ImportFormat; index: ImportIndex;+ readNote(key: string, signal?: AbortSignal): Promise<ImportDocument>;+ readResourcePart(resource: ImportResource, start: number, end: number, signal?: AbortSignal): Promise<Uint8Array<ArrayBuffer>>;+ releaseNote(note: ImportNote): Promise<void>;+ finishReading?(): Promise<void>;+}+export type ImportMemosInput = {+ importId: string; format: ImportFormat; notebookId: string; dryRun?: boolean;+ items: Array<{ externalId: string; title?: string; contentMarkdown?: string; tags?: string[]; createdAt?: string; updatedAt?: string }>;+};+export type ImportMemosResponse = {+ created: number; skipped: number; failed: number; wouldCreate: number;+ results: Array<{ externalId: string; status: "created" | "skipped" | "failed" | "would_create"; memo?: MemoDetail | null; error?: string }>;+};+export type ImportNoteResult = {+ key: string; path: string; memo: Pick<MemoDetail, "id" | "notebookId" | "revision" | "contentHash" | "createdAt" | "updatedAt">; urls: Record<string, string>;+ complete: boolean; error?: string; uncertain?: boolean; intendedMarkdown?: string;+};+export type ImportRunResult = { completed: number; failed: number; resources: number; cancelled: boolean; syncPending: boolean; failure?: { key: string; path: string; message: string; uncertain: boolean } };+export const IMPORT_CHUNK_BYTES = 64 * 1024;diff --git a/packages/shared/src/pdf-attachment.ts b/packages/shared/src/pdf-attachment.tsindex eed2469b..407735e4 100644--- a/packages/shared/src/pdf-attachment.ts+++ b/packages/shared/src/pdf-attachment.ts@@ -1,7 +1,8 @@import { mergeAttributes, Node } from "@tiptap/core";import type { TiptapDoc, TiptapMark, TiptapNode, TiptapTextNode } from "./content";-import { getAttachmentFilenameFromLabel } from "./resource-links";+import { getAttachmentFilenameFromLabel, resourceLinkToMarkdown } from "./resource-links";import { normalizeAttachmentByteSize } from "./attachment-metadata";+import { decodeString } from "micromark-util-decode-string";export const PDF_ATTACHMENT_NODE_TYPE = "edgeeverPdfAttachment" as const;export const PDF_DISPLAY_MODES = ["compact", "inline"] as const;@@ -10,14 +11,11 @@ export type PdfDisplayMode = (typeof PDF_DISPLAY_MODES)[number];export const resolvePdfDisplayMode = (value: unknown): PdfDisplayMode =>value === "inline" ? "inline" : "compact";-const PDF_LINK_PATTERN = /^\[([^\]]+)\]\(([^\s)]+)(?:\s+"[^"]*")?\)/;+const PDF_LINK_PATTERN = /^\[((?:\\.|[^[\]\\])+)\]\(([^\s)]+)(?:\s+"[^"]*")?\)/;export const isPdfAttachment = (mimeType: string | null | undefined, filename: string | null | undefined) =>mimeType?.toLowerCase() === "application/pdf" || filename?.trim().toLowerCase().endsWith(".pdf") === true;-const escapeMarkdownLabel = (value: string) =>- value.replace(/\\/g, "\\\\").replace(/\[/g, "\\[").replace(/\]/g, "\\]");-export const PdfAttachment = Node.create({name: PDF_ATTACHMENT_NODE_TYPE,group: "inline",@@ -92,7 +90,7 @@ export const PdfAttachment = Node.create({},}),- renderMarkdown: (node) => `[${escapeMarkdownLabel(String(node.attrs?.label || "PDF"))}](${String(node.attrs?.url || "")})`,+ renderMarkdown: (node) => resourceLinkToMarkdown(String(node.attrs?.label || "PDF"), String(node.attrs?.url || "")),markdownTokenizer: {name: PDF_ATTACHMENT_NODE_TYPE,@@ -103,13 +101,15 @@ export const PdfAttachment = Node.create({},tokenize(source: string) {const match = PDF_LINK_PATTERN.exec(source);- if (!match || !isPdfAttachment(null, match[1]) && !isPdfAttachment(null, match[2])) {+ if (!match) return undefined;+ const label = decodeString(match[1]);+ if (!isPdfAttachment(null, label) && !isPdfAttachment(null, match[2])) {return undefined;}return {type: PDF_ATTACHMENT_NODE_TYPE,raw: match[0],- label: match[1],+ label,url: match[2],};},diff --git a/packages/shared/src/resource-links.ts b/packages/shared/src/resource-links.tsindex 207f2577..bd8b486f 100644--- a/packages/shared/src/resource-links.ts+++ b/packages/shared/src/resource-links.ts@@ -1,5 +1,18 @@+import { toMarkdown } from "mdast-util-to-markdown";+const RESOURCE_PATH_PATTERN = /\/api\/v1\/resources\/([^/]+)\/blob(?:$|[?#])/;+// Resource nodes serialize without a surrounding paragraph/phrasing context.+// Escape entity-like label text there without changing destinations or titles.+export const resourceNodeToMarkdown = (node: Parameters<typeof toMarkdown>[0]): string => toMarkdown(node, {+ unsafe: [{ character: "&", after: "[#A-Za-z]", inConstruct: "label",+ notInConstruct: ["destinationRaw", "destinationLiteral", "titleQuote", "titleApostrophe"] }],+}).trimEnd();++export const resourceLinkToMarkdown = (label: string, url: string): string => resourceNodeToMarkdown({+ type: "link", url, children: [{ type: "text", value: label }],+});+export const getResourceIdFromUrl = (href: string): string | null => {if (href.startsWith("edgeever-resource://")) {try {diff --git a/packages/shared/src/sharing.test.mjs b/packages/shared/src/sharing.test.mjsindex 1926fcb0..1d273fb4 100644--- a/packages/shared/src/sharing.test.mjs+++ b/packages/shared/src/sharing.test.mjs@@ -1,7 +1,30 @@import { describe, expect, test } from "bun:test";import { rewriteMemoResourcesForShare } from "./sharing.ts";+import { docToMarkdown, markdownToDoc } from "./content.ts";describe("shared memo resources", () => {+ test("retains resource queries and PDF page fragments in image and text links", () => {+ const suffix = "?download=1#page=3";+ const markdown = `[](/api/v1/resources/manual/blob${suffix})\n\n[manual](/api/v1/resources/manual/blob${suffix})`;+ const source = markdownToDoc(markdown);+ const expected = structuredClone(source);+ for (const base of ["/api/public/shares/token/resources", "/api/public/notebook-shares/token/resources"]) {+ const access = base.includes("notebook-shares") ? { resourceBasePath: base, memoUrls: {} } : undefined;+ const exported = docToMarkdown(rewriteMemoResourcesForShare(source, "token", {}, access));+ expect(exported).toContain(`](${base}/manual/blob${suffix})`);+ expect(exported.match(/\?download=1#page=3/g)).toHaveLength(2);+ }+ expect(source).toEqual(expected);+ });+ test("maps image outer targets without treating their title as a resource", () => {+ const title = "/api/v1/resources/title/blob";+ const source = { type: "doc", content: [{ type: "image", attrs: { src: "/api/v1/resources/image/blob", link: { href: "/api/v1/resources/manual/blob", title } } }] };+ const rewritten = rewriteMemoResourcesForShare(source, "token");+ expect(rewritten.content[0].attrs.link).toEqual({ href: "/api/public/shares/token/resources/manual/blob", title });+ const notebook = rewriteMemoResourcesForShare(source, "token", {}, { resourceBasePath: "/api/public/notebook-shares/token/resources", memoUrls: {} });+ expect(notebook.content[0].attrs.link.href).toBe("/api/public/notebook-shares/token/resources/manual/blob");+ expect(source.content[0].attrs.link.href).toBe("/api/v1/resources/manual/blob");+ });test("rewrites owned images and attachments without touching external links", () => {const source = {type: "doc",diff --git a/packages/shared/src/sharing.ts b/packages/shared/src/sharing.tsindex 74080157..4113b5f1 100644--- a/packages/shared/src/sharing.ts+++ b/packages/shared/src/sharing.ts@@ -3,6 +3,7 @@ import type { TiptapDoc, TiptapNode, TiptapTextNode } from "./content";import type { PublicNoteProse } from "./note-prose";import { parseMemoLinkHref } from "./note-links";import type { NotebookSiteSettings } from "./notebook-site";+import { getImageLink } from "./image-link";export type MemoShare = {memoId: string;@@ -88,7 +89,7 @@ export type PublicMemoShare = {};const RESOURCE_URL_PATTERN =- /^\/api\/v1\/resources\/([^/?#]+)\/blob(?:[?#].*)?$/;+ /^\/api\/v1\/resources\/([^/?#]+)\/blob([?#].*)?$/;const rewriteAttributeValue = (value: unknown,@@ -99,7 +100,7 @@ const rewriteAttributeValue = (if (typeof value !== "string") return value;const match = value.match(RESOURCE_URL_PATTERN);if (match) {- return `${access?.resourceBasePath ?? `/api/public/shares/${encodeURIComponent(token)}/resources`}/${encodeURIComponent(match[1])}/blob`;+ return `${access?.resourceBasePath ?? `/api/public/shares/${encodeURIComponent(token)}/resources`}/${encodeURIComponent(match[1])}/blob${match[2] ?? ""}`;}const memoId = parseMemoLinkHref(value);@@ -121,10 +122,12 @@ export const rewriteMemoResourcesForShare = (const attrs ="attrs" in node && node.attrs? Object.fromEntries(- Object.entries(node.attrs).map(([key, value]) => [- key,- rewriteAttributeValue(value, token, memoShareTokens, access),- ]),+ Object.entries(node.attrs).map(([key, value]) => {+ const link = node.type === "image" && key === "link" ? getImageLink(value) : null;+ return [key, link+ ? { ...link, href: rewriteAttributeValue(link.href, token, memoShareTokens, access) }+ : rewriteAttributeValue(value, token, memoShareTokens, access)];+ }),): undefined;const marks =diff --git "a/tests/fixtures/note-import/evernote/\345\267\245\344\275\234/\351\241\271\347\233\256/\350\265\204\346\226\231.enex" "b/tests/fixtures/note-import/evernote/\345\267\245\344\275\234/\351\241\271\347\233\256/\350\265\204\346\226\231.enex"new file mode 100644index 00000000..dad8c0b8--- /dev/null+++ "b/tests/fixtures/note-import/evernote/\345\267\245\344\275\234/\351\241\271\347\233\256/\350\265\204\346\226\231.enex"@@ -0,0 +1,5 @@+<?xml version="1.0" encoding="UTF-8"?>+<!DOCTYPE en-export SYSTEM "http://xml.evernote.com/pub/evernote-export4.dtd">+<en-export export-date="20261027T000000Z" application="Evernote" version="10">+<note><title>迁移样本</title><content><![CDATA[<?xml version="1.0"?><!DOCTYPE en-note SYSTEM "http://xml.evernote.com/pub/enml2.dtd"><en-note><h1>资料</h1><div>保留<strong>正文</strong>和中文。</div><div><en-media type="text/plain" hash="5d41402abc4b2a76b9719d911017c592"/></div><div><en-todo checked="true"/>完成</div></en-note>]]></content><created>20200102T030405Z</created><updated>20210203T040506Z</updated><tag>研究</tag><resource><data encoding="base64">aGVsbG8=</data><mime>text/plain</mime><resource-attributes><file-name>说明.txt</file-name></resource-attributes></resource></note>+</en-export>diff --git a/tests/fixtures/note-import/generate-large.mjs b/tests/fixtures/note-import/generate-large.mjsnew file mode 100644index 00000000..4808b8fb--- /dev/null+++ b/tests/fixtures/note-import/generate-large.mjs@@ -0,0 +1,43 @@+// Reproducible, synthetic exports only. Usage: bun <this-file> /absolute/output-dir+import { mkdir } from "node:fs/promises";+import { join } from "node:path";+import { createHash } from "node:crypto";+import { ZipWriter, TextReader } from "@zip.js/zip.js/index-native.js";++const output = process.argv[2];+if (!output) throw new Error("Pass a temporary output directory.");+await mkdir(output, { recursive: true });+const GiB = 1024 ** 3;+for (const count of [1, 4]) {+ const path = join(output, `library-${count}GiB.zip`);+ const sink = Bun.file(path).writer();+ const stream = new WritableStream({ write(chunk) { sink.write(chunk); }, async close() { await sink.end(); } });+ const zip = new ZipWriter(stream, { useWebWorkers: false, level: 1 });+ const markdown = `---\ntitle: Library ${count} GiB\ncreated: 2020-01-02T03:04:05Z\nupdated: 2021-02-03T04:05:06Z\n---\n\n` + Array.from({ length: count }, (_, i) => `[Attachment ${i}](assets/${i}.bin)`).join("\n\n");+ await zip.add("Benchmark.md", new TextReader(markdown));+ for (let i = 0; i < count; i++) {+ let remaining = GiB;+ const block = new Uint8Array(64 * 1024).fill(97);+ await zip.add(`assets/${i}.bin`, new ReadableStream({ pull(controller) {+ if (!remaining) { controller.close(); return; }+ const bytes = block.subarray(0, Math.min(block.length, remaining)); remaining -= bytes.length; controller.enqueue(bytes);+ } }));+ }+ await zip.close();+ console.log(JSON.stringify({ path, logicalBytes: count * GiB, archiveBytes: Bun.file(path).size }));+}+// The digest is required by ENEX's en-media resource reference, not an extra+// integrity check. No whole attachment/base64 string is constructed.+const binaryBytes = 192 * 1024 * 1024;+const binaryBlock = Buffer.alloc(48 * 1024, 97);+const digest = createHash("md5");+for (let n = 0; n < binaryBytes; n += binaryBlock.length) digest.update(binaryBlock);+const hash = digest.digest("hex");+const path = join(output, "large-field.enex");+const writer = Bun.file(path).writer();+writer.write(`<?xml version="1.0"?><en-export><note><title>Large ENEX field</title><content><![CDATA[<en-note><en-media type="application/octet-stream" hash="${hash}"/></en-note>]]></content><created>20200102T030405Z</created><updated>20210203T040506Z</updated><resource><data encoding="base64">`);+const encoded = binaryBlock.toString("base64");+for (let n = 0; n < binaryBytes; n += binaryBlock.length) { writer.write(encoded); if (n % (1024 * 1024 * 3) === 0) await writer.flush(); }+writer.write("</data><mime>application/octet-stream</mime><resource-attributes><file-name>large.bin</file-name></resource-attributes></resource></note></en-export>");+await writer.end();+console.log(JSON.stringify({ path, logicalBytes: binaryBytes, exportBytes: Bun.file(path).size }));diff --git "a/tests/fixtures/note-import/joplin-markdown/_resources/\350\257\264\346\230\216.txt" "b/tests/fixtures/note-import/joplin-markdown/_resources/\350\257\264\346\230\216.txt"new file mode 100644index 00000000..b6fc4c62--- /dev/null+++ "b/tests/fixtures/note-import/joplin-markdown/_resources/\350\257\264\346\230\216.txt"@@ -0,0 +1 @@+hello\ No newline at end of filediff --git "a/tests/fixtures/note-import/joplin-markdown/\345\267\245\344\275\234/\351\241\271\347\233\256/\346\240\267\346\234\254.md" "b/tests/fixtures/note-import/joplin-markdown/\345\267\245\344\275\234/\351\241\271\347\233\256/\346\240\267\346\234\254.md"new file mode 100644index 00000000..1c1eb9fb--- /dev/null+++ "b/tests/fixtures/note-import/joplin-markdown/\345\267\245\344\275\234/\351\241\271\347\233\256/\346\240\267\346\234\254.md"@@ -0,0 +1,12 @@+---+title: Joplin Markdown+tags:+ - 迁移+created: 2020-01-02 03:04:05Z+updated: 2021-02-03 04:05:06Z+author: Test Author+---++# 原始正文++[附件](../../_resources/说明.txt)diff --git a/tests/fixtures/note-import/joplin/11111111111111111111111111111111.md b/tests/fixtures/note-import/joplin/11111111111111111111111111111111.mdnew file mode 100644index 00000000..22f2f3b5--- /dev/null+++ b/tests/fixtures/note-import/joplin/11111111111111111111111111111111.md@@ -0,0 +1,7 @@+工作/研究++id: 11111111111111111111111111111111+parent_id: 66666666666666666666666666666666+created_time: 2020-01-01T00:00:00.000Z+updated_time: 2020-01-01T00:00:00.000Z+type_: 2diff --git a/tests/fixtures/note-import/joplin/22222222222222222222222222222222.md b/tests/fixtures/note-import/joplin/22222222222222222222222222222222.mdnew file mode 100644index 00000000..dfdfc4e4--- /dev/null+++ b/tests/fixtures/note-import/joplin/22222222222222222222222222222222.md@@ -0,0 +1,11 @@+Joplin 笔记++# 正文++[附件](:/33333333333333333333333333333333)++id: 22222222222222222222222222222222+parent_id: 11111111111111111111111111111111+created_time: 2020-01-02T03:04:05.000Z+updated_time: 2021-02-03T04:05:06.000Z+type_: 1diff --git a/tests/fixtures/note-import/joplin/33333333333333333333333333333333.md b/tests/fixtures/note-import/joplin/33333333333333333333333333333333.mdnew file mode 100644index 00000000..91e04964--- /dev/null+++ b/tests/fixtures/note-import/joplin/33333333333333333333333333333333.md@@ -0,0 +1,8 @@+说明.txt++id: 33333333333333333333333333333333+mime: text/plain+file_extension: txt+created_time: 2020-01-02T03:04:05.000Z+updated_time: 2021-02-03T04:05:06.000Z+type_: 4diff --git a/tests/fixtures/note-import/joplin/44444444444444444444444444444444.md b/tests/fixtures/note-import/joplin/44444444444444444444444444444444.mdnew file mode 100644index 00000000..efc25f50--- /dev/null+++ b/tests/fixtures/note-import/joplin/44444444444444444444444444444444.md@@ -0,0 +1,4 @@+研究++id: 44444444444444444444444444444444+type_: 5diff --git a/tests/fixtures/note-import/joplin/55555555555555555555555555555555.md b/tests/fixtures/note-import/joplin/55555555555555555555555555555555.mdnew file mode 100644index 00000000..3ae61ff6--- /dev/null+++ b/tests/fixtures/note-import/joplin/55555555555555555555555555555555.md@@ -0,0 +1,6 @@+++id: 55555555555555555555555555555555+note_id: 22222222222222222222222222222222+tag_id: 44444444444444444444444444444444+type_: 6diff --git a/tests/fixtures/note-import/joplin/66666666666666666666666666666666.md b/tests/fixtures/note-import/joplin/66666666666666666666666666666666.mdnew file mode 100644index 00000000..44c16178--- /dev/null+++ b/tests/fixtures/note-import/joplin/66666666666666666666666666666666.md@@ -0,0 +1,5 @@+项目++id: 66666666666666666666666666666666+parent_id:+type_: 2diff --git a/tests/fixtures/note-import/joplin/_resources/33333333333333333333333333333333.txt b/tests/fixtures/note-import/joplin/_resources/33333333333333333333333333333333.txtnew file mode 100644index 00000000..b6fc4c62--- /dev/null+++ b/tests/fixtures/note-import/joplin/_resources/33333333333333333333333333333333.txt@@ -0,0 +1 @@+hello\ No newline at end of filediff --git a/tests/fixtures/note-import/obsidian/assets/diagram.svg b/tests/fixtures/note-import/obsidian/assets/diagram.svgnew file mode 100644index 00000000..5afecbac--- /dev/null+++ b/tests/fixtures/note-import/obsidian/assets/diagram.svg@@ -0,0 +1 @@+<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 10 10"><rect width="10" height="10" fill="#07130B"/></svg>diff --git "a/tests/fixtures/note-import/obsidian/assets/\350\257\264\346\230\216.txt" "b/tests/fixtures/note-import/obsidian/assets/\350\257\264\346\230\216.txt"new file mode 100644index 00000000..b6fc4c62--- /dev/null+++ "b/tests/fixtures/note-import/obsidian/assets/\350\257\264\346\230\216.txt"@@ -0,0 +1 @@+hello\ No newline at end of filediff --git "a/tests/fixtures/note-import/obsidian/\347\240\224\347\251\266/\346\240\267\346\234\254.md" "b/tests/fixtures/note-import/obsidian/\347\240\224\347\251\266/\346\240\267\346\234\254.md"new file mode 100644index 00000000..88ab4feb--- /dev/null+++ "b/tests/fixtures/note-import/obsidian/\347\240\224\347\251\266/\346\240\267\346\234\254.md"@@ -0,0 +1,23 @@+---+title: 我的研究+tags: [迁移, '#资料']+created: 2020-01-02T03:04:05Z+updated: 2021-02-03T04:05:06Z+author: Test Author+---++# 原始正文++++![[diagram.svg]]++[附件](../assets/说明.txt)++++``++```md+![[missing.png]]+```diff --git a/tests/fixtures/note-import/real-apps/README.md b/tests/fixtures/note-import/real-apps/README.mdnew file mode 100644index 00000000..3bbb10a7--- /dev/null+++ b/tests/fixtures/note-import/real-apps/README.md@@ -0,0 +1,19 @@+# Real source-app exports++[简体中文](README.zh-CN.md)++Obsidian/Joplin samples were created in isolated local libraries on macOS arm64 on 2026-10-29. The Evernote sample was created separately in a test account and exported on 2026-10-31. All note content and attachment bytes are self-authored; no personal library, account data or application binaries are included.++- **Obsidian 1.14.5:** six notes and five source attachments created through the official CLI and Vault API. `obsidian-1.14.5.zip` packages the native Vault directory with its outer folder and `.obsidian` configuration. Obsidian stores the original Markdown directly; there is no separate Markdown exporter.+- **Joplin 3.7.21:** five notes in three nested notebooks, created through the desktop Data API after in-app authorization. The installed desktop app exported the selected notebook as JEX, RAW, Markdown and Markdown with Front Matter. ZIPs package those unmodified export directories with one outer folder. Default welcome notebooks are excluded.+- **Evernote 11.40.2:** one new test note created in the desktop editor and exported through its note menu as unmodified `evernote-11.40.2.enex`. It includes a heading, table, code block, native checked/unchecked checklist, two tags, and PNG/SVG/PDF/text/ZIP attachments. Author metadata was deselected during export to exclude account details. Original creation/modification times are `2026-10-31T03:38:38Z` / `2026-10-31T03:41:05Z`.++`assets/` contains independent expected bytes shared by all three sources. Obsidian/Joplin each reference five source files, with a PNG reused by two notes, giving six imported note-owned resources per library; the single Evernote note imports five resources. The corpus includes Unicode/spaces/literal percent signs, a ZIP attachment, an empty note, tables, tasks, code, image links and 30 tags. Obsidian/Joplin metadata deliberately sets creation to `2020-01-02T03:04:05Z` and modification to `2021-02-03T04:05:06Z`. Plain Joplin Markdown does not export tags/timestamps; the importer uses its existing defaults for those absent values.++Run the real HTTP/SQLite/resource-readback cases:++```sh+bun test tests/note-import.integration.test.mjs --test-name-pattern 'real desktop-app exports'+```++Existing authored ENEX boundary fixtures remain in the sibling `evernote/` directory.diff --git a/tests/fixtures/note-import/real-apps/README.zh-CN.md b/tests/fixtures/note-import/real-apps/README.zh-CN.mdnew file mode 100644index 00000000..f05da109--- /dev/null+++ b/tests/fixtures/note-import/real-apps/README.zh-CN.md@@ -0,0 +1,19 @@+# 来源应用真实导出++[English](README.md)++Obsidian/Joplin 样本于 2026-10-29 在 macOS arm64 的独立本地测试库生成;Evernote 样本于 2026-10-31 使用测试账号单独新建并导出。正文与附件均为自行编写的数据,不含个人知识库、账号信息或应用安装包。++- **Obsidian 1.14.5**:通过官方 CLI 和 Vault API 创建 6 篇笔记及 5 个源附件。`obsidian-1.14.5.zip` 将原生 Vault 目录连同外层目录和 `.obsidian` 配置打包。Obsidian 直接保存原始 Markdown,无需另行导出。+- **Joplin 3.7.21**:在应用内批准 Data API 访问后,创建 3 层笔记本中的 5 篇笔记。由已安装的桌面应用将该笔记本导出为 JEX、RAW、Markdown 和带 Front Matter 的 Markdown;ZIP 只对原始导出目录增加外层目录并打包,未修改导出内容。不包含默认欢迎笔记本。+- **Evernote 11.40.2**:通过桌面编辑器新建一篇测试笔记,再从笔记菜单导出原始 `evernote-11.40.2.enex`,未修改导出字节。包含标题、表格、代码块、原生已勾选/未勾选清单、两个标签及 PNG、SVG、PDF、文本、ZIP 附件。导出时取消作者属性,不包含账号资料;原始创建/修改时间分别为 `2026-10-31T03:38:38Z` / `2026-10-31T03:41:05Z`。++`assets/` 是三个来源共用的独立预期原字节。Obsidian/Joplin 各引用 5 个源文件,PNG 被两篇笔记复用,因此各导入 6 个笔记所属资源;Evernote 单篇笔记导入 5 个资源。语料包括中文、空格、字面百分号、ZIP 附件、空笔记、表格、待办、代码、图片链接和 30 个标签。Obsidian/Joplin 来源元数据主动设为创建时间 `2020-01-02T03:04:05Z`、修改时间 `2021-02-03T04:05:06Z`。Joplin 普通 Markdown 不导出标签和时间,这些缺失值沿用导入器的既有默认值。++运行真实 HTTP、SQLite 和附件读回用例:++```sh+bun test tests/note-import.integration.test.mjs --test-name-pattern 'real desktop-app exports'+```++原有自行构造的 ENEX 边界样本仍位于相邻的 `evernote/` 目录。diff --git "a/tests/fixtures/note-import/real-apps/assets/\345\216\237\346\240\267\344\277\235\347\225\231.zip" "b/tests/fixtures/note-import/real-apps/assets/\345\216\237\346\240\267\344\277\235\347\225\231.zip"new file mode 100644index 00000000..d6bbc059Binary files /dev/null and "b/tests/fixtures/note-import/real-apps/assets/\345\216\237\346\240\267\344\277\235\347\225\231.zip" differdiff --git "a/tests/fixtures/note-import/real-apps/assets/\346\240\267\346\234\254 \345\233\276.png" "b/tests/fixtures/note-import/real-apps/assets/\346\240\267\346\234\254 \345\233\276.png"new file mode 100644index 00000000..cfa1fab1Binary files /dev/null and "b/tests/fixtures/note-import/real-apps/assets/\346\240\267\346\234\254 \345\233\276.png" differdiff --git "a/tests/fixtures/note-import/real-apps/assets/\347\237\242\351\207\217 \345\233\276.svg" "b/tests/fixtures/note-import/real-apps/assets/\347\237\242\351\207\217 \345\233\276.svg"new file mode 100644index 00000000..78504800--- /dev/null+++ "b/tests/fixtures/note-import/real-apps/assets/\347\237\242\351\207\217 \345\233\276.svg"@@ -0,0 +1 @@+<svg xmlns="http://www.w3.org/2000/svg" width="480" height="240"><rect width="480" height="240" fill="#07130b"/><circle cx="240" cy="120" r="70" fill="#5c996f"/><text x="240" y="130" text-anchor="middle" font-family="sans-serif" font-size="24" fill="white">Import QA</text></svg>\ No newline at end of filediff --git "a/tests/fixtures/note-import/real-apps/assets/\350\257\264\346\230\216 100%.txt" "b/tests/fixtures/note-import/real-apps/assets/\350\257\264\346\230\216 100%.txt"new file mode 100644index 00000000..aff4e792--- /dev/null+++ "b/tests/fixtures/note-import/real-apps/assets/\350\257\264\346\230\216 100%.txt"@@ -0,0 +1,3 @@+EdgeEver Import QA+中文、空格、100% 与 emoji 🌿+This file is self-authored test data.diff --git "a/tests/fixtures/note-import/real-apps/assets/\350\277\201\347\247\273\350\257\264\346\230\216.pdf" "b/tests/fixtures/note-import/real-apps/assets/\350\277\201\347\247\273\350\257\264\346\230\216.pdf"new file mode 100644index 00000000..8fbcbc6a--- /dev/null+++ "b/tests/fixtures/note-import/real-apps/assets/\350\277\201\347\247\273\350\257\264\346\230\216.pdf"@@ -0,0 +1,32 @@+%PDF-1.4+1 0 obj+<< /Type /Catalog /Pages 2 0 R >>+endobj+2 0 obj+<< /Type /Pages /Count 1 /Kids [3 0 R] >>+endobj+3 0 obj+<< /Type /Page /Parent 2 0 R /MediaBox [0 0 595 842] /Resources << /Font << /F1 4 0 R >> >> /Contents 5 0 R >>+endobj+4 0 obj+<< /Type /Font /Subtype /Type1 /BaseFont /Helvetica >>+endobj+5 0 obj+<< /Length 111 >>+stream+BT /F1 24 Tf 60 740 Td (EdgeEver Import QA) Tj 0 -40 Td /F1 12 Tf (2026-10-29 - original test attachment) Tj ET+endstream+endobj+xref+0 6+0000000000 65535 f+0000000009 00000 n+0000000058 00000 n+0000000115 00000 n+0000000241 00000 n+0000000311 00000 n+trailer+<< /Root 1 0 R /Size 6 >>+startxref+473+%%EOFdiff --git a/tests/fixtures/note-import/real-apps/evernote-11.40.2.enex b/tests/fixtures/note-import/real-apps/evernote-11.40.2.enexnew file mode 100644index 00000000..7b4e0398--- /dev/null+++ b/tests/fixtures/note-import/real-apps/evernote-11.40.2.enex@@ -0,0 +1,89 @@+<?xml version="1.0" encoding="UTF-8"?>+<!DOCTYPE en-export SYSTEM "http://xml.evernote.com/pub/evernote-export4.dtd">+<en-export export-date="20261031T034130Z" application="Evernote" version="11.40.2">+ <note>+ <title>EdgeEver QA · 正文与附件 🌿</title>+ <created>20261031T033838Z</created>+ <updated>20261031T034105Z</updated>+ <tag>edgeever-import-qa</tag>+ <tag>迁移验收</tag>+ <note-attributes>+ </note-attributes>+ <content>+ <![CDATA[<?xml version="1.0" encoding="UTF-8" standalone="no"?>+<!DOCTYPE en-note SYSTEM "http://xml.evernote.com/pub/enml2.dtd"><en-note><div style="display:none;--en-chs:"eyJpc0VtcHR5IjpmYWxzZSwiaDEiOnsiZm9udEZhbWlseSI6ImluaGVyaXQiLCJmb250U2l6ZSI6IjMwcHgiLCJjb2xvciI6ImluaGVyaXQiLCJmb250V2VpZ2h0IjoiNjAwIiwiZm9udFN0eWxlIjoiaW5oZXJpdCIsInRleHREZWNvcmF0aW9uIjoiaW5oZXJpdCJ9LCJoMiI6eyJmb250RmFtaWx5IjoiaW5oZXJpdCIsImZvbnRTaXplIjoiMjRweCIsImNvbG9yIjoiaW5oZXJpdCIsImZvbnRXZWlnaHQiOiI2MDAiLCJmb250U3R5bGUiOiJpbmhlcml0IiwidGV4dERlY29yYXRpb24iOiJpbmhlcml0In0sImgzIjp7ImZvbnRGYW1pbHkiOiJpbmhlcml0IiwiZm9udFNpemUiOiIxOHB4IiwiY29sb3IiOiJpbmhlcml0IiwiZm9udFdlaWdodCI6IjYwMCIsImZvbnRTdHlsZSI6ImluaGVyaXQiLCJ0ZXh0RGVjb3JhdGlvbiI6ImluaGVyaXQifSwiaDQiOnsiZm9udEZhbWlseSI6ImluaGVyaXQiLCJmb250U2l6ZSI6IjE2cHgiLCJjb2xvciI6ImluaGVyaXQiLCJmb250V2VpZ2h0IjoiNjAwIiwiZm9udFN0eWxlIjoiaW5oZXJpdCIsInRleHREZWNvcmF0aW9uIjoiaW5oZXJpdCJ9LCJwIjp7ImZvbnRGYW1pbHkiOiJpbmhlcml0IiwiZm9udFNpemUiOiJpbmhlcml0IiwiY29sb3IiOiJpbmhlcml0IiwiZm9udFdlaWdodCI6ImluaGVyaXQiLCJmb250U3R5bGUiOiJpbmhlcml0IiwidGV4dERlY29yYXRpb24iOiJpbmhlcml0In19""> </div><h2 style="--en-nodeId:dc67db22-23a0-4484-8673-1bf146d104b7;">真实 ENEX 导出验收</h2><div>这是一篇独立测试笔记,包含中文、空格与 100% 字面百分号。</div><div><b>粗体</b>、<i>斜体</i>以及<a href="https://example.com/edgeever?q=100%25#section" rev="en_rl_none">公开示例链接</a>。</div><table width="376px" style="border-collapse:collapse;width:376px;"><colgroup><col style="width: 188px;" /><col style="width: 188px;" /></colgroup><tbody><tr><th style="border-color:#d4d4d4;border-width:1.5px;border-style:solid;padding:10px;"><div>项目</div></th><th style="border-color:#d4d4d4;border-width:1.5px;border-style:solid;padding:10px;"><div>结果</div></th></tr><tr><td style="border-color:#d4d4d4;border-width:1.5px;border-style:solid;padding:10px;"><div>图片和附件</div></td><td style="border-color:#d4d4d4;border-width:1.5px;border-style:solid;padding:10px;"><div>保留原字节</div></td></tr></tbody></table><div><br/></div><div style="--en-codeblock:true; --en-lineWrapping:false;box-sizing: border-box; padding: 8px; font-family: "Fira Code","Consolas","Monaco","Andale Mono","Ubuntu Mono","Courier New",monospace font-size: 12px; color: rgb(51, 51, 51); border-top-left-radius: 4px; border-top-right-radius: 4px; border-bottom-right-radius: 4px; border-bottom-left-radius: 4px; background-color: rgb(251, 250, 248); border: 1px solid rgba(0, 0, 0, 0.14902); background-position: initial initial; background-repeat: initial initial;"><div>const message = 'ENEX & EdgeEver';</div></div><div>待办清单:</div><ul style="--en-todo:true;"><li><div>尚未完成的导入检查</div></li><li style="--en-checked:true;"><div>已经完成的格式准备</div></li></ul><en-media style="--en-viewAs:pdf-pageByPage;" hash="6af3b32b4cff4bbbcf517d86454f874b" type="application/pdf" /><en-media hash="829bacd0ace0acadae7c16efcbf094bf" type="text/plain" /><en-media hash="9d9fd983063603f3ac9a010e724a806a" type="image/svg+xml" style="--en-naturalWidth:480; --en-naturalHeight:240;" /><en-media hash="4476ac7f4ed91566d6c5bf7de69eec18" type="image/png" style="--en-naturalWidth:480; --en-naturalHeight:240;" /><en-media hash="9b88dbbdbffbf80811cec32d0036fc71" type="application/zip" /><div><br/></div></en-note> ]]>+ </content>+ <resource>+ <data encoding="base64">+UEsDBBQAAAgIADoIXV3R6QRsTgAAAEkAAAAKAAAA6K+05piOLnR4dAFJALb/6L+Z5piv56yU6K6w6YeM55qE5Y6L57yp6ZmE5Lu277yM5LiN5bqU5L2c5Li65paw55qE5p2l5rqQ5b2S5qGj5bGV5byA44CCClBLAQIUAxQAAAgIADoIXV3R6QRsTgAAAEkAAAAKAAAAAAAAAAAAAACAAQAAAADor7TmmI4udHh0UEsFBgAAAAABAAEAOAAAAHYA+AAAAAA==+ </data>+ <mime>application/zip</mime>+ <resource-attributes>+ <file-name>原样保留.zip</file-name>+ <source-url>en-cache://tokenKey%3D%22AuthToken%3AUser%3A317941790%22+3355ffb2-b4cd-136e-5dc5-3c6275eac41b+9b88dbbdbffbf80811cec32d0036fc71+https%3A%2F%2Fpublic.www.evernote.com%2Fresources%2Fs524%2Ffeb0de11-dd75-74a1-ec83-f6c1d0cc7e1e</source-url>+ </resource-attributes>+ </resource>+ <resource>+ <data encoding="base64">+PHN2ZyB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciIHdpZHRoPSI0ODAiIGhlaWdodD0iMjQwIj48cmVjdCB3aWR0aD0iNDgwIiBoZWlnaHQ9IjI0MCIgZmlsbD0iIzA3MTMwYiIvPjxjaXJjbGUgY3g9IjI0MCIgY3k9IjEyMCIgcj0iNzAiIGZpbGw9IiM1Yzk5NmYiLz48dGV4dCB4PSIyNDAiIHk9IjEzMCIgdGV4dC1hbmNo+b3I9Im1pZGRsZSIgZm9udC1mYW1pbHk9InNhbnMtc2VyaWYiIGZvbnQtc2l6ZT0iMjQiIGZpbGw9IndoaXRlIj5JbXBvcnQgUUE8L3RleHQ+PC9zdmc++ </data>+ <mime>image/svg+xml</mime>+ <resource-attributes>+ <file-name>矢量 图.svg</file-name>+ <source-url>en-cache://tokenKey%3D%22AuthToken%3AUser%3A317941790%22+3355ffb2-b4cd-136e-5dc5-3c6275eac41b+9d9fd983063603f3ac9a010e724a806a+https%3A%2F%2Fpublic.www.evernote.com%2Fresources%2Fs524%2Fcffad490-46dd-80ef-cb58-aebe4879bc62</source-url>+ </resource-attributes>+ </resource>+ <resource>+ <data encoding="base64">+iVBORw0KGgoAAAANSUhEUgAAAeAAAADwCAIAAABXFyDtAAAIq0lEQVR4nO3UsckYZBiF0XQWli5gYelI2SFVdnCFrOFKgqUTCHGBrwjC5YH3P3BmOJ9++uVnDvv87QuH/fXP3xz2KR+EqXwQpvJBmBL0cfkgTOWDMCXo4/JBmMoHYUrQx+WDMJUPwpSgj8sHYSofhClBH5cPwlQ+CFOCPi4fhKl8EKYEfVw+CFP5IEwJ+rh8EKbyQZgS9HH5IEzl+gzAl6OPyQZjKB2FK0MflgzCVD8KUoI/LB2EqH4QpQR+XD8JUPghTgj4uH4SpfBCmBH1cPghT+SBMCfq4fBCm8kGYEvRx+SBM5YMwJejj8kGYygdhStDH5YMwlQ/ClKCPywdhKh+EKUEflw/CVD4IU4I+Lh+EqXwQpgR9XD4IU/kgTAn6uHwQpvJBmBL0cfkgTOWDMCXo4/JBmMoHYUrQx+WDMJUPwpSgj8sHYSofhClBH5cPwlQ+CFOCPi4fhKl8+EKYEfVw+CFP5IEwJ+rh8EKbyQZgS9HH5IEzlgzAl6OPyQZjKB2FK0MflgzCVD8KUoI/LB2EqH4QpQR+XD8JUPghTgj4uH4SpfBCmBH1cPghT+SBMCfq4fBCm8kGYEvRx+SBM5YMwJejj8kGYygdhStDH5YMwlQ/ClKCPywdhKh+EKUEflw/CVD4IU4I+Lh+EqXwQpgR9XD4IU/kgTAn6uHwQpvJBmBL0cfkgTOWDMCXo4/JBmMoHYUrQx+WDMJUP+wpSgj8sHYSofhClBH5cPwlQ+CFOCPi4fhKl8EKYEfVw+CFP5IEwJ+rh8EKbyQZgS9HH5IEzlgzAl6OPyQZjKB2FK0MflgzCVD8LURwz6199/+zi+/vkHh/37/fuHko8paEELmh+VjyloQQta0LzlYwpa0IIWNG/5mIIWtKAFzVs+pqAFLWhB85aPKWhBC1rQvOVjClrQghY0b/mYgha0oAXNWz6moAUtaEHzlo8paEELWtC85WMKWtCCFjRv+ZiC+FrSgBc1bPqagBS1oQfOWjyloQQta0LzlYwpa0IIWNG/5mIIWtKAFzVs+pqAFLWhB85aPKWhBC1rQvOVjClrQghY0b/mYgha0oAXNWz6moAUtaEHzlo8paEELWtC85WMKWtCCFjRv+ZiCFrSgBc1bPqagBS1oQfOWjyloQQta0LzlYwpa0IIWNG/5mIIWtKAFzVs+pqAFLWhB85aPKWhBC1rQvOVjClrQghY0b/mYgha0oAXNWz6moAUtaEHzlo8p+aEELWtC85WMKWtCCFjRv+ZiCFrSgBc1bPqagBS1oQfOWjyloQQta0LzlYwpa0IIWNG/5mIIWtKAFzVs+pqAFLWhB85aPKWhBC1rQvOVjClrQghY0b/mYgha0oAXNWz6moAUtaEHzlo8paEELWtC85WMKWtCCFjRv+ZiCFrSgBc1bPqagBS1oQfOWjyloQQta0LzlYwpa0IIWNG/5mIIWtKAFzVs+pqAFLWhB85aPKWhBC1rQvOVjClrQghY0b/mY+gha0oAXNWz6moAUtaEHzlo8paEELWtC85WMKWtCCFjRv+ZiCFrSgBc1bPqagBS1oQfOWjyloQQta0LzlYwpa0IIWNG/5mIIWtKAFzVs+pqAFLWhB85aPKWhBC1rQvOVjClrQghY0b/mYgha0oAXNWz6moAUtaEHzlo8paEELWtC85WMKWtCCFjRv+ZiCFrSgBc1bPqagBS1oQfOWjyloQQta0LzlYwpa0IIWNG/5mIIWtKAFzVs+pqAFLWhB85aP+KWhBC1rQvOVjClrQghY0b/mYgha0oAXNWz6moAUtaEHzlo8paEELWtC85WMKWtCCFjRv+ZiCFrSgBc1bPqagBS1oQfOWjyloQQta0LzlYwpa0IIWNG/5mIIWtKAFzVs+pqAFLWhB85aPKWhBC1rQvOVjClrQghY0b/mYgha0oAXNWz6moAUtaEHzlo8paEELWtC85WMKWtCCFjRv+ZiCFrSgBc1bPqagBS1oQfOWjyloQQta0LzlYwpa0IIWNG/5+mIIWtKAFzVs+pqAFLWhB85aPKWhBC1rQvOVjClrQghY0b/mYgha0oAXNWz6moAUtaEHzlo8paEELWtC85WMKWtCCFjRv+ZiCFrSgBc1bPqagBS1oQfOWjyloQQta0LzlYwpa0IIWNG/5mIIWtKAFzVs+pqAFLWhB85aPKWhBC1rQvOVjClrQghY0b/mYgha0oAXNWz6moAUtaEHzlo8paEELWtC85WMKWtD8f5+/feGwfBCmBH1cPghT+SBMCfq4+fBCm8kGYEvRx+SBM5YMwJejj8kGYygdhStDH5YMwlQ/ClKCPywdhKh+EKUEflw/CVD4IU4I+Lh+EqXwQpgR9XD4IU/kgTAn6uHwQpvJBmBL0cfkgTOWDMCXo4/JBmMoHYUrQx+WDMJUPwpSgj8sHYSofhClBH5cPwlQ+CFOCPi4fhKl8EKYEfVw+CFP5IEwJ+rh8EKbyQZgS9HH5IEzlgzAl6OPyQZjKB2FK0MflgzCVD8KUoI/LB2EqH4QpQR+X+D8JUPghTgj4uH4SpfBCmBH1cPghT+SBMCfq4fBCm8kGYEvRx+SBM5YMwJejj8kGYygdhStDH5YMwlQ/ClKCPywdhKh+EKUEflw/CVD4IU4I+Lh+EqXwQpgR9XD4IU/kgTAn6uHwQpvJBmBL0cfkgTOWDMCXo4/JBmMoHYUrQx+WDMJUPwpSgj8sHYSofhClBH5cPwlQ+CFOCPi4fhKl8EKYEfVw+CFP5IEwJ+rh8EKbyQZgS9HH5IEzlgzAl6OPy+QZjKB2FK0MflgzCVD8KUoI/LB2EqH4QpQR+XD8JUPghTgj4uH4SpfBCmBH1cPghT+SBMCfq4fBCm8kGYEvRx+SBM5YMwJejj8kGYygdhStDH5YMwlQ/ClKCPywdhKh+EKUEflw/CVD4IU4I+Lh+EqXwQpgR9XD4IU/kgTAn6uHwQpvJBmBL0cfkgTOWDMPUfE9LvrXicikEAAAAASUVORK5CYII=+ </data>+ <mime>image/png</mime>+ <width>480</width>+ <height>240</height>+ <resource-attributes>+ <file-name>样本 图.png</file-name>+ <source-url>en-cache://tokenKey%3D%22AuthToken%3AUser%3A317941790%22+3355ffb2-b4cd-136e-5dc5-3c6275eac41b+4476ac7f4ed91566d6c5bf7de69eec18+https%3A%2F%2Fpublic.www.evernote.com%2Fresources%2Fs524%2Fce8f0121-3d2c-9500-9b96-18fe45f807a3</source-url>+ <application-data key="evernote.captionState">AVAILABLE</application-data>+ <application-data key="evernote.captionUpdatedAt">1793418078442</application-data>+ <application-data key="evernote.caption">The image displays a horizontal tripartite composition with a black square on the left, a solid green rectangle in the center, and a white square on the right, all set against a light green background.</application-data>+ <application-data key="evernote.captionSource">AI</application-data>+ </resource-attributes>+ </resource>+ <resource>+ <data encoding="base64">+JVBERi0xLjQKMSAwIG9iago8PCAvVHlwZSAvQ2F0YWxvZyAvUGFnZXMgMiAwIFIgPj4KZW5kb2JqCjIgMCBvYmoKPDwgL1R5cGUgL1BhZ2VzIC9Db3VudCAxIC9LaWRzIFszIDAgUl0gPj4KZW5kb2JqCjMgMCBvYmoKPDwgL1R5cGUgL1BhZ2UgL1BhcmVudCAyIDAgUiAvTWVkaWFCb3ggWzAgMCA1OTUgODQyXSAvUmVzb3VyY2VzIDw8IC9G+b250IDw8IC9GMSA0IDAgUiA+PiA+PiAvQ29udGVudHMgNSAwIFIgPj4KZW5kb2JqCjQgMCBvYmoKPDwgL1R5cGUgL0ZvbnQgL1N1YnR5cGUgL1R5cGUxIC9CYXNlRm9udCAvSGVsdmV0aWNhID4+CmVuZG9iago1IDAgb2JqCjw8IC9MZW5ndGggMTExID4+CnN0cmVhbQpCVCAvRjEgMjQgVGYgNjAgNzQwIFRkIChFZGdlRXZlciBJbXBvcnQg+UUEpIFRqIDAgLTQwIFRkIC9GMSAxMiBUZiAoMjAyNi0xMC0yOSAtIG9yaWdpbmFsIHRlc3QgYXR0YWNobWVudCkgVGogRVQKZW5kc3RyZWFtCmVuZG9iagp4cmVmCjAgNgowMDAwMDAwMDAwIDY1NTM1IGYgCjAwMDAwMDAwMDkgMDAwMDAgbiAKMDAwMDAwMDA1OCAwMDAwMCBuIAowMDAwMDAwMTE1IDAwMDAwIG4gCjAwMDAwMDAyNDEgMDAw+MDAgbiAKMDAwMDAwMDMxMSAwMDAwMCBuIAp0cmFpbGVyCjw8IC9Sb290IDEgMCBSIC9TaXplIDYgPj4Kc3RhcnR4cmVmCjQ3MwolJUVPRgo=+ </data>+ <mime>application/pdf</mime>+ <resource-attributes>+ <file-name>迁移说明.pdf</file-name>+ <source-url>en-cache://tokenKey%3D%22AuthToken%3AUser%3A317941790%22+3355ffb2-b4cd-136e-5dc5-3c6275eac41b+6af3b32b4cff4bbbcf517d86454f874b+https%3A%2F%2Fpublic.www.evernote.com%2Fresources%2Fs524%2Fbccf0f9d-98ca-2176-2406-3b0cdaba3f90</source-url>+ </resource-attributes>+ </resource>+ <resource>+ <data encoding="base64">+RWRnZUV2ZXIgSW1wb3J0IFFBCuS4reaWh+OAgeepuuagvOOAgTEwMCUg5LiOIGVtb2ppIPCfjL8KVGhpcyBmaWxlIGlzIHNlbGYtYXV0aG9yZWQgdGVzdCBkYXRhLgo=+ </data>+ <mime>text/plain</mime>+ <resource-attributes>+ <file-name>说明 100%.txt</file-name>+ <source-url>en-cache://tokenKey%3D%22AuthToken%3AUser%3A317941790%22+3355ffb2-b4cd-136e-5dc5-3c6275eac41b+829bacd0ace0acadae7c16efcbf094bf+https%3A%2F%2Fpublic.www.evernote.com%2Fresources%2Fs524%2Fe287483a-f3ee-4292-3381-532437b845fc</source-url>+ </resource-attributes>+ </resource>+ </note>+</en-export>diff --git a/tests/fixtures/note-import/real-apps/joplin-3.7.21-md-frontmatter.zip b/tests/fixtures/note-import/real-apps/joplin-3.7.21-md-frontmatter.zipnew file mode 100644index 00000000..79da7f08Binary files /dev/null and b/tests/fixtures/note-import/real-apps/joplin-3.7.21-md-frontmatter.zip differdiff --git a/tests/fixtures/note-import/real-apps/joplin-3.7.21-md.zip b/tests/fixtures/note-import/real-apps/joplin-3.7.21-md.zipnew file mode 100644index 00000000..65999143Binary files /dev/null and b/tests/fixtures/note-import/real-apps/joplin-3.7.21-md.zip differdiff --git a/tests/fixtures/note-import/real-apps/joplin-3.7.21-raw.zip b/tests/fixtures/note-import/real-apps/joplin-3.7.21-raw.zipnew file mode 100644index 00000000..5053120eBinary files /dev/null and b/tests/fixtures/note-import/real-apps/joplin-3.7.21-raw.zip differdiff --git a/tests/fixtures/note-import/real-apps/joplin-3.7.21.jex b/tests/fixtures/note-import/real-apps/joplin-3.7.21.jexnew file mode 100644index 00000000..761d310dBinary files /dev/null and b/tests/fixtures/note-import/real-apps/joplin-3.7.21.jex differdiff --git a/tests/fixtures/note-import/real-apps/obsidian-1.14.5.zip b/tests/fixtures/note-import/real-apps/obsidian-1.14.5.zipnew file mode 100644index 00000000..bde21a4eBinary files /dev/null and b/tests/fixtures/note-import/real-apps/obsidian-1.14.5.zip differdiff --git a/tests/json-backup.test.ts b/tests/json-backup.test.tsindex b24bd5a4..9a8ad187 100644--- a/tests/json-backup.test.ts+++ b/tests/json-backup.test.ts@@ -84,6 +84,22 @@ const prompt: AiPromptTemplate = {};describe("EdgeEver ZIP", () => {+ test("passes server-provided continuation cursors to later ZIP pages", async () => {+ const cursors: (string | undefined)[] = [];+ const blob = await createEdgeEverZip({+ listNotebooks: async () => ({ notebooks: [notebook("nb_child", "Child")] }),+ listPrompts: async () => ({ prompts: [] }),+ getPage: async (offset, _limit, cursor) => {+ cursors.push(cursor);+ return { memos: [{ ...memo, id: `memo_cursor_${offset}`, contentMarkdown: "body" }], resources: [], revisions: [],+ ...(offset === 0 ? { totalCount: 2 } : {}), nextOffset: offset === 0 ? 1 : null, nextCursor: offset === 0 ? "next-page" : null };+ },+ getResourceResponse: async () => new Response(),+ }, { edgeeverVersion: "2.16.1", buildId: "cursor" });+ expect(cursors).toEqual([undefined, "next-page"]);+ expect((await parseEdgeEverZip(blob)).manifest.counts.memos).toBe(2);+ });+test("reports actionable archive validation errors", async () => {await expect(parseEdgeEverZip(new Blob(["not a zip"]))).rejects.toEqual(expect.objectContaining({ code: "invalidZip" })diff --git a/tests/note-import.integration.test.mjs b/tests/note-import.integration.test.mjsnew file mode 100644index 00000000..85832d2e--- /dev/null+++ b/tests/note-import.integration.test.mjs@@ -0,0 +1,1007 @@+import { afterEach, describe, expect, test } from "bun:test";+import { Database } from "bun:sqlite";+import { readdir, readFile, mkdtemp, rm } from "node:fs/promises";+import { join } from "node:path";+import { tmpdir } from "node:os";+import { BlobReader, ZipReader, ZipWriter, Uint8ArrayWriter, Uint8ArrayReader } from "@zip.js/zip.js/index-native.js";+import { pack } from "it-tar";+import { fetchEdgeEverApp } from "../apps/api/src/index.ts";+import { createSelfHostedStorageAdapter } from "../apps/api/src/self-hosted-storage-adapter.ts";+import { createEdgeEverClient } from "../packages/client/src/index.ts";+import { docToMarkdown, markdownToDoc } from "../packages/shared/src/index.ts";+import { NoteImportSession, MemoryImportIndex } from "../packages/shared/src/note-import/index.ts";+import { executeNoteImport } from "../apps/web/src/lib/note-import/execute.ts";+import { convertImportEnml } from "../apps/web/src/lib/note-import/enml.ts";++const directories = [];+const servers = [];+afterEach(async () => { for (const server of servers.splice(0)) server.stop(true); for (const dir of directories.splice(0)) await rm(dir, { recursive: true, force: true }); });+const fixtureFiles = async (name) => {+ const root = new URL(`./fixtures/note-import/${name}/`, import.meta.url);+ const walk = async (prefix = "") => {+ const result = [];+ for (const entry of await readdir(new URL(prefix, root), { withFileTypes: true })) {+ const path = prefix + entry.name;+ if (entry.isDirectory()) result.push(...await walk(path + "/"));+ else result.push({ path, file: new File([await readFile(new URL(path, root))], entry.name) });+ }+ return result;+ };+ return walk();+};+const archive = async (files, format) => {+ if (format === "zip") {+ const writer = new ZipWriter(new Uint8ArrayWriter());+ for (const entry of files) await writer.add(entry.path, new Uint8ArrayReader(new Uint8Array(await entry.file.arrayBuffer())));+ return [{ path: "export.zip", file: new File([await writer.close()], "export.zip") }];+ }+ const entries = [];+ for (const entry of files) entries.push({ header: { name: entry.path, size: entry.file.size }, body: [new Uint8Array(await entry.file.arrayBuffer())] });+ const chunks = [];+ for await (const part of pack()(entries)) chunks.push(part instanceof Uint8Array ? part : part.subarray());+ return [{ path: "export.jex", file: new File(chunks, "export.jex") }];+};+const target = async () => {+ const dir = await mkdtemp(join(tmpdir(), "edgeever-note-import-"));+ directories.push(dir);+ const db = new Database(join(dir, "db.sqlite"), { create: true });+ db.exec("PRAGMA foreign_keys = ON");+ const migrations = new URL("../migrations/", import.meta.url);+ for (const name of (await readdir(migrations)).filter(x => x.endsWith(".sql")).sort()) db.exec(await readFile(new URL(name, migrations), "utf8"));+ db.exec("INSERT OR IGNORE INTO workspaces (id, name, is_personal) VALUES ('ws_default', 'Personal', 1)");+ const env = { storage: createSelfHostedStorageAdapter(db, join(dir, "resources")), EDGE_EVER_ALLOW_UNAUTHENTICATED: "true", EDGE_EVER_RUNTIME: "self-hosted-bun" };+ const server = Bun.serve({ hostname: "127.0.0.1", port: 0, fetch: request => fetchEdgeEverApp(request, env, { waitUntil() {}, passThroughOnException() {} }) });+ servers.push(server);+ let intercept = null;+ const client = createEdgeEverClient({ baseUrl: server.url.toString(), fetch: (url, init) => intercept?.(url, init) ?? fetch(url, init) });+ await client.getSession();+ await client.getImportCapabilities();+ return { client, db, env, intercept: (fn) => { intercept = fn; } };+};++describe("native third-party import through the real API", () => {+ test("imports growing notebooks without recounting previously imported notes", async () => {+ for (const count of [64, 256]) {+ const { client, env } = await target();+ const queries = [];+ const prepare = env.storage.db.prepare.bind(env.storage.db);+ env.storage.db.prepare = (sql) => { queries.push(sql); return prepare(sql); };+ const session = new NoteImportSession("obsidian", Array.from({ length: count }, (_, index) => ({+ path: `Budget/note-${index}.md`, file: new File(["# Budget\n\nBody"], `note-${index}.md`),+ })), new MemoryImportIndex());+ await session.scan();+ const result = await executeNoteImport({ reader: session, index: session.index, client, convertEnml: convertImportEnml });+ expect(result.completed).toBe(count);+ expect(result.failed).toBe(0);+ // Only the newly created, empty folder needs a full Notebook response.+ expect(queries.filter(sql => /COUNT\(m\.id\) AS memo_count/.test(sql))).toHaveLength(1);+ await session.close();+ }+ });++ test("creating child folders does not recount their populated parent", async () => {+ const { client, env, db } = await target();+ const { notebook: parent } = await client.createNotebook({ name: "Parent budget" });+ db.transaction(() => {+ for (let index = 0; index < 100; index++) db.query(+ "INSERT INTO memos(id,workspace_id,notebook_id,title) VALUES(?,'ws_default',?,'Existing')",+ ).run(`memo_parent_budget_${index}`, parent.id);+ })();+ let joinedMemoRows = 0;+ const prepare = env.storage.db.prepare.bind(env.storage.db);+ env.storage.db.prepare = (sql) => {+ const statement = prepare(sql);+ if (!/COUNT\(m\.id\) AS memo_count/.test(sql)) return statement;+ const bind = statement.bind.bind(statement);+ statement.bind = (...values) => {+ const bound = bind(...values), first = bound.first.bind(bound);+ bound.first = async (...args) => { const row = await first(...args); joinedMemoRows += row?.memo_count ?? 0; return row; };+ return bound;+ };+ return statement;+ };+ for (let index = 0; index < 8; index++) await client.createNotebook({ name: `Child ${index}`, parentId: parent.id });+ expect(joinedMemoRows).toBe(0);+ });++ for (const [format, fixture, kind, counts] of [+ ["evernote", "evernote", null, [3, 1, 1]],+ ["evernote", "evernote", "zip", [3, 1, 1]],+ ["obsidian", "obsidian", null, [1, 1, 2]],+ ["obsidian", "obsidian", "zip", [1, 1, 2]],+ ["joplin", "joplin", null, [2, 1, 1]],+ ["joplin", "joplin", "jex", [2, 1, 1]],+ ["joplin", "joplin-markdown", null, [2, 1, 1]],+ ["joplin", "joplin-markdown", "zip", [2, 1, 1]],+ ]) test(`${format} ${kind ?? "directory"}: preflight, import, resource readback, and timestamps`, async () => {+ const { client, db } = await target();+ const files = await fixtureFiles(fixture);+ const session = new NoteImportSession(format, kind ? await archive(files, kind) : files, new MemoryImportIndex());+ const before = db.query("SELECT COUNT(*) AS n FROM memos").get().n;+ const summary = await session.scan();+ expect(summary.errors).toBe(0);+ expect([summary.notebooks, summary.notes, summary.resources]).toEqual(counts);+ expect(db.query("SELECT COUNT(*) AS n FROM memos").get().n).toBe(before);+ const result = await executeNoteImport({ reader: session, index: session.index, client, convertEnml: convertImportEnml });+ expect((await session.index.page("result", 0, 10)).map(item => item.error)).toEqual([undefined]);+ expect(result.completed).toBe(1);+ expect(result.failed).toBe(0);+ const states = await session.index.page("result", 0, 10);+ const memo = (await client.getMemo(states[0].memo.id)).memo;+ const { notebooks } = await client.listNotebooks();+ const chain = [];+ let folder = notebooks.find(item => item.id === memo.notebookId);+ while (folder) { chain.unshift(folder.name); folder = notebooks.find(item => item.id === folder.parentId); }+ const preview = await session.index.page("notebook");+ for (const [index, folder] of preview.entries()) if (folder.parentKey) {+ expect(preview.findIndex(parent => parent.key === folder.parentKey)).toBeLessThan(index);+ }+ expect(chain).toEqual(format === "evernote" ? ["工作", "项目", "资料"] : format === "obsidian" ? ["研究"] : fixture === "joplin" ? ["项目", "工作/研究"] : ["工作", "项目"]);+ expect(memo.createdAt).toBe("2020-01-02T03:04:05.000Z");+ expect(memo.updatedAt).toBe("2021-02-03T04:05:06.000Z");+ expect(memo.contentMarkdown).toContain("/api/v1/resources/");+ if (format === "obsidian") {+ expect(memo.tags).toEqual(["迁移", "资料"]);+ expect(memo.contentMarkdown).toContain("author: Test Author");+ expect(memo.contentMarkdown).toContain("``");+ expect(memo.contentMarkdown).toContain("![[missing.png]]");+ }+ if (format === "joplin") expect(memo.tags).toEqual(fixture === "joplin" ? ["研究"] : ["迁移"]);+ const rows = db.query("SELECT id, filename FROM resources WHERE memo_id = ? AND is_deleted = 0").all(memo.id);+ expect(rows).toHaveLength(counts[2]);+ for (const row of rows) {+ const blob = await client.getResourceBlob(`/api/v1/resources/${row.id}/blob`);+ expect(blob.size).toBeGreaterThan(0);+ if (row.filename.endsWith(".txt")) expect(await blob.text()).toBe("hello");+ }+ const repeated = await executeNoteImport({ reader: session, index: session.index, client, convertEnml: convertImportEnml });+ expect(repeated.completed).toBe(1);+ expect(db.query("SELECT COUNT(*) AS n FROM memos").get().n).toBe(before + 1);+ await session.close();+ db.close();+ });++ test("REST import is scoped, preserves existing caller contracts, and replays a fixed key", async () => {+ const { client, db } = await target();+ expect(await client.getImportCapabilities()).toMatchObject({ protocolVersion: 1, maxBatchSize: 25 });+ const before = db.query("SELECT COUNT(*) AS n FROM memos").get().n;+ const { notebook } = await client.createNotebook({ name: "Existing" });+ const { memo } = await client.createMemo({ notebookId: notebook.id, title: "Existing", contentMarkdown: "keep" });+ const input = { importId: crypto.randomUUID(), format: "obsidian", notebookId: notebook.id, items: [{ externalId: "one", title: "Added", contentMarkdown: "new" }], dryRun: true };+ expect((await client.importMemos(input)).wouldCreate).toBe(1);+ expect(db.query("SELECT COUNT(*) AS n FROM memos").get().n).toBe(before + 1);+ input.dryRun = false;+ const first = await client.importMemos(input);+ const second = await client.importMemos(input);+ expect(first.created).toBe(1);+ expect(second.skipped).toBe(1);+ expect(second.results[0].memo.id).toBe(first.results[0].memo.id);+ expect((await client.getMemo(memo.id)).memo.contentMarkdown).toBe("keep");+ db.exec("INSERT INTO workspaces(id,name) VALUES('ws_import_foreign','Foreign'); INSERT INTO notebooks(id,workspace_id,name) VALUES('nb_import_foreign','ws_import_foreign','Foreign')");+ await expect(client.importMemos({ ...input, notebookId: "nb_import_foreign" })).rejects.toMatchObject({ status: 404 });+ db.query("UPDATE notebooks SET is_deleted=1 WHERE id=?").run(notebook.id);+ await expect(client.importMemos(input)).rejects.toMatchObject({ status: 404 });+ db.close();+ });++ test("a bound import context cannot write into a changed account", async () => {+ const { client, db } = await target();+ const capabilities = await client.getImportCapabilities();+ expect(typeof capabilities.importContext).toBe("string");+ const before = db.query("SELECT COUNT(*) AS n FROM notebooks").get().n;+ const base = servers.at(-1).url.toString();+ const changed = createEdgeEverClient({ baseUrl: base, fetch: (url, init) => {+ const headers = new Headers(init?.headers);+ headers.set("x-edgeever-import-context", "other-workspace");+ return fetch(url, { ...init, headers });+ }});+ await expect(changed.createNotebook({ name: "Must not exist" })).rejects.toThrow();+ expect(db.query("SELECT COUNT(*) AS n FROM notebooks").get().n).toBe(before);+ db.close();+ });++ test("partial attachment failure retries only missing steps and releases sync before refresh", async () => {+ const { client, db, intercept } = await target();+ const files = [+ { path: "A.md", file: new File(["# A"], "A.md") },+ { path: "B.md", file: new File(["[one](one.txt)\n\n[two](two.txt)"], "B.md") },+ { path: "C.md", file: new File(["# C"], "C.md") },+ { path: "one.txt", file: new File(["one"], "one.txt") },+ { path: "two.txt", file: new File(["two"], "two.txt") },+ ];+ const session = new NoteImportSession("obsidian", files, new MemoryImportIndex());+ expect((await session.scan()).errors).toBe(0);+ expect((await session.index.page("note")).map(note => note.path)).toEqual(["A.md", "B.md", "C.md"]);+ let uploads = 0;+ intercept((url, init) => {+ if (init?.method === "POST" && new URL(url).pathname.endsWith("/resource-uploads") && ++uploads === 2) return new Response(JSON.stringify({ error: { code: "test_failure", message: "Controlled failure" } }), { status: 503, headers: { "Content-Type": "application/json" } });+ });+ const events = [];+ const options = { reader: session, index: session.index, client, convertEnml: convertImportEnml, pauseSync: async () => { events.push("pause"); return () => { events.push("release"); }; }, refresh: async () => { expect(events.at(-1)).toBe("release"); events.push("refresh"); } };+ expect(await executeNoteImport(options)).toMatchObject({ completed: 1, failed: 1 });+ expect(db.query("SELECT title FROM memos WHERE title IN ('A','B','C') ORDER BY title").all()).toEqual([{ title: "A" }, { title: "B" }]);+ expect(await executeNoteImport(options)).toMatchObject({ completed: 3, failed: 0, resources: 2 });+ expect(db.query("SELECT COUNT(*) AS n FROM memos WHERE title IN ('A','B','C')").get().n).toBe(3);+ const b = db.query("SELECT id FROM memos WHERE title='B'").get();+ expect(db.query("SELECT COUNT(*) AS n FROM resources WHERE memo_id=?").get(b.id).n).toBe(2);+ expect(events).toEqual(["pause", "release", "refresh", "pause", "release", "refresh"]);+ await session.close(); db.close();+ });++ test("cancellation stops new note writes and still releases sync", async () => {+ const { client, db } = await target();+ const session = new NoteImportSession("obsidian", ["A", "B"].map(name => ({ path: name + ".md", file: new File([name], name + ".md") })), new MemoryImportIndex());+ await session.scan();+ const abort = new AbortController();+ let released = false;+ const result = await executeNoteImport({ reader: session, index: session.index, client, convertEnml: convertImportEnml, signal: abort.signal, pauseSync: async () => () => { released = true; }, onProgress: progress => { if (progress.phase === "notes" && progress.path === "B.md") abort.abort(); } });+ expect(result).toMatchObject({ completed: 1, cancelled: true });+ expect(released).toBe(true);+ expect(db.query("SELECT COUNT(*) AS n FROM memos WHERE title='B'").get().n).toBe(0);+ await session.close(); db.close();+ });++ test("missing resources and .notes block confirmation without writes", async () => {+ for (const files of [[{ path: "bad.notes", file: new File(["not enex"], "bad.notes") }], [{ path: "note.md", file: new File([""], "note.md") }]]) {+ const session = new NoteImportSession(files[0].path.endsWith(".notes") ? "evernote" : "obsidian", files, new MemoryImportIndex());+ expect((await session.scan()).errors).toBeGreaterThan(0);+ await session.close();+ }+ });+ test("cleanup failure still refreshes committed notes after releasing sync", async () => {+ const { client, db } = await target();+ const session = new NoteImportSession("obsidian", [{ path: "note.md", file: new File(["Body"], "note.md") }], new MemoryImportIndex());+ await session.scan();+ const finish = session.finishReading, events = [];+ session.finishReading = async () => { events.push("finish"); throw new Error("importWorkerFailed"); };+ try {+ await expect(executeNoteImport({ reader: session, index: session.index, client, convertEnml: convertImportEnml, pauseSync: async () => () => { events.push("release"); }, refresh: async () => { events.push("refresh"); } })).rejects.toThrow("importWorkerFailed");+ expect(events).toEqual(["release", "finish", "refresh"]);+ const [state] = await session.index.page("result");+ expect(state.complete).toBe(true);+ expect((await client.getMemo(state.memo.id)).memo.contentMarkdown).toBe("Body");+ } finally { session.finishReading = finish; await session.close(); db.close(); }+ });++ for (const selectedParent of [true, false]) test(`source folder __destination preserves ${selectedParent ? "selected-parent hierarchy" : "top-level retries"}`, async () => {+ const { client, db, intercept } = await target();+ const files = await fixtureFiles("evernote");+ files[0].path = "__destination.enex";+ const session = new NoteImportSession("evernote", files, new MemoryImportIndex());+ expect(await session.scan()).toMatchObject({ notes: 1, notebooks: 1, errors: 0 });+ const parentId = selectedParent ? (await client.createNotebook({ name: "Chosen parent" })).notebook.id : null;+ const before = db.query("SELECT COUNT(*) AS n FROM memos").get().n;+ const options = { reader: session, index: session.index, client, parentId, convertEnml: convertImportEnml };+ if (!selectedParent) {+ intercept((url, init) => init?.method === "POST" && new URL(url).pathname.endsWith("/imports/memos") ? new Response(JSON.stringify({ error: { code: "test_failure", message: "Controlled rejection" } }), { status: 503, headers: { "Content-Type": "application/json" } }) : undefined);+ expect(await executeNoteImport(options)).toMatchObject({ completed: 0, failed: 1 });+ intercept(null);+ }+ expect(await executeNoteImport(options)).toMatchObject({ completed: 1, failed: 0 });+ const [state] = await session.index.page("result");+ const memo = (await client.getMemo(state.memo.id)).memo;+ const folders = (await client.listNotebooks()).notebooks.filter(folder => folder.name === "__destination");+ expect(folders).toHaveLength(1);+ expect(folders[0]).toMatchObject({ id: memo.notebookId, parentId });+ expect(db.query("SELECT COUNT(*) AS n FROM memos").get().n).toBe(before + 1);+ await session.close(); db.close();+ });++ test("a concurrent human edit is not overwritten by link rewriting", async () => {+ const { client, db, intercept } = await target();+ const session = new NoteImportSession("obsidian", await fixtureFiles("obsidian"), new MemoryImportIndex());+ await session.scan();+ let changed = false;+ intercept(async (url, init) => {+ if (!changed && init?.method === "POST" && new URL(url).pathname.endsWith("/edit-sessions")) {+ changed = true;+ const id = new URL(url).pathname.split("/").at(-2);+ const { memo } = await client.getMemo(id);+ const { editSession } = await client.createMemoEditSession(id);+ await client.updateMemo(id, { contentMarkdown: "Human edit", expectedRevision: memo.revision, expectedContentHash: memo.contentHash, editSessionId: editSession.id });+ }+ return fetch(url, init);+ });+ const outcome = await executeNoteImport({ reader: session, index: session.index, client, convertEnml: convertImportEnml });+ expect(outcome).toMatchObject({ completed: 0, failed: 1, failure: { message: "importConflict" } });+ const [state] = await session.index.page("result");+ expect((await client.getMemo(state.memo.id)).memo.contentMarkdown).toBe("Human edit");+ await session.close(); db.close();+ });++ test("a lost final response is reconciled without duplicating the note or attachments", async () => {+ const { client, db, intercept } = await target();+ const session = new NoteImportSession("obsidian", await fixtureFiles("obsidian"), new MemoryImportIndex());+ await session.scan();+ let lost = false;+ intercept(async (url, init) => {+ const response = await fetch(url, init);+ if (!lost && init?.method === "PATCH" && /\/memos\/[^/]+$/.test(new URL(url).pathname)) {+ lost = true;+ throw new TypeError("Controlled lost response");+ }+ return response;+ });+ const options = { reader: session, index: session.index, client, convertEnml: convertImportEnml };+ expect(await executeNoteImport(options)).toMatchObject({ completed: 0, failed: 1, failure: { uncertain: true } });+ const count = db.query("SELECT COUNT(*) AS n FROM resources").get().n;+ expect(await executeNoteImport(options)).toMatchObject({ completed: 1, failed: 0 });+ expect(db.query("SELECT COUNT(*) AS n FROM resources").get().n).toBe(count);+ await session.close(); db.close();+ });++ test("a missing but in-root relative reference is reported as missing, not an escape", async () => {+ const session = new NoteImportSession("obsidian", [{ path: "folder/note.md", file: new File(["[file](../missing.txt)"], "note.md") }], new MemoryImportIndex());+ await session.scan();+ expect((await session.index.page("issue")).map(item => item.code)).toContain("missingResource");+ await session.close();+ });++ test("cancelling an incomplete note retains its result without a transport error", async () => {+ const { client, db } = await target();+ const session = new NoteImportSession("obsidian", [+ { path: "note.md", file: new File(["[file](file.txt)"], "note.md") },+ { path: "file.txt", file: new File(["hello"], "file.txt") },+ ], new MemoryImportIndex());+ await session.scan();+ const abort = new AbortController();+ const outcome = await executeNoteImport({ reader: session, index: session.index, client, convertEnml: convertImportEnml, signal: abort.signal, onProgress: progress => { if (progress.phase === "resources") abort.abort(); } });+ expect(outcome).toMatchObject({ cancelled: true, completed: 0, failure: { message: "incomplete" } });+ const [state] = await session.index.page("result");+ expect(state).toMatchObject({ complete: false, error: "incomplete" });+ expect((await client.getMemo(state.memo.id)).memo).toBeDefined();+ await session.close(); db.close();+ });++ for (const format of ["evernote", "obsidian", "joplin"]) test(`${format}: extra tags are ignored without blocking import or idempotent retry`, async () => {+ const { client, db, intercept } = await target();+ const count = format === "obsidian" ? 130 : 30;+ const tags = Array.from({ length: count }, (_, i) => `tag${i}`);+ const rawTags = [" ", "#", "##", " tag0 ", "###tag0", "#tag0", ...tags.slice(1), "tag1"];+ let files;+ if (format === "evernote") {+ files = await fixtureFiles("evernote");+ files = await Promise.all(files.map(async input => ({ ...input, file: new File([(await input.file.text()).replace("<tag>研究</tag>", rawTags.map(tag => `<tag>${tag}</tag>`).join(""))], input.file.name) })));+ } else if (format === "joplin") {+ files = (await fixtureFiles("joplin")).filter(input => !/^(4444|5555)/.test(input.path));+ for (const [i, tag] of rawTags.entries()) {+ const tagId = (1000 + i).toString(16).padStart(32, "0");+ const associationId = (2000 + i).toString(16).padStart(32, "0");+ files.push({ path: tagId + ".md", file: new File([`${tag}\n\nid: ${tagId}\ntype_: 5\n`], tagId + ".md") });+ files.push({ path: associationId + ".md", file: new File([`\n\nid: ${associationId}\nnote_id: 22222222222222222222222222222222\ntag_id: ${tagId}\ntype_: 6\n`], associationId + ".md") });+ }+ } else {+ files = [{ path: "Tags.md", file: new File([`---\ntitle: Tags\ntags: ${JSON.stringify(rawTags)}\ncreated: 2020-01-02T03:04:05Z\nupdated: 2021-02-03T04:05:06Z\n---\n\nBody`], "Tags.md") }];+ }+ const session = new NoteImportSession(format, files, new MemoryImportIndex());+ const summary = await session.scan();+ expect(summary.errors).toBe(0);+ const warning = (await session.index.page("issue")).find(issue => issue.code === "ignoredTags");+ expect(warning).toMatchObject({ severity: "warning", detail: String(count - 24) });+ expect((await session.index.page("note"))[0].tags).toEqual(tags.slice(0, 24));+ const before = db.query("SELECT COUNT(*) AS n FROM memos").get().n;+ let lost = false;+ if (format === "obsidian") intercept(async (url, init) => {+ const response = await fetch(url, init);+ if (!lost && init?.method === "POST" && new URL(url).pathname.endsWith("/imports/memos")) {+ lost = true;+ throw new TypeError("Controlled lost create response");+ }+ return response;+ });+ const options = { reader: session, index: session.index, client, convertEnml: convertImportEnml };+ const first = await executeNoteImport(options);+ if (format === "obsidian") expect(first).toMatchObject({ completed: 0, failed: 1 });+ else expect(first).toMatchObject({ completed: 1, failed: 0 });+ expect(await executeNoteImport(options)).toMatchObject({ completed: 1, failed: 0 });+ const [state] = await session.index.page("result");+ expect((await client.getMemo(state.memo.id)).memo.tags).toEqual(tags.slice(0, 24));+ expect(db.query("SELECT COUNT(*) AS n FROM memos").get().n).toBe(before + 1);+ await session.close(); db.close();+ });++ test("an uncertain attachment commit stays blocked across repeated retries", async () => {+ const { client, db, intercept } = await target();+ const session = new NoteImportSession("obsidian", [+ { path: "note.md", file: new File(["[file](file.txt)"], "note.md") },+ { path: "file.txt", file: new File(["hello"], "file.txt") },+ ], new MemoryImportIndex());+ await session.scan();+ let lost = false;+ intercept(async (url, init) => {+ const response = await fetch(url, init);+ if (!lost && init?.method === "POST" && new URL(url).pathname.endsWith("/complete")) {+ lost = true; throw new TypeError("Lost attachment commit response");+ }+ return response;+ });+ const uncertain = [], counts = [];+ for (let retry = 0; retry < 3; retry++) {+ await executeNoteImport({ reader: session, index: session.index, client, convertEnml: convertImportEnml });+ uncertain.push((await session.index.page("result"))[0].uncertain);+ counts.push(db.query("SELECT COUNT(*) AS n FROM resources WHERE memo_id=?").get((await session.index.page("result"))[0].memo.id).n);+ }+ expect(counts).toEqual([1, 1, 1]);+ expect(uncertain).toEqual([true, true, true]);+ await session.close(); db.close();+ });++ for (const exit of ["failure", "cancel"]) test(`partial folder ${exit} refreshes after releasing sync before any note exists`, async () => {+ const { client, db, intercept } = await target();+ const session = new NoteImportSession("obsidian", [{ path: "Parent/Child/note.md", file: new File(["body"], "note.md") }], new MemoryImportIndex());+ await session.scan();+ let folders = 0;+ intercept((url, init) => {+ if (exit === "failure" && init?.method === "POST" && new URL(url).pathname.endsWith("/notebooks") && ++folders === 2) return new Response(JSON.stringify({ error: { code: "test_failure", message: "Folder failure" } }), { status: 503, headers: { "Content-Type": "application/json" } });+ });+ const abort = new AbortController(), events = [];+ const options = { reader: session, index: session.index, client, signal: abort.signal, convertEnml: convertImportEnml,+ pauseSync: async () => { events.push("pause"); return () => events.push("release"); },+ refresh: async () => { events.push("refresh"); expect((await client.listNotebooks()).notebooks.some(n => n.name === "Parent")).toBe(true); },+ onProgress: progress => { if (exit === "cancel" && progress.phase === "folders" && progress.completed === 1) abort.abort(); },+ };+ if (exit === "failure") await expect(executeNoteImport(options)).rejects.toThrow("Folder failure");+ else expect(await executeNoteImport(options)).toMatchObject({ cancelled: true, completed: 0 });+ expect(events).toEqual(["pause", "release", "refresh"]);+ expect(db.query("SELECT COUNT(*) AS n FROM notebooks WHERE name='Parent'").get().n).toBe(1);+ await session.close(); db.close();+ });++ test("failed resource mapping persistence cannot cause a duplicate commit on retry", async () => {+ const { client, db, intercept } = await target();+ let committed = false;+ class FailingIndex extends MemoryImportIndex {+ async put(kind, key, value) {+ if (committed && kind === "result") throw new DOMException("Mapping write failed", "QuotaExceededError");+ return super.put(kind, key, structuredClone(value));+ }+ async get(kind, key) { return structuredClone(await super.get(kind, key)); }+ async page(...args) { return structuredClone(await super.page(...args)); }+ }+ const session = new NoteImportSession("obsidian", [+ { path: "note.md", file: new File(["[file](file.txt)"], "note.md") },+ { path: "file.txt", file: new File(["hello"], "file.txt") },+ ], new FailingIndex());+ await session.scan();+ intercept(async (url, init) => {+ const response = await fetch(url, init);+ if (init?.method === "POST" && new URL(url).pathname.endsWith("/complete")) committed = true;+ return response;+ });+ const options = { reader: session, index: session.index, client, convertEnml: convertImportEnml };+ await expect(executeNoteImport(options)).rejects.toThrow("Mapping write failed");+ committed = false;+ expect((await session.index.page("result"))[0].uncertain).toBe(true);+ await executeNoteImport(options);+ expect(db.query("SELECT COUNT(*) AS n FROM resources WHERE memo_id=?").get((await session.index.page("result"))[0].memo.id).n).toBe(1);+ await session.close(); db.close();+ });++ for (const phase of ["before", "after"]) test(`folder persistence failure ${phase} creation cannot duplicate the directory`, async () => {+ const { client, db, intercept } = await target();+ let quota = phase === "before";+ class FailingIndex extends MemoryImportIndex {+ async put(kind, key, value) {+ if (quota && kind === "folderResult" && key.startsWith("folder:")) throw new DOMException("Folder mapping write failed", "QuotaExceededError");+ return super.put(kind, key, structuredClone(value));+ }+ async get(...args) { return structuredClone(await super.get(...args)); }+ async page(...args) { return structuredClone(await super.page(...args)); }+ }+ const session = new NoteImportSession("obsidian", [{ path: "Quota folder/note.md", file: new File(["Body"], "note.md") }], new FailingIndex());+ await session.scan();+ intercept(async (url, init) => {+ const response = await fetch(url, init);+ if (phase === "after" && init?.method === "POST" && new URL(url).pathname.endsWith("/notebooks") && response.ok) quota = true;+ return response;+ });+ const options = { reader: session, index: session.index, client, convertEnml: convertImportEnml };+ await expect(executeNoteImport(options)).rejects.toThrow("Folder mapping write failed");+ expect(db.query("SELECT COUNT(*) AS n FROM notebooks WHERE name='Quota folder'").get().n).toBe(phase === "before" ? 0 : 1);+ quota = false;+ if (phase === "before") expect(await executeNoteImport(options)).toMatchObject({ completed: 1, failed: 0 });+ else {+ expect(await session.index.get("folderResult", "folder:Quota folder")).toMatchObject({ uncertain: true });+ await expect(executeNoteImport(options)).rejects.toThrow("uncertainNotebook");+ }+ expect(db.query("SELECT COUNT(*) AS n FROM notebooks WHERE name='Quota folder'").get().n).toBe(1);+ await session.close(); db.close();+ });++ test("duplicate ENEX resources leave no chunks after releasing their note", async () => {+ const { client, db } = await target();+ const resource = '<resource><data encoding="base64">aGVsbG8=</data><mime>text/plain</mime><resource-attributes><file-name>file.txt</file-name></resource-attributes></resource>';+ const enex = `<en-export><note><title>Duplicate resources</title><content><![CDATA[<en-note>Body</en-note>]]></content><created>20200101T000000Z</created><updated>20200102T000000Z</updated>${resource}${resource}</note></en-export>`;+ const session = new NoteImportSession("evernote", [{ path: "notes.enex", file: new File([enex], "notes.enex") }], new MemoryImportIndex());+ expect(await session.scan()).toMatchObject({ notes: 1, resources: 1, errors: 0 });+ const release = session.releaseNote.bind(session), chunksAfterRelease = [];+ session.releaseNote = async note => { await release(note); chunksAfterRelease.push(await session.index.count("chunk")); };+ expect(await executeNoteImport({ reader: session, index: session.index, client, convertEnml: convertImportEnml })).toMatchObject({ completed: 1, resources: 1 });+ expect(chunksAfterRelease).toEqual([0]);+ const [state] = await session.index.page("result");+ expect(await (await client.getResourceBlob(Object.values(state.urls)[0])).text()).toBe("hello");+ await session.close(); db.close();+ });++ test("unreadable encrypted ZIP entries finish preflight instead of hanging", async () => {+ const writer = new ZipWriter(new Uint8ArrayWriter(), { password: "fixture-password", useWebWorkers: false });+ await writer.add("note.md", new Uint8ArrayReader(new TextEncoder().encode("Encrypted note")));+ const session = new NoteImportSession("obsidian", [{ path: "vault.zip", file: new File([await writer.close()], "vault.zip") }], new MemoryImportIndex());+ try {+ expect((await session.scan()).errors).toBeGreaterThan(0);+ expect((await session.index.page("issue")).some(issue => issue.code === "invalidArchive")).toBe(true);+ } finally { await session.close(); }+ }, 3000);++ test("ZIP initialization cancellation settles its pending byte read", async () => {+ const session = new NoteImportSession("obsidian", await archive([{ path: "note.md", file: new File(["Body"], "note.md") }], "zip"), new MemoryImportIndex());+ await session.sources.scan();+ const [entry] = await session.index.page("entry"), abort = new AbortController();+ const bytes = session.sources.bytes(entry, abort.signal), pending = bytes.next();+ abort.abort();+ try { await expect(pending).rejects.toThrow(); }+ finally { await bytes.return(); await session.close(); }+ }, 3000);++ for (const name of ["toString", "constructor", "__proto__", "file.constructor"]) test(`legal prototype-like resource ${name} uploads and retries without duplication`, async () => {+ const { client, db, intercept } = await target();+ class CloningIndex extends MemoryImportIndex {+ async put(kind, key, value) { return super.put(kind, key, structuredClone(value)); }+ async get(...args) { return structuredClone(await super.get(...args)); }+ async page(...args) { return structuredClone(await super.page(...args)); }+ }+ const session = new NoteImportSession("obsidian", [+ { path: "note.md", file: new File([`[file](${name})`], "note.md") },+ { path: name, file: new File(["Attachment bytes"], name) },+ ], new CloningIndex());+ expect(await session.scan()).toMatchObject({ notes: 1, resources: 1, errors: 0 });+ let rejected = false;+ intercept((url, init) => {+ if (!rejected && init?.method === "PATCH" && /\/memos\/[^/]+$/.test(new URL(url).pathname)) {+ rejected = true;+ return new Response(JSON.stringify({ error: { code: "test_unavailable", message: "Try later" } }), { status: 503, headers: { "Content-Type": "application/json" } });+ }+ });+ const options = { reader: session, index: session.index, client, convertEnml: convertImportEnml };+ expect(await executeNoteImport(options)).toMatchObject({ completed: 0, failed: 1 });+ expect(await executeNoteImport(options)).toMatchObject({ completed: 1, failed: 0, resources: 1 });+ const [state] = await session.index.page("result");+ expect(Object.hasOwn(state.urls, name)).toBe(true);+ expect((await client.getMemo(state.memo.id)).memo.contentMarkdown).toBe(`[file](${state.urls[name]})`);+ expect(db.query("SELECT COUNT(*) AS n FROM resources WHERE memo_id=?").get(state.memo.id).n).toBe(1);+ expect(await (await client.getResourceBlob(state.urls[name])).text()).toBe("Attachment bytes");+ await session.close(); db.close();+ });++ test("completed large-note results retain version baselines instead of full bodies", async () => {+ const { client, db } = await target();+ const body = "x".repeat(1024 * 1024) + "\n\n[file](file.txt)";+ const session = new NoteImportSession("obsidian", [+ { path: "Memory/first.md", file: new File([body], "first.md") },+ { path: "Memory/second.md", file: new File([body], "second.md") },+ { path: "Memory/file.txt", file: new File(["Attachment bytes"], "file.txt") },+ ], new MemoryImportIndex());+ expect((await session.scan()).errors).toBe(0);+ expect(await executeNoteImport({ reader: session, index: session.index, client, convertEnml: convertImportEnml })).toMatchObject({ completed: 2, resources: 2 });+ const states = await session.index.page("result");+ for (const state of states) {+ expect(state.memo).not.toHaveProperty("contentMarkdown");+ expect(state.memo).not.toHaveProperty("contentJson");+ expect(state.memo).not.toHaveProperty("contentText");+ expect(state.intendedMarkdown).toBeUndefined();+ }+ expect(JSON.stringify(states).length).toBeLessThan(8192);+ await session.close(); db.close();+ });++ test("Evernote todo labels and adjacent paragraphs survive real import", async () => {+ const { client, db } = await target();+ const files = await fixtureFiles("evernote");+ const enml = '<en-note><div><en-todo checked="true"/>完成</div><div><en-todo checked="false"/>未完成</div><div>尾文</div></en-note>';+ files[0].file = new File([(await files[0].file.text()).replace(/<content><!\[CDATA\[[\s\S]*?\]\]><\/content>/, `<content><![CDATA[${enml}]]></content>`)], files[0].file.name);+ const session = new NoteImportSession("evernote", files, new MemoryImportIndex());+ expect((await session.scan()).errors).toBe(0);+ await executeNoteImport({ reader: session, index: session.index, client, convertEnml: convertImportEnml });+ const [state] = await session.index.page("result");+ const { memo } = await client.getMemo(state.memo.id);+ expect(memo.contentMarkdown).toContain("- [x] 完成");+ expect(memo.contentMarkdown).toContain("- [ ] 未完成");+ expect(memo.contentMarkdown).toContain("尾文");+ await session.close(); db.close();+ });++ test("nested links, labels, titles and referenced definitions retain Markdown structure", async () => {+ const { client, db } = await target();+ const body = '[](assets/manual.txt "Document")\n\n[section [A]](assets/manual.txt "See manual")\n\n[`code label`](assets/manual.txt)\n\n![reference][image]\n\n[image][]\n\n[image]\n\n[image]: assets/image.svg "Ref title"\n[unused]: missing.png\n\n``';+ const session = new NoteImportSession("obsidian", [+ { path: "note.md", file: new File([body], "note.md") },+ { path: "assets/image.svg", file: new File(['<svg xmlns="http://www.w3.org/2000/svg"/>'], "image.svg") },+ { path: "assets/manual.txt", file: new File(["manual"], "manual.txt") },+ ], new MemoryImportIndex());+ expect((await session.scan()).errors).toBe(0);+ expect(await executeNoteImport({ reader: session, index: session.index, client, convertEnml: convertImportEnml })).toMatchObject({ completed: 1, resources: 2 });+ const [state] = await session.index.page("result");+ const { memo } = await client.getMemo(state.memo.id);+ const image = state.urls["assets/image.svg"], manual = state.urls["assets/manual.txt"];+ expect(memo.contentMarkdown).toContain(`[](${manual} "Document")`);+ expect(memo.contentMarkdown).toContain(`[section [A]](${manual} "See manual")`);+ expect(memo.contentMarkdown).toContain('[`code label`](' + manual + ')');+ expect(memo.contentMarkdown).toContain('![reference][image]');+ expect(memo.contentMarkdown).toContain('[image][]');+ expect(memo.contentMarkdown).toContain('\n\n[image]\n');+ expect(memo.contentMarkdown).toContain(`[image]: ${image} "Ref title"`);+ expect(memo.contentMarkdown).toContain('[unused]: missing.png');+ expect(memo.contentMarkdown).toContain('``');+ const nodes = [];+ const visit = node => { nodes.push(node); node.content?.forEach(visit); };+ visit(memo.contentJson);+ expect(nodes.filter(node => node.type === "image").map(node => node.attrs.src)).toEqual([image, image]);+ expect(nodes.some(node => node.type === "image" && node.attrs.alt === "thumb")).toBe(true);+ expect(nodes.find(node => node.type === "image" && node.attrs.alt === "thumb").attrs.link).toEqual({ href: manual, title: "Document" });+ const edited = structuredClone(memo.contentJson);+ edited.content.push({ type: "paragraph", content: [{ type: "text", text: "Edited after import" }] });+ const { editSession } = await client.createMemoEditSession(memo.id);+ await client.updateMemo(memo.id, { contentJson: edited, contentMarkdown: docToMarkdown(edited), expectedRevision: memo.revision, expectedContentHash: memo.contentHash, editSessionId: editSession.id });+ const reopened = (await client.getMemo(memo.id)).memo;+ expect(reopened.contentMarkdown).toContain(`[](${manual} "Document")`);+ expect(reopened.contentMarkdown).toContain("Edited after import");+ await session.close(); db.close();+ });++ for (const kind of ["zip", "jex"]) test(`directory ${kind} attachment is not expanded as another import`, async () => {+ const { client, db } = await target();+ const [container] = await archive([{ path: "inside.md", file: new File(["Inside archive, not a note to import"], "inside.md") }], kind);+ const path = `assets/bundle.${kind}`;+ const session = new NoteImportSession("obsidian", [+ { path: "notes/main.md", file: new File([`[bundle](../${path})`], "main.md"), expandArchive: false },+ { path, file: container.file, expandArchive: false },+ ], new MemoryImportIndex());+ expect(await session.scan()).toMatchObject({ errors: 0, notes: 1, notebooks: 1, resources: 1 });+ expect(await executeNoteImport({ reader: session, index: session.index, client, convertEnml: convertImportEnml })).toMatchObject({ completed: 1, resources: 1 });+ const [state] = await session.index.page("result");+ expect(new Uint8Array(await (await client.getResourceBlob(state.urls[path])).arrayBuffer())).toEqual(new Uint8Array(await container.file.arrayBuffer()));+ await session.close(); db.close();+ });++ test("Vault-root folder paths win collisions and Wiki percent names remain literal", async () => {+ const { client, db } = await target();+ const session = new NoteImportSession("obsidian", [+ { path: "notes/main.md", file: new File(['![[assets/diagram.svg]]\n\n\n\n![[100% ready.svg]]'], "main.md") },+ { path: "assets/diagram.svg", file: new File(['<svg xmlns="http://www.w3.org/2000/svg"><text>ROOT</text></svg>'], "diagram.svg") },+ { path: "notes/assets/diagram.svg", file: new File(['<svg xmlns="http://www.w3.org/2000/svg"><text>LOCAL</text></svg>'], "diagram.svg") },+ { path: "100% ready.svg", file: new File(['<svg xmlns="http://www.w3.org/2000/svg"><text>Percent</text></svg>'], "100% ready.svg") },+ ], new MemoryImportIndex());+ expect(await session.scan()).toMatchObject({ errors: 0, notes: 1, resources: 2 });+ expect(await executeNoteImport({ reader: session, index: session.index, client, convertEnml: convertImportEnml })).toMatchObject({ completed: 1, resources: 2 });+ const [state] = await session.index.page("result");+ expect(await (await client.getResourceBlob(state.urls["assets/diagram.svg"])).text()).toContain("ROOT");+ expect(await (await client.getResourceBlob(state.urls["100% ready.svg"])).text()).toContain("Percent");+ await session.close(); db.close();+ });++ test("Wiki PDF embeds and escaped fragments become consumable attachment links", async () => {+ const { client, db } = await target();+ const session = new NoteImportSession("obsidian", [+ { path: "note.md", file: new File(['![[paper.pdf#page=3]]\n\n[pdf](paper.pdf#name\\)value)'], "note.md") },+ { path: "paper.pdf", file: new File(["%PDF-1.4\n%%EOF"], "paper.pdf") },+ ], new MemoryImportIndex());+ expect((await session.scan()).errors).toBe(0);+ await executeNoteImport({ reader: session, index: session.index, client, convertEnml: convertImportEnml });+ const [state] = await session.index.page("result");+ const { memo } = await client.getMemo(state.memo.id);+ expect(memo.contentMarkdown).toContain(`[paper.pdf](${state.urls["paper.pdf"]}#page=3)`);+ expect(memo.contentMarkdown).toContain(`[pdf](${state.urls["paper.pdf"]}#name%29value)`);+ expect(JSON.stringify(markdownToDoc(memo.contentMarkdown))).not.toContain('"type":"image"');+ await session.close(); db.close();+ });++ test("invalid calendar dates warn instead of inventing another original date", async () => {+ const session = new NoteImportSession("joplin", [{ path: "note.md", file: new File(['---\ncreated: 2021-02-30T00:00:00Z\nupdated: 2020-02-29T08:00:00+08:00\n---\n\nBody'], "note.md") }], new MemoryImportIndex());+ expect((await session.scan()).errors).toBe(0);+ const [note] = await session.index.page("note");+ expect(note.createdAt).toBeUndefined();+ expect(note.updatedAt).toBe("2020-02-29T00:00:00.000Z");+ expect((await session.index.page("issue")).some(issue => issue.code === "invalidTime")).toBe(true);+ await session.close();+ });++ test("renamed Joplin RAW entries are not imported again as Markdown", async () => {+ const { client, db } = await target();+ const files = await fixtureFiles("joplin");+ const note = files.find(entry => entry.path === "22222222222222222222222222222222.md");+ note.path = "Renamed.md";+ const session = new NoteImportSession("joplin", files, new MemoryImportIndex());+ expect(await session.scan()).toMatchObject({ notes: 1, notebooks: 2, resources: 1, errors: 0 });+ expect(await executeNoteImport({ reader: session, index: session.index, client, convertEnml: convertImportEnml })).toMatchObject({ completed: 1, resources: 1 });+ await session.close(); db.close();+ });++ for (const rawShaped of [false, true]) test(`RAW Markdown attachment ${rawShaped ? "with record-shaped content" : "with a large body"} is not scanned as a note`, async () => {+ const { client, db } = await target();+ const noteId = "11111111111111111111111111111111", resourceId = "33333333333333333333333333333333";+ const attachment = rawShaped ? "Attached exported note\n\nAttachment text\n\nid: 77777777777777777777777777777777\nparent_id: \ntype_: 1\n" : "# Attached Markdown\n" + "x".repeat(1024 * 1024);+ const session = new NoteImportSession("joplin", [+ { path: `${noteId}.md`, file: new File([`Root\n\n[file](:/${resourceId})\n\nid: ${noteId}\nparent_id: \ntype_: 1\n`], `${noteId}.md`) },+ { path: `${resourceId}.md`, file: new File([`attached.md\n\nid: ${resourceId}\nmime: text/markdown\nfile_extension: md\ntype_: 4\n`], `${resourceId}.md`) },+ { path: `_resources/${resourceId}.md`, file: new File([attachment], `${resourceId}.md`) },+ ], new MemoryImportIndex());+ const text = session.sources.text.bind(session.sources), attachmentReads = [];+ session.sources.text = async (entry, ...args) => { if (entry.path.startsWith("_resources/")) attachmentReads.push(entry.path); return text(entry, ...args); };+ expect(await session.scan()).toMatchObject({ notes: 1, resources: 1, errors: 0 });+ expect(attachmentReads).toEqual([]);+ expect(await executeNoteImport({ reader: session, index: session.index, client, convertEnml: convertImportEnml })).toMatchObject({ completed: 1, resources: 1 });+ const [state] = await session.index.page("result");+ expect(await (await client.getResourceBlob(Object.values(state.urls)[0])).text()).toBe(attachment);+ await session.close(); db.close();+ });++ test("a resource-shaped Markdown path without a RAW descriptor remains a note", async () => {+ const { client, db } = await target();+ const name = "33333333333333333333333333333333.md";+ const session = new NoteImportSession("joplin", [{ path: `_resources/${name}`, file: new File(["# A real Markdown note"], name) }], new MemoryImportIndex());+ expect(await session.scan()).toMatchObject({ notes: 1, notebooks: 1, resources: 0, errors: 0 });+ expect(await executeNoteImport({ reader: session, index: session.index, client, convertEnml: convertImportEnml })).toMatchObject({ completed: 1, failed: 0 });+ const [state] = await session.index.page("result");+ expect((await client.getMemo(state.memo.id)).memo.contentMarkdown).toBe("# A real Markdown note");+ await session.close(); db.close();+ });++ test("a definite attachment commit rejection remains retryable", async () => {+ const { client, db, intercept } = await target();+ const session = new NoteImportSession("obsidian", [+ { path: "note.md", file: new File(["[file](file.txt)"], "note.md") },+ { path: "file.txt", file: new File(["hello"], "file.txt") },+ ], new MemoryImportIndex());+ await session.scan();+ let rejected = false;+ intercept((url, init) => {+ if (!rejected && init?.method === "POST" && new URL(url).pathname.endsWith("/complete")) {+ rejected = true; return new Response(JSON.stringify({ error: { code: "test_quota", message: "Try later" } }), { status: 429, headers: { "Content-Type": "application/json" } });+ }+ });+ const options = { reader: session, index: session.index, client, convertEnml: convertImportEnml };+ expect(await executeNoteImport(options)).toMatchObject({ completed: 0, failed: 1, failure: { uncertain: false } });+ expect(await executeNoteImport(options)).toMatchObject({ completed: 1, failed: 0 });+ const [state] = await session.index.page("result");+ expect(db.query("SELECT COUNT(*) AS n FROM resources WHERE memo_id=?").get(state.memo.id).n).toBe(1);+ await session.close(); db.close();+ });++ test("inline HTML prefixes and CRLF link labels retain their actual resources", async () => {+ const { client, db } = await target();+ const body = '[<div></div>]\n\n\n[<div></div>]\n![[assets/image.svg]]\n\n[two\r\nlines](assets/manual.txt)\n\n';+ const session = new NoteImportSession("obsidian", [+ { path: "note.md", file: new File([body], "note.md") },+ { path: "assets/image.svg", file: new File(['<svg xmlns="http://www.w3.org/2000/svg"/>'], "image.svg") },+ { path: "assets/manual.txt", file: new File(["manual"], "manual.txt") },+ ], new MemoryImportIndex());+ expect(await session.scan()).toMatchObject({ errors: 0, resources: 2 });+ expect(await executeNoteImport({ reader: session, index: session.index, client, convertEnml: convertImportEnml })).toMatchObject({ completed: 1, resources: 2 });+ const [state] = await session.index.page("result");+ const { memo } = await client.getMemo(state.memo.id);+ expect(memo.contentMarkdown).toContain(`[<div></div>]\n`);+ expect(memo.contentMarkdown).toContain(`[two\r\nlines](${state.urls["assets/manual.txt"]})`);+ expect(memo.contentMarkdown).toContain(``);+ await session.close(); db.close();+ });++ test("large valid properties retain source metadata while ignoring extra tags", async () => {+ const { client, db } = await target();+ const tags = Array.from({ length: 6000 }, (_, i) => `tag_${String(i).padStart(5, "0")}`);+ const body = `---\ntitle: Large properties\ntags:\n${tags.map(tag => ` - ${tag}\n`).join("")}created: 2020-01-02T03:04:05Z\nupdated: 2021-02-03T04:05:06Z\nauthor: Retained Author\n---\n\nBody retained`;+ const session = new NoteImportSession("obsidian", [{ path: "note.md", file: new File([body], "note.md") }], new MemoryImportIndex());+ expect(body.length).toBeGreaterThan(64 * 1024);+ expect(await session.scan()).toMatchObject({ errors: 0, warnings: 1, notes: 1 });+ expect(await session.index.page("issue")).toMatchObject([{ code: "ignoredTags", detail: "5976" }]);+ expect(await executeNoteImport({ reader: session, index: session.index, client, convertEnml: convertImportEnml })).toMatchObject({ completed: 1, failed: 0 });+ const [state] = await session.index.page("result");+ const { memo } = await client.getMemo(state.memo.id);+ expect(memo).toMatchObject({ title: "Large properties", tags: tags.slice(0, 24), createdAt: "2020-01-02T03:04:05.000Z", updatedAt: "2021-02-03T04:05:06.000Z", contentMarkdown: body });+ await session.close(); db.close();+ });++ for (const subtree of [false, true]) test(`Joplin ${subtree ? "JEX subtree" : "RAW standalone note"} accepts an excluded ancestor`, async () => {+ const { client, db } = await target();+ const files = (await fixtureFiles("joplin")).filter(file => file.path !== "66666666666666666666666666666666.md" && (subtree || file.path !== "11111111111111111111111111111111.md"));+ const session = new NoteImportSession("joplin", subtree ? await archive(files, "jex") : files, new MemoryImportIndex());+ expect(await session.scan()).toMatchObject({ errors: 0, notes: 1, notebooks: 1, resources: 1 });+ expect(await executeNoteImport({ reader: session, index: session.index, client, convertEnml: convertImportEnml })).toMatchObject({ completed: 1, failed: 0, resources: 1 });+ const [state] = await session.index.page("result");+ const { memo } = await client.getMemo(state.memo.id);+ const { notebooks } = await client.listNotebooks();+ expect(notebooks.find(folder => folder.id === memo.notebookId)).toMatchObject({ name: subtree ? "工作/研究" : "Joplin", parentId: null });+ expect(memo.contentMarkdown).toContain("# 正文");+ expect(memo.tags).toEqual(["研究"]);+ expect(await (await client.getResourceBlob(Object.values(state.urls)[0])).text()).toBe("hello");+ await session.close(); db.close();+ });++ for (const invalid of ["cycle", "non-folder"]) test(`Joplin ${invalid} parent still blocks preflight`, async () => {+ const files = await fixtureFiles("joplin");+ const parent = files.find(file => file.path === "66666666666666666666666666666666.md");+ parent.file = new File([(await parent.file.text()).replace("parent_id:\n", "parent_id: 11111111111111111111111111111111\n").replace("type_: 2", invalid === "cycle" ? "type_: 2" : "type_: 5")], parent.file.name);+ const session = new NoteImportSession("joplin", files, new MemoryImportIndex());+ expect((await session.scan()).errors).toBeGreaterThan(0);+ expect((await session.index.page("issue")).some(issue => issue.code === "invalidJoplin" && issue.detail.includes(invalid === "cycle" ? "cyclicNotebook" : "missingNotebook"))).toBe(true);+ await session.close();+ });++ test("untitled RAW create response loss is replayed using the server title default", async () => {+ const { client, db, intercept } = await target();+ const files = await fixtureFiles("joplin");+ const note = files.find(file => file.path === "22222222222222222222222222222222.md");+ note.file = new File([(await note.file.text()).replace(/^Joplin 笔记/, "")], note.file.name);+ const session = new NoteImportSession("joplin", files, new MemoryImportIndex());+ expect((await session.scan()).errors).toBe(0);+ let lost = false;+ intercept(async (url, init) => {+ const response = await fetch(url, init);+ if (!lost && init?.method === "POST" && new URL(url).pathname.endsWith("/imports/memos")) { lost = true; throw new TypeError("Lost create response"); }+ return response;+ });+ const options = { reader: session, index: session.index, client, convertEnml: convertImportEnml };+ expect(await executeNoteImport(options)).toMatchObject({ completed: 0, failed: 1 });+ expect(await executeNoteImport(options)).toMatchObject({ completed: 1, failed: 0 });+ const [state] = await session.index.page("result");+ expect((await client.getMemo(state.memo.id)).memo.title).toBe("无标题笔记");+ await session.close(); db.close();+ });++});++describe("real desktop-app exports", () => {+ const root = new URL("./fixtures/note-import/real-apps/", import.meta.url);+ test("ENEX media and unreferenced resources retain special-character labels through real import", async () => {+ const { client, db } = await target();+ const samples = [+ { filename: "photo ] © 100%.png", mime: "image/png", media: true },+ { filename: "report [v1] ] © 100%.pdf", mime: "application/pdf", media: true },+ { filename: "explicit alt.png", alt: "Alt [ unpaired ] ] \\ value", mime: "image/png", media: true },+ { filename: "unmatched [ note\\name.txt", storedFilename: "unmatched [ note-name.txt", mime: "text/plain", media: true },+ { filename: "line\r\n\r\nimage.png", storedFilename: "lineimage.png", mime: "image/png", media: true },+ { filename: "orphan ]\r\n\r\n100%.txt", storedFilename: "orphan ]100%.txt", mime: "text/plain", media: false },+ ].map((sample, index) => {+ const data = new TextEncoder().encode(`Independent resource ${index}`);+ return { ...sample, data, hash: new Bun.CryptoHasher("md5").update(data).digest("hex") };+ });+ const xml = value => value.replaceAll("&", "&").replaceAll("<", "<").replaceAll('"', """);+ const enml = `<en-note>${samples.filter(sample => sample.media).map(sample => `<en-media type="${sample.mime}" hash="${sample.hash}"${sample.alt ? ` alt="${xml(sample.alt)}"` : ""}/>`).join("")}</en-note>`;+ const enex = `<en-export><note><title>Special labels</title><content><![CDATA[${enml}]]></content>${samples.map(sample => `<resource><data encoding="base64">${Buffer.from(sample.data).toString("base64")}</data><mime>${sample.mime}</mime><resource-attributes><file-name>${xml(sample.filename)}</file-name></resource-attributes></resource>`).join("")}</note></en-export>`;+ const session = new NoteImportSession("evernote", [{ path: "labels.enex", file: new File([enex], "labels.enex") }], new MemoryImportIndex());+ try {+ expect(await session.scan()).toMatchObject({ notes: 1, resources: samples.length, errors: 0 });+ expect(await executeNoteImport({ reader: session, index: session.index, client, convertEnml: convertImportEnml })).toMatchObject({ completed: 1, failed: 0, resources: samples.length });+ const [state] = await session.index.page("result");+ let { memo } = await client.getMemo(state.memo.id);+ expect(memo.contentMarkdown).not.toContain("evernote-resource:");+ const checkLabels = () => {+ const labels = [];+ const visit = node => {+ if (node.type === "image") labels.push(node.attrs.alt);+ if (node.type === "edgeeverPdfAttachment" || node.type === "edgeeverFileAttachment") labels.push(node.attrs.filename);+ node.content?.forEach(visit);+ };+ visit(memo.contentJson);+ for (const sample of samples) expect(labels).toContain((sample.alt ?? sample.filename).replace(/[\r\n]+/g, " "));+ };+ checkLabels();+ const { editSession } = await client.createMemoEditSession(memo.id);+ await client.updateMemo(memo.id, { contentMarkdown: docToMarkdown(memo.contentJson) + "\n\nEdited", expectedRevision: memo.revision, expectedContentHash: memo.contentHash, editSessionId: editSession.id });+ ({ memo } = await client.getMemo(memo.id));+ checkLabels();+ expect(memo.contentMarkdown).not.toContain("evernote-resource:");+ const resources = db.query("SELECT id,filename FROM resources WHERE memo_id=? AND is_deleted=0").all(memo.id);+ expect(resources).toHaveLength(samples.length);+ expect(resources.map(resource => resource.filename).sort()).toEqual(samples.map(sample => sample.storedFilename ?? sample.filename).sort());+ const received = [];+ for (const resource of resources) {+ const blob = await client.getResourceBlob(`/api/v1/resources/${resource.id}/blob`);+ received.push(await blob.text());+ }+ expect(received.sort()).toEqual(samples.map(sample => new TextDecoder().decode(sample.data)).sort());+ } finally { await session.close(); db.close(); }+ });+ test("Evernote 11.40.2 ENEX retains current task states, code blocks and original attachment bytes", async () => {+ const { client, db } = await target();+ const name = "evernote-11.40.2.enex";+ const session = new NoteImportSession("evernote", [{ path: name, file: new File([await readFile(new URL(name, root))], name) }], new MemoryImportIndex());+ const before = db.query("SELECT COUNT(*) AS n FROM memos").get().n;+ expect(await session.scan()).toMatchObject({ notes: 1, notebooks: 1, resources: 5, errors: 0 });+ expect(db.query("SELECT COUNT(*) AS n FROM memos").get().n).toBe(before);+ const options = { reader: session, index: session.index, client, convertEnml: convertImportEnml };+ expect(await executeNoteImport(options)).toMatchObject({ completed: 1, failed: 0, resources: 5 });+ const [state] = await session.index.page("result");+ const { memo } = await client.getMemo(state.memo.id);+ expect(memo).toMatchObject({ title: "EdgeEver QA · 正文与附件 🌿", tags: ["edgeever-import-qa", "迁移验收"], createdAt: "2026-10-31T03:38:38.000Z", updatedAt: "2026-10-31T03:41:05.000Z" });+ expect(memo.contentMarkdown).toMatch(/^- +\[ \] 尚未完成的导入检查/m);+ expect(memo.contentMarkdown).toMatch(/^- +\[x\] 已经完成的格式准备/m);+ expect(memo.contentMarkdown.startsWith("## 真实 ENEX 导出验收")).toBe(true);+ expect(memo.contentMarkdown).toContain("```\nconst message = 'ENEX & EdgeEver';\n```");+ expect(memo.contentMarkdown).toContain("| 项目");+ expect(memo.contentMarkdown).toContain("保留原字节");+ expect(memo.contentMarkdown).toContain("https://example.com/edgeever?q=100%25#section");+ const structure = { pdfs: [], files: [], tableRows: 0 };+ const emphasis = { bold: [], italic: [] };+ const visit = node => {+ if (node.type === "text") for (const mark of node.marks ?? []) if (Object.hasOwn(emphasis, mark.type)) emphasis[mark.type].push(node.text);+ if (node.type === "edgeeverPdfAttachment") structure.pdfs.push(node.attrs.filename);+ if (node.type === "edgeeverFileAttachment") structure.files.push(node.attrs.filename);+ if (node.type === "table") structure.tableRows = node.content.length;+ for (const child of node.content ?? []) visit(child);+ };+ visit(memo.contentJson);+ expect(structure).toEqual({ pdfs: ["迁移说明.pdf"], files: ["说明 100%.txt", "原样保留.zip"], tableRows: 2 });+ expect(emphasis.bold).toContain("粗体");+ expect(emphasis.italic).toContain("斜体");+ const resources = db.query("SELECT id,filename FROM resources WHERE is_deleted=0 AND memo_id=?").all(memo.id);+ expect(resources).toHaveLength(5);+ for (const resource of resources) {+ const blob = await client.getResourceBlob(`/api/v1/resources/${resource.id}/blob`);+ expect(new Uint8Array(await blob.arrayBuffer())).toEqual(new Uint8Array(await readFile(new URL(`assets/${encodeURIComponent(resource.filename)}`, root))));+ }+ expect(await executeNoteImport(options)).toMatchObject({ completed: 1, failed: 0, resources: 5 });+ expect(db.query("SELECT COUNT(*) AS n FROM memos").get().n).toBe(before + 1);+ expect(db.query("SELECT COUNT(*) AS n FROM resources WHERE is_deleted=0 AND memo_id=?").get(memo.id).n).toBe(5);+ await session.close(); db.close();+ });+ for (const [name, format, directory, notes, notebooks] of [+ ["obsidian-1.14.5.zip", "obsidian", false, 6, 4],+ ["obsidian-1.14.5.zip", "obsidian", true, 6, 4],+ ["joplin-3.7.21.jex", "joplin", false, 5, 3],+ ["joplin-3.7.21-raw.zip", "joplin", false, 5, 3],+ ["joplin-3.7.21-raw.zip", "joplin", true, 5, 3],+ ["joplin-3.7.21-md.zip", "joplin", true, 5, 3],+ ["joplin-3.7.21-md-frontmatter.zip", "joplin", true, 5, 3],+ ]) test(`${name} ${directory ? "directory" : "archive"}: metadata and original attachment bytes`, async () => {+ const { client, db } = await target();+ const file = new File([await readFile(new URL(name, root))], name);+ let files = [{ path: name, file }];+ if (directory) {+ const reader = new ZipReader(new BlobReader(file));+ files = [];+ for (const entry of await reader.getEntries()) if (!entry.directory) {+ const path = entry.filename.split("/").slice(1).join("/");+ files.push({ path, file: new File([await entry.getData(new Uint8ArrayWriter())], path.split("/").at(-1)), expandArchive: false });+ }+ await reader.close();+ }+ const before = db.query("SELECT COUNT(*) AS n FROM memos").get().n;+ const session = new NoteImportSession(format, files, new MemoryImportIndex());+ expect(await session.scan()).toMatchObject({ notes, notebooks, resources: 6, errors: 0 });+ expect(db.query("SELECT COUNT(*) AS n FROM memos").get().n).toBe(before);+ expect(await executeNoteImport({ reader: session, index: session.index, client, convertEnml: convertImportEnml })).toMatchObject({ completed: notes, failed: 0, resources: 6 });+ const imported = [];+ for (const state of await session.index.page("result", 0, 100)) imported.push((await client.getMemo(state.memo.id)).memo);+ expect(imported.map(note => note.title).sort()).toEqual((format === "obsidian" ? ["迁移验收 · 欢迎 🌿", "图片与附件", "研究索引", "100% 完整", "任务清单", "代码示例"] : ["欢迎 🌿", "图片与附件", "任务清单", "代码示例", "空正文"]).sort());+ const plainMarkdown = name === "joplin-3.7.21-md.zip";+ for (const memo of imported) if (!plainMarkdown) {+ expect(memo.createdAt).toBe("2020-01-02T03:04:05.000Z");+ expect(memo.updatedAt).toBe("2021-02-03T04:05:06.000Z");+ }+ const picture = imported.find(note => note.title === "图片与附件");+ expect(picture.tags).toHaveLength(plainMarkdown ? 0 : 24);+ expect(picture.tags.every(tag => /^tag(?:0[1-9]|[12][0-9]|30)$/.test(tag))).toBe(true);+ expect(picture.contentMarkdown).toContain("https://example.com/full");+ expect(picture.contentMarkdown).toContain("missing.png");+ if (format === "obsidian") expect(picture.contentMarkdown).toContain("provenance: official-obsidian-cli");+ const { notebooks: folders } = await client.listNotebooks();+ const chain = [];+ let folder = folders.find(item => item.id === picture.notebookId);+ while (folder) { chain.unshift(folder.name); folder = folders.find(item => item.id === folder.parentId); }+ expect(chain).toEqual(format === "obsidian" ? [...(directory ? [] : ["Obsidian Vault"]), "研究", "项目 A"] : ["EdgeEver 导入验收", "研究", "项目 A"]);+ const resources = db.query(`SELECT id,filename FROM resources WHERE is_deleted=0 AND memo_id IN (${imported.map(() => "?").join(",")})`).all(...imported.map(note => note.id));+ expect(resources).toHaveLength(6);+ for (const resource of resources) {+ const blob = await client.getResourceBlob(`/api/v1/resources/${resource.id}/blob`);+ expect(new Uint8Array(await blob.arrayBuffer())).toEqual(new Uint8Array(await readFile(new URL(`assets/${encodeURIComponent(resource.filename)}`, root))));+ }+ await session.close(); db.close();+ });++ test("percent decoding retains traversal and invalid-UTF-8 rejection", async () => {+ const session = new NoteImportSession("joplin", [{ path: "note.md", file: new File(["[outside](%2e%2e/secret.txt)\n\n[invalid](%FF%20.txt)"], "note.md") }], new MemoryImportIndex());+ expect(await session.scan()).toMatchObject({ errors: 2 });+ expect((await session.index.page("issue")).filter(issue => issue.code === "outsideRoot")).toHaveLength(2);+ await session.close();+ });+});diff --git a/tests/selected-markdown-export.test.ts b/tests/selected-markdown-export.test.tsindex 9a893cb9..f9a024fa 100644--- a/tests/selected-markdown-export.test.ts+++ b/tests/selected-markdown-export.test.ts@@ -60,6 +60,23 @@ const resource: Resource = {};describe("selected Markdown ZIP export", () => {+ test("retains selection IDs while forwarding continuation cursors", async () => {+ const notes = [memo({ id: "memo_cursor_a", title: "A" }), memo({ id: "memo_cursor_b", title: "B" })];+ const calls: { ids: string[]; cursor?: string }[] = [];+ const result = await exportSelectedMemosAsMarkdownZip({+ memoIds: notes.map(note => note.id),+ listNotebooks: async () => ({ notebooks: [notebook({ id: "nb_root", name: "Root" })] }),+ getPage: async (offset, _limit, ids, cursor) => {+ calls.push({ ids, cursor });+ return { memos: [notes[offset]], resources: [], ...(offset === 0 ? { totalCount: 2 } : {}),+ nextOffset: offset === 0 ? 1 : null, nextCursor: offset === 0 ? "selected-next" : null };+ },+ getResourceBlob: async () => new Blob(), save: () => {},+ });+ expect(result).toEqual({ status: "ok", exported: 2, skippedLocal: 0 });+ expect(calls).toEqual([{ ids: notes.map(note => note.id), cursor: undefined }, { ids: notes.map(note => note.id), cursor: "selected-next" }]);+ });+test("reports the actual selected export count when continuation totals are omitted", async () => {const notes = [memo({ id: "memo_first", title: "First" }), memo({ id: "memo_second", title: "Second" })];const result = await exportSelectedMemosAsMarkdownZip({