Skip to content

Instantly share code, notes, and snippets.

@d-oderbolz
Last active June 22, 2025 07:20
Show Gist options
  • Save d-oderbolz/bfcd2c9ac1b02a9ab632c0ebf5a4bccf to your computer and use it in GitHub Desktop.
Save d-oderbolz/bfcd2c9ac1b02a9ab632c0ebf5a4bccf to your computer and use it in GitHub Desktop.

Revisions

  1. d-oderbolz revised this gist Jun 22, 2025. 1 changed file with 12 additions and 3 deletions.
    15 changes: 12 additions & 3 deletions Export-to-Confluence.ps1
    Original file line number Diff line number Diff line change
    @@ -1,3 +1,4 @@
    # @see https://gist.github.com/d-oderbolz/bfcd2c9ac1b02a9ab632c0ebf5a4bccf
    # We have a list of Markdown files in a directory.
    # We want to convert them to Confluence format and export them to a Confluence space.
    # The file names start with an ISO date in the format YYYY-MM-DD, followed by a hyphen delimited name and end with .md.
    @@ -11,7 +12,8 @@ param (
    [string]$StartDate = "2023-01-01",
    [string]$EndDate = "2023-12-31",
    [string]$Directory = ".",
    [string]$Pattern = "^(\d{4}-\d{2}-\d{2})-(.+)\.md$"
    [string]$Pattern = "^(\d{4}-\d{2}-\d{2})-(.+)\.md$",
    [string]$OutputFile = "ConfluenceExport.html"
    )

    # Template for the table row in Confluence format
    @@ -20,7 +22,7 @@ $ConfluenceRowTemplate = @"
    <td>
    <div class="content-wrapper">
    <p>
    <time datetime="{0}"/> </p>
    <time datetime="{0}"/></p>
    </div>
    </td>
    <td>{1}</td>
    @@ -56,6 +58,8 @@ $ConfluenceRowTemplate = @"
    </tr>
    "@

    $rows = @()

    # Iterate through each file in the directory
    Get-ChildItem -Path $Directory -Filter "*.md" | Sort-Object Name | Where-Object {
    $_.Name -match $Pattern -and
    @@ -83,10 +87,15 @@ Get-ChildItem -Path $Directory -Filter "*.md" | Sort-Object Name | Where-Object

    # Create the row for the Confluence table by expanding the template
    $row = $ConfluenceRowTemplate -f $fileDate, $fileTitle, "Category", $Reference, $confluenceContent

    # Add row to array of rows
    $rows += $row

    # Output the row for the Confluence table to the console surrounded by newlines for readability
    Write-Output "`n"
    Write-Output $row
    Write-Output "`n"
    }


    # Write all rows to a file
    $rows -join "`r`n" | Set-Content -Path $OutputFile -Encoding UTF8
  2. d-oderbolz created this gist Jun 21, 2025.
    92 changes: 92 additions & 0 deletions Export-to-Confluence.ps1
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,92 @@
    # We have a list of Markdown files in a directory.
    # We want to convert them to Confluence format and export them to a Confluence space.
    # The file names start with an ISO date in the format YYYY-MM-DD, followed by a hyphen delimited name and end with .md.
    # We want to loop through each file, convert it to Confluence format, and then create a row in a Confluence table.
    # I want to be able to choose the start date and end date for the files to be processed.


    # Usage: .\Export-to-Confluence.ps1 -StartDate "2023-01-01" -EndDate "2023-12-31" -Directory "C:\Path\To\MarkdownFiles" -Pattern "^(\d{4}-\d{2}-\d{2})-(.+)\.md$"

    param (
    [string]$StartDate = "2023-01-01",
    [string]$EndDate = "2023-12-31",
    [string]$Directory = ".",
    [string]$Pattern = "^(\d{4}-\d{2}-\d{2})-(.+)\.md$"
    )

    # Template for the table row in Confluence format
    $ConfluenceRowTemplate = @"
    <tr>
    <td>
    <div class="content-wrapper">
    <p>
    <time datetime="{0}"/> </p>
    </div>
    </td>
    <td>{1}</td>
    <td>
    {2}
    </td>
    <td>
    <div class="content-wrapper">
    <p>
    <ac:link>
    <ri:user ri:userkey="2c9480838431599b0184358c87bb0025"/>
    </ac:link> </p>
    </p>
    </div>
    </td>
    <td>
    <div class="content-wrapper">
    <p>
    {3}
    </p>
    </div>
    </td>
    <td>
    <div class="content-wrapper">
    <ac:structured-macro ac:macro-id="119b6670-c774-4dc7-8bc6-5b580e421945" ac:name="markdown" ac:schema-version="1">
    <ac:plain-text-body><![CDATA[{4}]]></ac:plain-text-body>
    </ac:structured-macro>
    <p>
    <br/>
    </p>
    </div>
    </td>
    </tr>
    "@

    # Iterate through each file in the directory
    Get-ChildItem -Path $Directory -Filter "*.md" | Sort-Object Name | Where-Object {
    $_.Name -match $Pattern -and
    [datetime]::ParseExact($matches[1], "yyyy-MM-dd", $null) -ge [datetime]::ParseExact($StartDate, "yyyy-MM-dd", $null) -and
    [datetime]::ParseExact($matches[1], "yyyy-MM-dd", $null) -le [datetime]::ParseExact($EndDate, "yyyy-MM-dd", $null)
    } | ForEach-Object {
    $fileName = $_.Name
    $fileDate = $matches[1]

    # Convert the file content to Confluence format (this is a placeholder for actual conversion logic)
    $confluenceContent = Get-Content $_.FullName -Raw | Out-String

    # Extract the first heading from the content to use as the title
    # (Assuming the first line is a Markdown heading, e.g., "# Title")
    # Do this using a regex to find the first line that starts with a Markdown heading syntax
    $fileTitle = $confluenceContent -replace "^#\s*(.*)", "$1"

    # As a Reference, we use either an INC or CS Number
    $Reference = $confluenceContent -replace "\b((INC|CS)\d+)", '$1' # Extract INC or CS number from the title
    # If the title is empty, use the file name without the date prefix
    if ([string]::IsNullOrWhiteSpace($fileTitle)) {
    $fileTitle = $fileName -replace $Pattern, '$2' # Remove the date prefix and .md extension
    }


    # Create the row for the Confluence table by expanding the template
    $row = $ConfluenceRowTemplate -f $fileDate, $fileTitle, "Category", $Reference, $confluenceContent
    # Output the row for the Confluence table to the console surrounded by newlines for readability
    Write-Output "`n"
    Write-Output $row
    Write-Output "`n"
    }