Skip to content

Commit 99e41e4

Browse files
author
Al Manning
committed
progress on 2-pass link updates
1 parent 0eeaaf8 commit 99e41e4

File tree

3 files changed

+248
-5
lines changed

3 files changed

+248
-5
lines changed

src/publish/confluence/confluence-helper.ts

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -632,6 +632,37 @@ export const updateLinks = (
632632
return { pass1Changes: updatedChanges, pass2Changes: collectedPass2Changes };
633633
};
634634

635+
export const convertForSecondPass = (
636+
fileMetadataTable: Record<string, SitePage>,
637+
spaceChanges: ConfluenceSpaceChange[],
638+
server: string,
639+
parent: ConfluenceParent
640+
): ConfluenceSpaceChange[] => {
641+
const toUpdatesReducer = (
642+
accumulator: ConfluenceSpaceChange[],
643+
change: ConfluenceSpaceChange
644+
) => {
645+
if (isContentUpdate(change)) {
646+
accumulator = [...accumulator, change];
647+
}
648+
649+
if (isContentCreate(change)) {
650+
const convertedUpdate = buildContentUpdate(
651+
"fake-id-fixme",
652+
change.title,
653+
change.body, //TODO convert link
654+
change.fileName ?? ""
655+
);
656+
accumulator = [...accumulator, convertedUpdate];
657+
}
658+
659+
return accumulator;
660+
};
661+
662+
const changesAsUpdates = spaceChanges.reduce(toUpdatesReducer, []);
663+
return changesAsUpdates;
664+
};
665+
635666
export const updateImagePaths = (body: ContentBody): ContentBody => {
636667
const replacer = (match: string): string => {
637668
let updated: string = match.replace(/^.*[\\\/]/, "");

src/publish/confluence/confluence.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ import {
5151
buildPublishRecordForContent,
5252
buildSpaceChanges,
5353
confluenceParentFromString,
54+
convertForSecondPass,
5455
doWithSpinner,
5556
filterFilesForUpdate,
5657
findAttachments,
@@ -718,13 +719,14 @@ async function publish(
718719
console.log("metadataByFilename", metadataByFilename);
719720
console.log("pass2Changes", pass2Changes);
720721

721-
const pass2Result = updateLinks(
722+
const linkUpdateChanges: ConfluenceSpaceChange[] = convertForSecondPass(
722723
metadataByFilename,
723724
pass2Changes,
724725
server,
725726
parent
726727
);
727-
console.log("pass2Changes after", pass2Result);
728+
729+
console.log("linkUpdateChanges", linkUpdateChanges);
728730
}
729731

730732
const parentPage: Content = await client.getContent(parentId);

tests/unit/confluence.test.ts

Lines changed: 213 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import {
1313
buildSpaceChanges,
1414
capitalizeFirstLetter,
1515
confluenceParentFromString,
16+
convertForSecondPass,
1617
FILE_FINDER,
1718
filterFilesForUpdate,
1819
findAttachments,
@@ -58,8 +59,8 @@ import {
5859
Space,
5960
} from "../../src/publish/confluence/api/types.ts";
6061

61-
const RUN_ALL_TESTS = true;
62-
const FOCUS_TEST = false;
62+
const RUN_ALL_TESTS = false;
63+
const FOCUS_TEST = true;
6364

6465
const xtest = (
6566
name: string,
@@ -3553,6 +3554,214 @@ const runUpdateLinks = () => {
35533554
});
35543555
};
35553556

3557+
const runConvertForSecondPass = () => {
3558+
const suiteLabel = (label: string) => `ConvertForSecondPass_${label}`;
3559+
3560+
const fileMetadataTable = {
3561+
["release-planning.qmd"]: {
3562+
title: "Release Planning",
3563+
id: "19890228",
3564+
metadata: { fileName: "release-planning.xml" },
3565+
},
3566+
["team.qmd"]: {
3567+
title: "Team",
3568+
id: "19857455",
3569+
metadata: { fileName: "team.xml" },
3570+
},
3571+
["triage.qmd"]: {
3572+
title: "Issue Triage",
3573+
id: "19890180",
3574+
metadata: {
3575+
fileName: "triage.xml",
3576+
},
3577+
},
3578+
["authoring/hello-world5.qmd"]: {
3579+
title: "Hello World5",
3580+
id: "43417628",
3581+
metadata: { editor: "v2", fileName: "authoring/hello-world5.xml" },
3582+
},
3583+
};
3584+
3585+
const fakeSpace: Space = {
3586+
key: "fake-space-key",
3587+
id: "fake-space-id",
3588+
homepage: buildFakeContent(),
3589+
};
3590+
3591+
const UPDATE_NO_LINKS: ContentUpdate = {
3592+
contentChangeType: ContentChangeType.update,
3593+
id: "19890228",
3594+
version: null,
3595+
title: "Release Planning",
3596+
type: "page",
3597+
status: "current",
3598+
ancestors: [{ id: "19759105" }],
3599+
body: {
3600+
storage: {
3601+
value: "no links",
3602+
representation: "storage",
3603+
},
3604+
},
3605+
fileName: "release-planning.xml",
3606+
};
3607+
3608+
const CREATE_NO_LINKS: ContentCreate = {
3609+
contentChangeType: ContentChangeType.create,
3610+
title: "Release Planning",
3611+
type: "page",
3612+
status: "current",
3613+
ancestors: [{ id: "19759105" }],
3614+
space: fakeSpace,
3615+
body: {
3616+
storage: {
3617+
value: "no links",
3618+
representation: "storage",
3619+
},
3620+
},
3621+
fileName: "release-planning.xml",
3622+
};
3623+
3624+
const UPDATE_LINKS_ONE: ContentUpdate = {
3625+
contentChangeType: ContentChangeType.update,
3626+
id: "19890228",
3627+
version: null,
3628+
title: "Release Planning",
3629+
type: "page",
3630+
status: "current",
3631+
ancestors: [{ id: "19759105" }],
3632+
body: {
3633+
storage: {
3634+
value:
3635+
"<a href='no-replace.qmd'/>no</a> content content <a href='team.qmd'>team</a> content content <a href='zqmdzz.qmd'>team</a>",
3636+
representation: "storage",
3637+
},
3638+
},
3639+
fileName: "release-planning.xml",
3640+
};
3641+
3642+
const UPDATE_LINKS_ONE_NESTED_DOT_SLASH: ContentUpdate = {
3643+
contentChangeType: ContentChangeType.update,
3644+
id: "43778049",
3645+
version: null,
3646+
title: "Links2",
3647+
type: "page",
3648+
status: "current",
3649+
ancestors: [{ id: "42336414" }],
3650+
body: {
3651+
storage: {
3652+
value: `<a href='./hello-world5.qmd'>Hello World 5</a>`,
3653+
representation: "storage",
3654+
},
3655+
},
3656+
fileName: "authoring/links2.xml",
3657+
};
3658+
3659+
const UPDATE_LINKS_ONE_NESTED: ContentUpdate = {
3660+
contentChangeType: ContentChangeType.update,
3661+
id: "43778049",
3662+
version: null,
3663+
title: "Links2",
3664+
type: "page",
3665+
status: "current",
3666+
ancestors: [{ id: "42336414" }],
3667+
body: {
3668+
storage: {
3669+
value: `<a href='hello-world5.qmd'>Hello World 5</a>`,
3670+
representation: "storage",
3671+
},
3672+
},
3673+
fileName: "authoring/links2.xml",
3674+
};
3675+
3676+
const UPDATE_LINKS_ONE_NESTED_ABS: ContentUpdate = {
3677+
contentChangeType: ContentChangeType.update,
3678+
id: "43778049",
3679+
version: null,
3680+
title: "Links2",
3681+
type: "page",
3682+
status: "current",
3683+
ancestors: [{ id: "42336414" }],
3684+
body: {
3685+
storage: {
3686+
value: `<a href='/release-planning.qmd'>Release Planning</a>`,
3687+
representation: "storage",
3688+
},
3689+
},
3690+
fileName: "authoring/links2.xml",
3691+
};
3692+
3693+
const UPDATE_LINKS_ONE_ANCHOR: ContentUpdate = {
3694+
contentChangeType: ContentChangeType.update,
3695+
id: "19890228",
3696+
version: null,
3697+
title: "Release Planning",
3698+
type: "page",
3699+
status: "current",
3700+
ancestors: [{ id: "19759105" }],
3701+
body: {
3702+
storage: {
3703+
value:
3704+
"<a href='no-replace.qmd'/>no</a> content content <a href='team.qmd#Fake-Anchor'>team</a> content content <a href='zqmdzz.qmd'>team</a>",
3705+
representation: "storage",
3706+
},
3707+
},
3708+
fileName: "release-planning.xml",
3709+
};
3710+
3711+
const UPDATE_LINKS_SEVERAL: ContentUpdate = {
3712+
contentChangeType: ContentChangeType.update,
3713+
id: "19890228",
3714+
version: null,
3715+
title: "Release Planning",
3716+
type: "page",
3717+
status: "current",
3718+
ancestors: [{ id: "19759105" }],
3719+
body: {
3720+
storage: {
3721+
value:
3722+
"<a href='no-replace.qmd'/>not-found</a> content content <a href='team.qmd'>teamz</a> content content <a href='zqmdzz.qmd'>not-found</a> <a href='release-planning.qmd'>Do the Release Planning</a> and then triage.qmd .qmd <a href='triage.qmd'>triage.qmd</a>",
3723+
representation: "storage",
3724+
},
3725+
},
3726+
fileName: "release-planning.xml",
3727+
};
3728+
3729+
const check = (
3730+
expected: ConfluenceSpaceChange[] = [],
3731+
changes: ConfluenceSpaceChange[],
3732+
fileMetadataTable: Record<string, SitePage>,
3733+
server = "fake-server",
3734+
parent = FAKE_PARENT
3735+
) => {
3736+
const result = convertForSecondPass(
3737+
fileMetadataTable,
3738+
changes,
3739+
server,
3740+
parent
3741+
);
3742+
console.log("result", result);
3743+
// assertEquals(expected, result);
3744+
};
3745+
3746+
test(suiteLabel("no_files"), async () => {
3747+
const changes: ConfluenceSpaceChange[] = [];
3748+
const expected: ConfluenceSpaceChange[] = [];
3749+
check(expected, changes, fileMetadataTable);
3750+
});
3751+
3752+
test(suiteLabel("one_update_noLink"), async () => {
3753+
const changes: ConfluenceSpaceChange[] = [UPDATE_NO_LINKS];
3754+
const expected: ConfluenceSpaceChange[] = [UPDATE_NO_LINKS];
3755+
check(expected, changes, fileMetadataTable);
3756+
});
3757+
3758+
otest(suiteLabel("one_create_noLink_convert_to_update"), async () => {
3759+
const changes: ConfluenceSpaceChange[] = [CREATE_NO_LINKS];
3760+
const expected: ConfluenceSpaceChange[] = [UPDATE_NO_LINKS];
3761+
check(expected, changes, fileMetadataTable);
3762+
});
3763+
};
3764+
35563765
const runFindAttachments = () => {
35573766
const suiteLabel = (label: string) => `FindAttachments_${label}`;
35583767

@@ -3839,9 +4048,10 @@ if (RUN_ALL_TESTS) {
38394048
runBuildFileToMetaTable();
38404049
runExtractLinks();
38414050
runUpdateLinks();
4051+
runConvertForSecondPass();
38424052
runFindAttachments();
38434053
runUpdateImagePathsForContentBody();
38444054
runCapFirstLetter();
38454055
} else {
3846-
runUpdateLinks();
4056+
runConvertForSecondPass();
38474057
}

0 commit comments

Comments
 (0)