@@ -260,7 +260,8 @@ export const buildContentUpdate = (
260260 parent ?: string ,
261261 status : ContentStatusEnum = ContentStatusEnum . current ,
262262 type : string = PAGE_TYPE ,
263- version : ContentVersion | null = null
263+ version : ContentVersion | null = null ,
264+ ancestors : ContentAncestor [ ] | null = null
264265) : ContentUpdate => {
265266 return {
266267 contentChangeType : ContentChangeType . update ,
@@ -269,7 +270,7 @@ export const buildContentUpdate = (
269270 title,
270271 type,
271272 status,
272- ancestors : parent ? [ { id : parent } ] : null ,
273+ ancestors : parent ? [ { id : parent } ] : ancestors ,
273274 body,
274275 fileName,
275276 } ;
@@ -474,11 +475,20 @@ export const buildSpaceChanges = (
474475 return spaceChanges ;
475476} ;
476477
478+ export const replaceExtension = (
479+ fileName : string ,
480+ oldExtension : string ,
481+ newExtension : string
482+ ) => {
483+ return fileName . replace ( oldExtension , newExtension ) ;
484+ } ;
485+
477486export const getTitle = (
478487 fileName : string ,
479488 metadataByInput : Record < string , InputMetadata >
480489) : string => {
481- const qmdFileName = fileName . replace ( ".xml" , ".qmd" ) ;
490+ const qmdFileName = replaceExtension ( fileName , ".xml" , ".qmd" ) ;
491+
482492 const metadataTitle = metadataByInput [ qmdFileName ] ?. title ;
483493
484494 const titleFromFilename = capitalizeWord ( fileName . split ( "." ) [ 0 ] ?? fileName ) ;
@@ -540,12 +550,17 @@ export const updateLinks = (
540550 spaceChanges : ConfluenceSpaceChange [ ] ,
541551 server : string ,
542552 parent : ConfluenceParent
543- ) : ConfluenceSpaceChange [ ] => {
553+ ) : {
554+ pass1Changes : ConfluenceSpaceChange [ ] ;
555+ pass2Changes : ConfluenceSpaceChange [ ] ;
556+ } => {
544557 const root = `${ server } ` ;
545558 const url = `${ ensureTrailingSlash ( server ) } wiki/spaces/${
546559 parent . space
547560 } /pages/`;
548561
562+ let collectedPass2Changes : ConfluenceSpaceChange [ ] = [ ] ;
563+
549564 const changeMapper = (
550565 changeToProcess : ConfluenceSpaceChange
551566 ) : ConfluenceSpaceChange => {
@@ -597,7 +612,9 @@ export const updateLinks = (
597612
598613 updated = updated . replace ( linkFullFileName , pagePath ) ;
599614 } else {
600- console . warn ( `Link not found for ${ siteFilePath } ` ) ;
615+ if ( ! collectedPass2Changes . includes ( changeToProcess ) ) {
616+ collectedPass2Changes = [ ...collectedPass2Changes , changeToProcess ] ;
617+ }
601618 }
602619
603620 return updated ;
@@ -621,7 +638,66 @@ export const updateLinks = (
621638 const updatedChanges : ConfluenceSpaceChange [ ] =
622639 spaceChanges . map ( changeMapper ) ;
623640
624- return updatedChanges ;
641+ return { pass1Changes : updatedChanges , pass2Changes : collectedPass2Changes } ;
642+ } ;
643+
644+ export const convertForSecondPass = (
645+ fileMetadataTable : Record < string , SitePage > ,
646+ spaceChanges : ConfluenceSpaceChange [ ] ,
647+ server : string ,
648+ parent : ConfluenceParent
649+ ) : ConfluenceSpaceChange [ ] => {
650+ const toUpdatesReducer = (
651+ accumulator : ConfluenceSpaceChange [ ] ,
652+ change : ConfluenceSpaceChange
653+ ) => {
654+ if ( isContentUpdate ( change ) ) {
655+ accumulator = [ ...accumulator , change ] ;
656+ }
657+
658+ if ( isContentCreate ( change ) ) {
659+ console . log (
660+ "basename(change?.fileName?)" ,
661+ basename ( change ?. fileName ?? "" )
662+ ) ;
663+ const qmdFileName = replaceExtension (
664+ change . fileName ?? "" ,
665+ ".xml" ,
666+ ".qmd"
667+ ) ;
668+ const updateId = fileMetadataTable [ qmdFileName ] ?. id ;
669+
670+ if ( updateId ) {
671+ const convertedUpdate = buildContentUpdate (
672+ updateId ,
673+ change . title ,
674+ change . body ,
675+ change . fileName ?? "" ,
676+ "" ,
677+ ContentStatusEnum . current ,
678+ PAGE_TYPE ,
679+ null ,
680+ change . ancestors
681+ ) ;
682+ accumulator = [ ...accumulator , convertedUpdate ] ;
683+ } else {
684+ console . warn ( "update ID not found for" , change . fileName ) ;
685+ }
686+ }
687+
688+ return accumulator ;
689+ } ;
690+
691+ const changesAsUpdates = spaceChanges . reduce ( toUpdatesReducer , [ ] ) ;
692+
693+ const updateLinkResults = updateLinks (
694+ fileMetadataTable ,
695+ changesAsUpdates ,
696+ server ,
697+ parent
698+ ) ;
699+
700+ return updateLinkResults . pass1Changes ;
625701} ;
626702
627703export const updateImagePaths = ( body : ContentBody ) : ContentBody => {
0 commit comments