Skip to main content

Publish Markdown files to Confluence wiki

Contributors to software projects typically write documentation in Markdown format and host Markdown files in collaborative version control systems (VCS) such as GitHub or GitLab to track changes and facilitate the review process. However, not everyone at a company has access to VCS, and documents are often circulated in Confluence wiki instead.

Replicating documentation to Confluence by hand is tedious, and a lack of automated synchronization with the project repositories where the documents live leads to outdated documentation.

This Python package

  • parses Markdown files,
  • converts Markdown content into the Confluence Storage Format (XHTML),
  • invokes Confluence API endpoints to upload images and content.

Features

  • Sections and subsections
  • Text with bold, italic, monospace, underline and strikethrough
  • Link to external locations
  • Ordered and unordered lists
  • Code blocks (e.g. Python, JSON, XML)
  • Image references (uploaded as Confluence page attachments)
  • Table of Contents
  • Admonitions (converted into info, tip, note and warning Confluence panels)
  • Collapsed sections (converted into expand macros)
  • Mermaid diagrams in code blocks

Installation

Converting code blocks of Mermaid diagrams into Confluence image attachments requires mermaid-cli:

npm install -g @mermaid-js/mermaid-cli

Getting started

In order to get started, you will need

  • your organization domain name (e.g. instructure.atlassian.net),
  • base path for Confluence wiki (typically /wiki/ for managed Confluence, / for on-premise)
  • your Confluence username (e.g. levente.hunyadi@instructure.com) (only if required by your deployment),
  • a Confluence API token (a string of alphanumeric characters), and
  • the space key in Confluence (e.g. DAP) you are publishing content to.

Obtaining an API token

  1. Log in to https://id.atlassian.com/manage/api-tokens.
  2. Click Create API token.
  3. From the dialog that appears, enter a memorable and concise Label for your token and click Create.
  4. Click Copy to clipboard, then paste the token to your script, or elsewhere to save.

Setting up the environment

Confluence organization domain, base path, username, API token and space key can be specified at runtime or set as Confluence environment variables (e.g. add to your ~/.profile on Linux, or ~/.bash_profile or ~/.zshenv on MacOS):

export CONFLUENCE_DOMAIN='instructure.atlassian.net'
export CONFLUENCE_PATH='/wiki/'
export CONFLUENCE_USER_NAME='levente.hunyadi@instructure.com'
export CONFLUENCE_API_KEY='0123456789abcdef'
export CONFLUENCE_SPACE_KEY='DAP'

On Windows, these can be set via system properties.

Permissions

The tool requires appropriate permissions in Confluence in order to invoke endpoints.

If a Confluence username is set, the tool uses HTTP Basic authentication to pass the username and the API key to Confluence REST API endpoints. If no username is provided, the tool authenticates with HTTP Bearer, and passes the API key as the bearer token.

If you lack appropriate permissions, you will get an Unauthorized response from Confluence. The tool will emit a message that looks as follows:

2023-06-30 23:59:59,000 - ERROR - <module> [80] - 401 Client Error: Unauthorized for url: ...

Associating a Markdown file with a wiki page

Each Markdown file is associated with a Confluence wiki page with a Markdown comment:

<!-- confluence-page-id: 85668266616 -->

The above tells the tool to synchronize the Markdown file with the given Confluence page ID. This implies that the Confluence wiki page must exist such that it has an ID. The comment can be placed anywhere in the source file.

Setting the Confluence space

If you work in an environment where there are multiple Confluence spaces, and some Markdown pages may go into one space, whereas other pages may go into another, you can set the target space on a per-document basis:

<!-- confluence-space-key: DAP -->

This overrides the default space set via command-line arguments or environment variables.

Setting generated-by prompt text for wiki pages

In order to ensure readers are not editing a generated document, the tool adds a warning message at the top of the Confluence page as an info panel. You can customize the text that appears. The text can contain markup as per the Confluence Storage Format, and is emitted directly into the info panel macro.

Provide generated-by prompt text in the Markdown file with a tag:

<!-- generated-by: Do not edit! Check out the <a href="https://example.com/project">original source</a>. -->

Alternatively, use the --generated-by GENERATED_BY option. The tag takes precedence.

Running the tool

You execute the command-line tool md2conf to synchronize the Markdown file with Confluence:

$ python3 -m md2conf sample/example.md

Use the --help switch to get a full list of supported command-line options:

$ python3 -m md2conf --help
usage: md2conf [-h] [-d DOMAIN] [-p PATH] [-u USERNAME] [-a APIKEY] [-s SPACE] [-l {debug,info,warning,error,critical}] [-r ROOT_PAGE] [--generated-by GENERATED_BY]
               [--no-generated-by] [--ignore-invalid-url] [--local]
               mdpath

positional arguments:
  mdpath                Path to Markdown file or directory to convert and publish.

optional arguments:
  -h, --help            show this help message and exit
  -d DOMAIN, --domain DOMAIN
                        Confluence organization domain.
  -p PATH, --path PATH  Base path for Confluence (default: '/wiki/').
  -u USERNAME, --username USERNAME
                        Confluence user name.
  -a APIKEY, --apikey APIKEY
                        Confluence API key. Refer to documentation how to obtain one.
  -s SPACE, --space SPACE
                        Confluence space key for pages to be published. If omitted, will default to user space.
  -l {debug,info,warning,error,critical}, --loglevel {debug,info,warning,error,critical}
                        Use this option to set the log verbosity.
  -r ROOT_PAGE          Root Confluence page to create new pages. If omitted, will raise exception when creating new pages.
  --generated-by GENERATED_BY
                        Add prompt to pages (default: 'This page has been generated with a tool.').
  --no-generated-by     Do not add 'generated by a tool' prompt to pages.
  --render-mermaid      Render Mermaid diagrams as image files and add as attachments.
  --no-render-mermaid   Inline Mermaid diagram in Confluence page.
  --render-mermaid-format {png,svg}
                        Format for rendering Mermaid diagrams (default: 'png').
  --ignore-invalid-url  Emit a warning but otherwise ignore relative URLs that point to ill-specified locations.
  --local               Write XHTML-based Confluence Storage Format files locally without invoking Confluence API.

Metadata

Release files for hemstreet-markdown-to-confluence 0.1.15

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for hemstreet-markdown-to-confluence 0.1.15
File Size Uploaded
hemstreet_markdown-to-confluence-0.1.15.tar.gz 25.2 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for hemstreet-markdown-to-confluence 0.1.15
File Interpreter ABI Platform
hemstreet_markdown_to_confluence-0.1.15-py3-none-any.whl Python 3 none any Details

Total release size: 53.6 kB

Release files / hemstreet_markdown-to-confluence-0.1.15.tar.gz

Download URL hemstreet_markdown-to-confluence-0.1.15.tar.gz
Size 25.2 kB
Tags Source
SHA-256 checksum
How to use checksums
517800a1d12ded1709459355d9132aa55bb0ec34f46b209f146783e137ece1c5
BLAKE2b-256 checksum
How to use checksums
9593e870aac9b42c52ef27a879246593f54e1e1122c9ab973ed83aa4931707fe
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/5.1.1 CPython/3.12.5

Release files / hemstreet_markdown_to_confluence-0.1.15-py3-none-any.whl

Download URL hemstreet_markdown_to_confluence-0.1.15-py3-none-any.whl
Size 28.4 kB
Tags Python 3
SHA-256 checksum
How to use checksums
81c041fbe8ee8503a4044a4e209d9c0574f17ebc54f8187a89e574ef2aa9ae15
BLAKE2b-256 checksum
How to use checksums
4815595172fa9307dee506ffe839fdd642bc14e06392971697a6faa7f7c4b7a9
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/5.1.1 CPython/3.12.5

Release history Release notifications | RSS feed

This release

0.1.15 This release

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page