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,97 @@ 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+ const parentFileName = fileName . replace ( "/index.xml" , "" ) ;
517+ const parentSitePage : SitePage = metadataByFileName [ parentFileName ] ;
518+ if ( isIndexFile ( change ) ) {
519+ if ( parentSitePage ) {
520+ // The parent has already been created, this index create
521+ // is actually an index parent update update the folder with
522+ // index contents
523+ const parentUpdate = buildContentUpdate (
524+ parentSitePage . id ,
525+ change . title ,
526+ change . body ,
527+ parentSitePage . metadata . fileName ?? "" ,
528+ "" ,
529+ ContentStatusEnum . current ,
530+ PAGE_TYPE ,
531+ null ,
532+ parentSitePage . ancestors
533+ ) ;
534+
535+ return [ ...accumulator , parentUpdate ] ;
536+ } else {
537+ return [ ...accumulator ] ; //filter out index file creates
538+ }
539+ }
540+
541+ const indexCreateChange : ConfluenceSpaceChange = indexLookup [ fileName ] ;
542+
543+ if ( indexCreateChange && ! isContentDelete ( indexCreateChange ) ) {
544+ change . title = indexCreateChange . title ?? change . title ;
545+ change . body = indexCreateChange . body ?? change . body ;
546+ }
547+
548+ return [ ...accumulator , change ] ;
549+ } ;
550+
551+ const flattenedIndexes = changes . reduce ( toFlattenedIndexes , [ ] ) ;
552+
553+ return flattenedIndexes ;
554+ } ;
555+
472556export const replaceExtension = (
473557 fileName : string ,
474558 oldExtension : string ,
@@ -596,6 +680,14 @@ export const updateLinks = (
596680 siteFilePath = siteFilePath . slice ( 1 ) ; //remove '/'
597681 }
598682
683+ if ( siteFilePath . endsWith ( "/index.qmd" ) ) {
684+ //flatten child index links to the parent
685+ const siteFilePathParent = siteFilePath . replace ( "/index.qmd" , "" ) ;
686+ if ( fileMetadataTable [ siteFilePathParent ] ) {
687+ siteFilePath = siteFilePathParent ;
688+ }
689+ }
690+
599691 const sitePage : SitePage | null = fileMetadataTable [ siteFilePath ] ?? null ;
600692
601693 if ( sitePage ) {
0 commit comments