Upload local markdown file via Confluence REST API (PowerShell)

Antti Nousiainen
I'm New Here
I'm New Here
Those new to the Atlassian Community have posted less than three times. Give them a warm welcome!
November 21, 2023

I'm working on a PowerShell script that would allow me to upload a local Markdown file from a path, and create a Confluence sub-page under a parent page and display the file's contents as Markdown.

So far I'm able to create the sub-page under whatever parent or space I like, however, the Markdown isn't displaying as Markdown, just raw text.

I tried going over the REST API docs and tried using both representation types: storage (errors out) and wiki (works but doesn't shows as raw text).

Any ideas on what I'm doing wrong? Been stuck a while on this one...

Here's how the user inputs parameters for the script:

New-MarkdownSubPage -ConfluenceUrl "https://confluence.domain.corp" -SpaceKey "SPACE1" -PageTitle "Parent_Page" -FilePath "C:\Temp\markdown.md"

 

And here's the relevant bits of the script:

$parentPageUri = "$ConfluenceUrl/rest/api/content?spaceKey=$SpaceKey&title=$PageTitle"
$parentPage = Invoke-ConfluenceAPI -Uri $parentPageUri -Method "GET"
$parentPageId = $parentPage.results.id
$fileContent = Get-Content -Path $FilePath -Raw
$fileName = (Get-Item -Path $FilePath).Name

$pageData = @{
type      = "page"
title     = $fileName
space     = @{ key = $SpaceKey }
ancestors = @(@{ id = $parentPageId })
body      = @{
storage = @{
value = $fileContent
representation = "wiki"
}
}
} | ConvertTo-Json -Depth 100

$createPageUri = "$ConfluenceUrl/rest/api/content"
$createdPage = Invoke-ConfluenceAPI -Uri $createPageUri -Method "POST" -Body $pageData

 

1 answer

0 votes
Benno Baumgartner November 22, 2023

You need to convert the Markdown to Confluence storage Format. Maybe try an addon  https://marketplace.atlassian.com/apps/1211438/markdown-html-plantuml-latex-diagrams-open-api-mermaid 

Maybe a Library for this exists?

Regards Benno

Suggest an answer

Log in or Sign up to answer
TAGS
AUG Leaders

Atlassian Community Events