<# .SYNOPSIS Export a Confluence space page tree to local files (HTML + Markdown + JSON index) .USAGE powershell -ExecutionPolicy Bypass -File scripts/confluence-export.ps1 #> # Load config and shared functions . "$PSScriptRoot/../config.ps1" . "$PSScriptRoot/lib/confluence-api.ps1" Write-Host "============================================================" Write-Host " Confluence Export - $SpaceKey" Write-Host "============================================================" Write-Host " URL: $ConfluenceUrl" Write-Host " Root Page: $RootPageId" Write-Host "" function Export-PageTree { param([string]$PageId, [string]$ParentPath, [int]$Depth = 0, [ref]$Counter) $Indent = " " * $Depth $Counter.Value++ $Page = Get-PageContent -PageId $PageId if ($null -eq $Page) { return $null } $Title = [string]$Page.title Write-Host "$Indent[$($Counter.Value)] $Title" $Labels = @() if ($Page.metadata -and $Page.metadata.labels -and $Page.metadata.labels.results) { $Labels = @($Page.metadata.labels.results | ForEach-Object { $_.name }) } $VersionNum = if ($Page.version) { $Page.version.number } else { 0 } $PageInfo = [PSCustomObject]@{ id = $Page.id; title = $Title; version = $VersionNum labels = $Labels; depth = $Depth; path = "$ParentPath/$Title"; children = @() } # Save content $HtmlContent = "" $TextContent = "" if ($Page.body -and $Page.body.storage) { $HtmlContent = [string]$Page.body.storage.value $TextContent = Convert-HtmlToText $HtmlContent } $SafeTitle = $Title -replace '[\\/:*?"<>|]', '_' if ($SafeTitle.Length -gt 80) { $SafeTitle = $SafeTitle.Substring(0, 80) } $PageDir = Join-Path $OutputDir "pages" New-Item -ItemType Directory -Path $PageDir -Force | Out-Null # Markdown $MdLines = @("# $Title", "", "> Page ID: $($Page.id)", "> Version: $VersionNum", "> Path: $($PageInfo.path)", "> Labels: $(($Labels) -join ', ')", "", "---", "", $TextContent) $MdPath = Join-Path $PageDir "$($Page.id)_$SafeTitle.md" $MdLines -join "`n" | Out-File -FilePath $MdPath -Encoding UTF8 # HTML $HtmlPath = Join-Path $PageDir "$($Page.id)_$SafeTitle.html" $HtmlContent | Out-File -FilePath $HtmlPath -Encoding UTF8 # Children $Children = @(Get-ChildPages -PageId $PageId) $ChildInfos = @() foreach ($Child in $Children) { $ChildInfo = Export-PageTree -PageId $Child.id -ParentPath $PageInfo.path -Depth ($Depth + 1) -Counter $Counter if ($null -ne $ChildInfo) { $ChildInfos += $ChildInfo } } $PageInfo.children = $ChildInfos return $PageInfo } # Main New-Item -ItemType Directory -Path $OutputDir -Force | Out-Null Write-Host ">> Testing connection..." -ForegroundColor Yellow $TestResult = Invoke-ConfluenceApi -Endpoint "/space/$SpaceKey" if ($null -eq $TestResult) { Write-Host " Connection failed." -ForegroundColor Red; exit 1 } Write-Host " Space: $($TestResult.name) ($SpaceKey)" -ForegroundColor Green Write-Host "" Write-Host ">> Exporting page tree from $RootPageId..." -ForegroundColor Yellow $Counter = [ref]0 $Tree = Export-PageTree -PageId $RootPageId -ParentPath "" -Depth 0 -Counter $Counter Write-Host "" Write-Host ">> $($Counter.Value) pages exported" -ForegroundColor Green # JSON index $JsonPath = Join-Path $OutputDir "page-index.json" $Tree | ConvertTo-Json -Depth 20 | Out-File -FilePath $JsonPath -Encoding UTF8 Write-Host " Index: $JsonPath" Write-Host "============================================================" -ForegroundColor Green