Skip to content

Instantly share code, notes, and snippets.

@d-oderbolz
Last active June 22, 2025 07:20
Show Gist options
  • Select an option

  • Save d-oderbolz/bfcd2c9ac1b02a9ab632c0ebf5a4bccf to your computer and use it in GitHub Desktop.

Select an option

Save d-oderbolz/bfcd2c9ac1b02a9ab632c0ebf5a4bccf to your computer and use it in GitHub Desktop.
Turn a couple of *.md Files into a Confluence Table
# @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.
# 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$",
[string]$OutputFile = "ConfluenceExport.html"
)
# 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>
"@
$rows = @()
# 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
# 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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment