Files
outline/server/queues/tasks/MarkdownAPIImportTask.test.ts
T
Claude ce587064e7 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 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01AC5Ho2SHBjNpxEAMr5uL6o
2026-06-29 15:55:42 +00:00

129 lines
4.0 KiB
TypeScript

import {
rewriteAttachmentPaths,
rewriteInternalLinks,
} from "./MarkdownAPIImportTask";
describe("rewriteAttachmentPaths", () => {
it("replaces a direct encoded path with the placeholder", () => {
const out = rewriteAttachmentPaths(
"![alt](My%20Collection/attachments/foo.png)",
[{ id: "att-1", pathInZip: "My Collection/attachments/foo.png" }]
);
expect(out).toBe("![alt](<<att-1>>)");
});
it("normalizes legacy `uploads/` bucket layout", () => {
const out = rewriteAttachmentPaths("![x](./uploads/abc/file.png)", [
{
id: "att-2",
pathInZip: "Some Collection/uploads/abc/file.png",
},
]);
expect(out).toBe("![x](<<att-2>>)");
});
it("normalizes legacy `public/` bucket layout", () => {
const out = rewriteAttachmentPaths("![x](./public/abc/file.png)", [
{
id: "att-3",
pathInZip: "Some Collection/public/abc/file.png",
},
]);
expect(out).toBe("![x](<<att-3>>)");
});
it("handles arbitrary folder names like 'attachments/'", () => {
const out = rewriteAttachmentPaths("![x](./attachments/foo.png)", [
{ id: "att-4", pathInZip: "Collection/attachments/foo.png" },
]);
expect(out).toBe("![x](<<att-4>>)");
});
it("matches nested attachments folders", () => {
const out = rewriteAttachmentPaths("![x](./attachments/sub/bar.png)", [
{
id: "att-5",
pathInZip: "Collection/Doc/attachments/sub/bar.png",
},
]);
expect(out).toBe("![x](<<att-5>>)");
});
it("substitutes multiple references in the same document", () => {
const out = rewriteAttachmentPaths(
"![a](./attachments/a.png) and ![b](./attachments/b.png)",
[
{ id: "id-a", pathInZip: "C/attachments/a.png" },
{ id: "id-b", pathInZip: "C/attachments/b.png" },
]
);
expect(out).toBe("![a](<<id-a>>) and ![b](<<id-b>>)");
});
it("is a no-op when no attachments match", () => {
const out = rewriteAttachmentPaths("![x](https://example.com/a.png)", [
{ id: "id-a", pathInZip: "C/attachments/a.png" },
]);
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", () => {
it("rewrites a sibling .md link to a placeholder", () => {
const out = rewriteInternalLinks(
"see [other](./other.md)",
"Collection/parent.md",
{ "Collection/other.md": "doc-1" }
);
expect(out).toBe("see [other](<<doc-1>>)");
});
it("rewrites a nested .md link", () => {
const out = rewriteInternalLinks(
"see [child](./sub/child.md)",
"Collection/parent.md",
{ "Collection/sub/child.md": "doc-2" }
);
expect(out).toBe("see [child](<<doc-2>>)");
});
it("leaves unresolved .md links untouched", () => {
const out = rewriteInternalLinks(
"see [missing](./missing.md)",
"Collection/parent.md",
{}
);
expect(out).toBe("see [missing](./missing.md)");
});
it("ignores non-md links", () => {
const out = rewriteInternalLinks(
"see [site](https://example.com)",
"Collection/parent.md",
{ "Collection/parent.md": "doc-self" }
);
expect(out).toBe("see [site](https://example.com)");
});
it("decodes encoded path segments before lookup", () => {
const out = rewriteInternalLinks(
"see [other](./My%20Doc.md)",
"Collection/parent.md",
{ "Collection/My Doc.md": "doc-3" }
);
expect(out).toBe("see [other](<<doc-3>>)");
});
});