From ce587064e73879994da065bf51114119dfc848ae Mon Sep 17 00:00:00 2001 From: Claude Date: Mon, 29 Jun 2026 15:55:42 +0000 Subject: [PATCH] Add Slab importer backed by the Markdown import pipeline Slab exports a zip of Markdown documents that is structurally identical to the Outline Markdown export, with one difference: images are referenced as remote signed URLs rather than files inside the archive. Rather than build a parallel importer, this reuses the Markdown task and processor and teaches the shared import pipeline to download remote images: - Enable per-page attachment upload for the Markdown task. The base APIImportTask already downloads remote image/video/attachment URLs and rewrites them to internal redirect URLs; the Markdown task previously opted out because its attachments live in the zip. - Make the base upload step skip URLs that are already internal, so local zip attachments (resolved to redirect URLs during rewriteMarkdown and uploaded from the archive in onAllTasksCompleted) pass through untouched while remote URLs are fetched and re-hosted. Also guards the URL rewrite against nodes not present in the download map. - Introduce IntegrationService.Slab as a distinct, importable service that routes through the Markdown task/processor, so imports are tracked and labelled as "Slab" with no duplicated import logic. - Wire the create API (schema + route), the import settings UI (Slab card and dialog), and a placeholder logo asset. Co-Authored-By: Claude Opus 4.8 Claude-Session: https://claude.ai/code/session_01AC5Ho2SHBjNpxEAMr5uL6o --- app/scenes/Settings/Import.tsx | 21 ++++++++++++ .../Settings/components/DropToImport.tsx | 8 +++++ .../Settings/components/ImportSlabDialog.tsx | 32 ++++++++++++++++++ public/images/slab.png | Bin 0 -> 880 bytes .../processors/MarkdownImportsProcessor.ts | 19 ++++++----- server/queues/tasks/APIImportTask.ts | 23 +++++++++++-- .../tasks/MarkdownAPIImportTask.test.ts | 12 +++++++ server/queues/tasks/MarkdownAPIImportTask.ts | 14 ++++++-- server/routes/api/imports/imports.ts | 1 + server/routes/api/imports/schema.ts | 5 +++ shared/schema.ts | 17 +++++----- shared/types.ts | 3 ++ 12 files changed, 134 insertions(+), 21 deletions(-) create mode 100644 app/scenes/Settings/components/ImportSlabDialog.tsx create mode 100644 public/images/slab.png diff --git a/app/scenes/Settings/Import.tsx b/app/scenes/Settings/Import.tsx index 6893ae7bc1..1b58b86128 100644 --- a/app/scenes/Settings/Import.tsx +++ b/app/scenes/Settings/Import.tsx @@ -23,6 +23,7 @@ import FileOperationListItem from "./components/FileOperationListItem"; import ImportJSONDialog from "./components/ImportJSONDialog"; import { ImportListItem } from "./components/ImportListItem"; import ImportMarkdownDialog from "./components/ImportMarkdownDialog"; +import ImportSlabDialog from "./components/ImportSlabDialog"; type Config = { /** The title of the import. */ @@ -89,6 +90,26 @@ function useImportsConfig() { }, ]; + items.push({ + title: "Slab", + subtitle: t("Import a zip file of Markdown documents exported from Slab"), + icon: , + action: ( + + ), + }); + PluginManager.getHooks(Hook.Imports).forEach((plugin) => { items.push({ ...plugin.value }); }); diff --git a/app/scenes/Settings/components/DropToImport.tsx b/app/scenes/Settings/components/DropToImport.tsx index 7d943f8c74..8413b3c707 100644 --- a/app/scenes/Settings/components/DropToImport.tsx +++ b/app/scenes/Settings/components/DropToImport.tsx @@ -71,6 +71,14 @@ function DropToImport({ disabled, onSubmit, children, format }: Props) { permission: permission ?? undefined, } ); + } else if (format === IntegrationService.Slab) { + await imports.create( + { service: IntegrationService.Slab }, + { + attachmentId: attachment.id, + permission: permission ?? undefined, + } + ); } else if (format === FileOperationFormat.JSON) { await imports.create( { service: IntegrationService.JSON }, diff --git a/app/scenes/Settings/components/ImportSlabDialog.tsx b/app/scenes/Settings/components/ImportSlabDialog.tsx new file mode 100644 index 0000000000..17317bb22b --- /dev/null +++ b/app/scenes/Settings/components/ImportSlabDialog.tsx @@ -0,0 +1,32 @@ +import { Trans } from "react-i18next"; +import { IntegrationService } from "@shared/types"; +import Text from "@shared/components/Text"; +import useStores from "~/hooks/useStores"; +import DropToImport from "./DropToImport"; + +function ImportSlabDialog() { + const { dialogs } = useStores(); + + return ( + <> + + + Import a zip file of Markdown documents exported from Slab – + collections, posts, and images will be imported. In Slab, open the + admin settings and use the Export option to download an + archive of your content. + + + + + Drag and drop the zip file exported from Slab, or click to upload + + + + ); +} + +export default ImportSlabDialog; diff --git a/public/images/slab.png b/public/images/slab.png new file mode 100644 index 0000000000000000000000000000000000000000..77b57a34161335818ef9c1a974d6b4c7d82eb6bf GIT binary patch literal 880 zcmeAS@N?(olHy`uVBq!ia0y~yVAKI(1r8>lNaNj`kqivX=AJH&Ar*7pUN+=qFyJ|` z!RU_trKKhkmT4y$KBc^iR4}PztU1>rV61q=hr>|V>C7l|G#o|~!4bI~k{6lQXLuF@ Pa}tB6tDnm{r-UW|=FSNV literal 0 HcmV?d00001 diff --git a/server/queues/processors/MarkdownImportsProcessor.ts b/server/queues/processors/MarkdownImportsProcessor.ts index 5f6a851836..f12dd82eca 100644 --- a/server/queues/processors/MarkdownImportsProcessor.ts +++ b/server/queues/processors/MarkdownImportsProcessor.ts @@ -5,11 +5,14 @@ import type { Import, ImportTask } from "@server/models"; import MarkdownAPIImportTask from "../tasks/MarkdownAPIImportTask"; import ImportsProcessor from "./ImportsProcessor"; -export default class MarkdownImportsProcessor extends ImportsProcessor { - protected canProcess( - importModel: Import - ): boolean { - return importModel.service === IntegrationService.Markdown; +type Markdown = IntegrationService.Markdown | IntegrationService.Slab; + +export default class MarkdownImportsProcessor extends ImportsProcessor { + protected canProcess(importModel: Import): boolean { + return ( + importModel.service === IntegrationService.Markdown || + importModel.service === IntegrationService.Slab + ); } protected getInitialPhase(): ImportTaskPhase { @@ -17,9 +20,9 @@ export default class MarkdownImportsProcessor extends ImportsProcessor, + importModel: Import, _transaction: Transaction - ): Promise> { + ): Promise> { if (!importModel.scratch?.storageKey) { throw new Error( "Markdown import is missing scratch.storageKey for the bootstrap phase" @@ -30,7 +33,7 @@ export default class MarkdownImportsProcessor extends ImportsProcessor + importTask: ImportTask ): Promise { await new MarkdownAPIImportTask().schedule({ importTaskId: importTask.id }); } diff --git a/server/queues/tasks/APIImportTask.ts b/server/queues/tasks/APIImportTask.ts index e2037b7793..16b03a6342 100644 --- a/server/queues/tasks/APIImportTask.ts +++ b/server/queues/tasks/APIImportTask.ts @@ -21,6 +21,7 @@ import { ImportTaskState, } from "@shared/types"; import { toError } from "@shared/utils/error"; +import { isExternalUrl } from "@shared/utils/urls"; import { createContext } from "@server/context"; import { schema } from "@server/editor"; import Logger from "@server/logging/Logger"; @@ -365,7 +366,14 @@ export default abstract class APIImportTask< return { url, name: name.length !== 0 ? name : node.type.name }; }), "url" - ); + ).filter((item) => isExternalUrl(item.url)); + + // Nothing remote to download — content already points at internal + // attachments (e.g. a Markdown zip's local files resolved to redirect + // URLs), so leave the doc untouched. + if (!attachmentsData.length) { + return doc; + } await sequelize.transaction(async (transaction) => { const dbPromises = attachmentsData.map(async (item) => { @@ -436,14 +444,23 @@ export default abstract class APIImportTask< const attrs = json.attrs ?? {}; if (node.type.name === "attachment") { - const attachmentModel = urlToAttachment[attrs.href as string]; // attachment node uses 'href' attribute. + const attachmentModel = urlToAttachment[attrs.href as string]; + // Nodes already pointing at internal attachments aren't in the map; + // leave them untouched. + if (!attachmentModel) { + return node; + } attrs.href = attachmentModel.redirectUrl; // attachment node can have id. attrs.id = attachmentModel.id; } else if (node.type.name === "image" || node.type.name === "video") { // image & video nodes use 'src' attribute. - attrs.src = urlToAttachment[attrs.src as string].redirectUrl; + const attachmentModel = urlToAttachment[attrs.src as string]; + if (!attachmentModel) { + return node; + } + attrs.src = attachmentModel.redirectUrl; } json.attrs = attrs; diff --git a/server/queues/tasks/MarkdownAPIImportTask.test.ts b/server/queues/tasks/MarkdownAPIImportTask.test.ts index 5767f340b3..ad832dc50b 100644 --- a/server/queues/tasks/MarkdownAPIImportTask.test.ts +++ b/server/queues/tasks/MarkdownAPIImportTask.test.ts @@ -66,6 +66,18 @@ describe("rewriteAttachmentPaths", () => { ]); expect(out).toBe("![x](https://example.com/a.png)"); }); + + it("leaves remote signed URLs untouched so the base task can download them", () => { + // Slab exports reference images as remote signed URLs rather than files + // in the zip; these aren't in the manifest and must survive rewriting so + // the per-page attachment upload step can fetch and re-host them. + const signedUrl = + "https://uploads.slab.com/posts/abc/image.png?Signature=xyz&Expires=123"; + const out = rewriteAttachmentPaths(`![x](${signedUrl})`, [ + { id: "id-a", pathInZip: "C/attachments/local.png" }, + ]); + expect(out).toBe(`![x](${signedUrl})`); + }); }); describe("rewriteInternalLinks", () => { diff --git a/server/queues/tasks/MarkdownAPIImportTask.ts b/server/queues/tasks/MarkdownAPIImportTask.ts index 0f125ed97a..ee9faec287 100644 --- a/server/queues/tasks/MarkdownAPIImportTask.ts +++ b/server/queues/tasks/MarkdownAPIImportTask.ts @@ -30,7 +30,11 @@ import type { ProcessOutput } from "./APIImportTask"; import APIImportTask from "./APIImportTask"; import { DocumentConverter } from "@server/utils/DocumentConverter"; -type Markdown = IntegrationService.Markdown; +// The Markdown task backs both the Outline Markdown zip importer and the +// Slab importer — they share an identical zip-of-markdown shape, differing +// only in that Slab references images as remote signed URLs (downloaded via +// the base class's per-page attachment upload) rather than local files. +type Markdown = IntegrationService.Markdown | IntegrationService.Slab; interface DiscoveredDocument { id: string; @@ -153,7 +157,13 @@ export function rewriteInternalLinks( export default class MarkdownAPIImportTask extends APIImportTask { protected shouldUploadAttachmentsPerPage(): boolean { - return false; + // Per-page upload downloads remote image/attachment URLs referenced in + // the markdown (e.g. Slab's signed export URLs) and rewrites them to + // internal redirect URLs. The base step skips URLs that are already + // internal, so local zip attachments — rewritten to redirect URLs during + // `rewriteMarkdown` and uploaded from the archive in + // `onAllTasksCompleted` — pass through untouched. + return true; } protected async scheduleNextTask(importTask: ImportTask) { diff --git a/server/routes/api/imports/imports.ts b/server/routes/api/imports/imports.ts index 3e0c98c378..a28025904b 100644 --- a/server/routes/api/imports/imports.ts +++ b/server/routes/api/imports/imports.ts @@ -34,6 +34,7 @@ router.post( if ( body.service === IntegrationService.Markdown || + body.service === IntegrationService.Slab || body.service === IntegrationService.JSON ) { const attachment = await Attachment.findByPk(body.attachmentId, { diff --git a/server/routes/api/imports/schema.ts b/server/routes/api/imports/schema.ts index 70c99f7d4c..86e2204277 100644 --- a/server/routes/api/imports/schema.ts +++ b/server/routes/api/imports/schema.ts @@ -43,6 +43,11 @@ export const ImportsCreateSchema = BaseSchema.extend({ attachmentId: z.uuid(), permission: z.enum(CollectionPermission).optional(), }), + z.object({ + service: z.literal(IntegrationService.Slab), + attachmentId: z.uuid(), + permission: z.enum(CollectionPermission).optional(), + }), z.object({ service: z.literal(IntegrationService.JSON), attachmentId: z.uuid(), diff --git a/shared/schema.ts b/shared/schema.ts index b55f8d2e99..82124e685d 100644 --- a/shared/schema.ts +++ b/shared/schema.ts @@ -41,7 +41,7 @@ export type JSONImportInput = z.infer[]; export type ImportInput = T extends IntegrationService.Notion ? NotionImportInput - : T extends IntegrationService.Markdown + : T extends IntegrationService.Markdown | IntegrationService.Slab ? MarkdownImportInput : T extends IntegrationService.JSON ? JSONImportInput @@ -130,12 +130,13 @@ export interface JSONImportScratch { * isn't part of any single task's input. Cleared when the import flips to * `Processed`. */ -export type ImportScratch = - T extends IntegrationService.Markdown - ? MarkdownImportScratch - : T extends IntegrationService.JSON - ? JSONImportScratch - : never; +export type ImportScratch = T extends + | IntegrationService.Markdown + | IntegrationService.Slab + ? MarkdownImportScratch + : T extends IntegrationService.JSON + ? JSONImportScratch + : never; /** * Per-page task input. Generated by the bootstrap task and consumed by @@ -213,7 +214,7 @@ export type JSONImportTaskInput = ( export type ImportTaskInput = T extends IntegrationService.Notion ? NotionImportTaskInput - : T extends IntegrationService.Markdown + : T extends IntegrationService.Markdown | IntegrationService.Slab ? MarkdownImportTaskInput : T extends IntegrationService.JSON ? JSONImportTaskInput diff --git a/shared/types.ts b/shared/types.ts index 1e22cd23ad..63b55f784d 100644 --- a/shared/types.ts +++ b/shared/types.ts @@ -168,6 +168,7 @@ export enum IntegrationService { Figma = "figma", Notion = "notion", Markdown = "markdown", + Slab = "slab", JSON = "json", } @@ -175,12 +176,14 @@ export type ImportableIntegrationService = Extract< IntegrationService, | IntegrationService.Notion | IntegrationService.Markdown + | IntegrationService.Slab | IntegrationService.JSON >; export const ImportableIntegrationService = { Notion: IntegrationService.Notion, Markdown: IntegrationService.Markdown, + Slab: IntegrationService.Slab, JSON: IntegrationService.JSON, } as const;