fix: Remapping internal links on import (#12461)

* fix: No remapping of internal links

closes #9584

* PR feedback, testing

* tsc
This commit is contained in:
Tom Moor authored and GitHub committed 2026-05-26 07:06:56 -04:00
1 parent 8c1be70198
commit f3da1bc79e
4 files changed
+250 -31

No files matched your search

@@ -19,6 +19,7 @@ import {
} from "@shared/types";
import {
buildAdmin,
buildDocument,
buildImport,
buildTeam,
buildUser,
@@ -35,6 +36,8 @@ const FIXTURE_USER_EMAIL = "hmac.devo@gmail.com";
interface BuiltZip {
filePath: string;
documentOneUrlId: string;
documentTwoUrlId: string;
cleanup: () => Promise<void>;
}
@@ -85,6 +88,42 @@ async function buildJSONExportZip(): Promise<BuiltZip> {
type: "paragraph",
content: [{ type: "text", text: "Some random text" }],
},
{
type: "paragraph",
content: [
{
type: "text",
text: "see doc two",
marks: [
{
type: "link",
attrs: {
href: `/doc/document-2-${documentTwoUrlId}`,
title: null,
},
},
],
},
],
},
{
type: "paragraph",
content: [
{
type: "text",
text: "see collection",
marks: [
{
type: "link",
attrs: {
href: `/collection/test-json-${collectionUrlId}`,
title: null,
},
},
],
},
],
},
{
type: "paragraph",
content: [
@@ -168,6 +207,8 @@ async function buildJSONExportZip(): Promise<BuiltZip> {
return {
filePath,
documentOneUrlId,
documentTwoUrlId,
cleanup: async () => {
await fs.rm(filePath, { force: true }).catch(() => {});
},
@@ -290,6 +331,65 @@ describe("JSONAPIImportTask", () => {
expect(attachments.length).toBeGreaterThanOrEqual(1);
});
it("rewrites internal document links to the new urlIds", async () => {
const admin = await buildAdmin();
// Pre-create a document with the exported urlId so the importer is
// forced to allocate a fresh urlId for Document 2. Without a collision
// the original urlId would be preserved and link rewriting wouldn't be
// exercised end-to-end.
await buildDocument({
teamId: admin.teamId,
userId: admin.id,
urlId: zip.documentTwoUrlId,
});
const { importId } = await runImport({
teamId: admin.teamId,
createdById: admin.id,
zipPath: zip.filePath,
});
const documents = await Document.findAll({
where: { apiImportId: importId },
order: [["title", "ASC"]],
});
expect(documents.length).toBe(2);
const docOne = documents.find((d) => d.title === "Document 1");
const docTwo = documents.find((d) => d.title === "Document 2");
expect(docOne).toBeDefined();
expect(docTwo).toBeDefined();
expect(docTwo!.urlId).not.toBe(zip.documentTwoUrlId);
const linkParagraph = docOne!.content?.content?.[1];
const linkText = linkParagraph?.content?.[0];
const linkMark = linkText?.marks?.find((m) => m.type === "link");
expect(linkMark?.attrs?.href).toBe(`/doc/document-2-${docTwo!.urlId}`);
expect(linkMark?.attrs?.href).not.toContain(zip.documentTwoUrlId);
});
it("rewrites internal collection links to slugged collection paths", async () => {
const admin = await buildAdmin();
const { importId } = await runImport({
teamId: admin.teamId,
createdById: admin.id,
zipPath: zip.filePath,
});
const collection = await Collection.findOne({
where: { apiImportId: importId },
rejectOnEmpty: true,
});
const docOne = await Document.findOne({
where: { apiImportId: importId, title: "Document 1" },
rejectOnEmpty: true,
});
const linkParagraph = docOne.content?.content?.[2];
const linkText = linkParagraph?.content?.[0];
const linkMark = linkText?.marks?.find((m) => m.type === "link");
expect(linkMark?.attrs?.href).toBe(collection.path);
});
describe("user mapping", () => {
it("maps createdById to an existing user by ID", async () => {
let originalAuthor = await User.findByPk(FIXTURE_USER_ID);