11import { trace } from "./confluence-logger.ts" ;
22import { ApiError , PublishRecord } from "../types.ts" ;
3- import { ensureTrailingSlash } from "../../core/path.ts" ;
43import {
5- join ,
6- basename ,
7- parse ,
8- dirname ,
9- toFileUrl ,
10- resolve ,
11- } from "path/mod.ts" ;
4+ ensureTrailingSlash ,
5+ pathWithForwardSlashes ,
6+ } from "../../core/path.ts" ;
7+ import { join } from "path/mod.ts" ;
128import { isHttpUrl } from "../../core/url.ts" ;
13- import { pathWithForwardSlashes } from "../../core/path.ts" ;
149import { AccountToken , InputMetadata } from "../provider.ts" ;
1510import {
1611 ConfluenceParent ,
@@ -29,8 +24,6 @@ import {
2924 ContentUpdate ,
3025 ContentVersion ,
3126 EMPTY_PARENT ,
32- LogLevel ,
33- LogPrefix ,
3427 PAGE_TYPE ,
3528 SiteFileMetadata ,
3629 SitePage ,
@@ -469,6 +462,88 @@ export const buildSpaceChanges = (
469462 return spaceChanges ;
470463} ;
471464
465+ export const flattenIndexes = (
466+ changes : ConfluenceSpaceChange [ ] ,
467+ metadataByFileName : Record < string , SitePage >
468+ ) : ConfluenceSpaceChange [ ] => {
469+ const getFileNameForChange = ( change : ConfluenceSpaceChange ) => {
470+ if ( isContentDelete ( change ) ) {
471+ return "" ;
472+ }
473+
474+ return pathWithForwardSlashes ( change ?. fileName ?? "" ) ;
475+ } ;
476+
477+ const isIndexFile = ( change : ConfluenceSpaceChange ) => {
478+ return getFileNameForChange ( change ) ?. endsWith ( "/index.xml" ) ;
479+ } ;
480+
481+ const toIndexPageLookup = (
482+ accumulator : Record < string , ConfluenceSpaceChange > ,
483+ change : ConfluenceSpaceChange
484+ ) : Record < string , any > => {
485+ if ( isContentDelete ( change ) ) {
486+ return accumulator ;
487+ }
488+ const fileName = getFileNameForChange ( change ) ;
489+ const isIndex = isIndexFile ( change ) ;
490+
491+ if ( isIndex ) {
492+ const folderFileName = fileName . replace ( "/index.xml" , "" ) ;
493+ return {
494+ ...accumulator ,
495+ [ folderFileName ] : change ,
496+ } ;
497+ }
498+
499+ return accumulator ;
500+ } ;
501+
502+ const indexLookup : Record < string , ConfluenceSpaceChange > = changes . reduce (
503+ toIndexPageLookup ,
504+ { }
505+ ) ;
506+
507+ const toFlattenedIndexes = (
508+ accumulator : ConfluenceSpaceChange [ ] ,
509+ change : ConfluenceSpaceChange
510+ ) : ConfluenceSpaceChange [ ] => {
511+ if ( isContentDelete ( change ) ) {
512+ return [ ...accumulator , change ] ;
513+ }
514+
515+ const fileName = getFileNameForChange ( change ) ;
516+
517+ if ( isIndexFile ( change ) ) {
518+ // console.log("change", change);
519+ console . log ( "is index file, return" ) ;
520+ const parentId = fileName . replace ( "/index.xml" , "" ) ;
521+ console . log ( "parentId" , parentId ) ;
522+
523+ // console.log("parentCreateChange", parentCreateChange);
524+ // if (!parentCreateChange) {
525+ // the parent has already been created, this index create
526+ // is actually an index parent update
527+ // const parentUpdate: ContentUpdate = buildContentUpdate(id);
528+ // }
529+
530+ return accumulator ;
531+ }
532+
533+ const indexChange : ConfluenceSpaceChange = indexLookup [ fileName ] ;
534+ if ( indexChange && ! isContentDelete ( indexChange ) ) {
535+ change . title = indexChange . title ?? change . title ;
536+ change . body = indexChange . body ?? change . body ;
537+ }
538+
539+ return [ ...accumulator , change ] ;
540+ } ;
541+
542+ const flattenedIndexes = changes . reduce ( toFlattenedIndexes , [ ] ) ;
543+
544+ return flattenedIndexes ;
545+ } ;
546+
472547export const replaceExtension = (
473548 fileName : string ,
474549 oldExtension : string ,
0 commit comments