From 57679c6faff1d96169cd4a20334f4886792bdc91 Mon Sep 17 00:00:00 2001 From: Eugene Blikh Date: Thu, 26 Mar 2026 00:31:21 +0300 Subject: [PATCH] Rename module, add features, fix round-trip fidelity - Rename module to sourcecraft.dev/bigbes/confluence-md-utilities - Add fmt command with syntax highlighting (--color=auto/force/disabled) - Add shell completions for all commands with file extension hints - Preserve ac:layout/ac:layout-section/ac:layout-cell through round-trips - Preserve ac:macro-id on code blocks and TOC macros - Preserve table class/style attributes and colgroup column widths - Preserve TOC macro as comment - Fix table cell rendering to preserve bold, italic, code, br, links - Fix user references (ri:userkey) and attachment images (ri:attachment) - Fix inline spacing between formatting elements in list items false --- .gitignore | 2 + README.md | 100 ++++++++++++----- cmd/completions.go | 55 +++++++++ cmd/convert.go | 2 +- cmd/embed.go | 4 +- cmd/extract.go | 4 +- cmd/fmt.go | 36 +++++- cmd/pull.go | 4 +- cmd/push.go | 6 +- confluence/elements.go | 10 +- confluence/renderer.go | 119 +++++++++++++++++++- converter/md2xml.go | 2 +- converter/md2xml_test.go | 66 +++++++++++ converter/xml2md.go | 237 +++++++++++++++++++++++++++++++++++++-- format/color.go | 199 ++++++++++++++++++++++++++++++++ format/color_test.go | 76 +++++++++++++ go.mod | 2 +- main.go | 2 +- 18 files changed, 869 insertions(+), 57 deletions(-) create mode 100644 cmd/completions.go create mode 100644 format/color.go create mode 100644 format/color_test.go diff --git a/.gitignore b/.gitignore index c831e24ee96c95715d688960f0d41bb060a38829..c594bb7e6bb2f4a1bbf8b4675e10d3b8d03f029d 100644 --- a/.gitignore +++ b/.gitignore @@ -16,3 +16,5 @@ rfc-111.md # OS .DS_Store Thumbs.db +rfc-111.xml.md +rfc-111.xml.roundtrip.xml diff --git a/README.md b/README.md index 1077af578ce879ef425f517d447872d57ae4bf89..aea555e8e3389ca7f8455151a454355e01324e5a 100644 --- a/README.md +++ b/README.md @@ -1,51 +1,57 @@ # mdcx -Markdown to Confluence XML converter with bidirectional sync support for self-hosted Confluence Server/Data Center. +Markdown to Confluence XML converter with bidirectional sync for self-hosted Confluence Server/Data Center. -Converts Markdown to [Confluence storage format](https://confluence.atlassian.com/doc/confluence-storage-format-790796544.html) XML and back. Supports pulling pages from Confluence, editing locally as Markdown, and pushing changes back — with template-aware embedding that preserves metadata tables, changelogs, and inline comment markers. +Converts Markdown to [Confluence storage format](https://confluence.atlassian.com/doc/confluence-storage-format-790796544.html) XML and back. +Pull pages from Confluence, edit locally as Markdown, push changes back. +Template-aware embedding preserves metadata tables, changelogs, inline comment markers, user references, and attachment images through round-trips. ## Install ```bash -go install sourcecraft.dev/bigbes/markdown-to-confluence-xml@latest +go install sourcecraft.dev/bigbes/confluence-md-utilities@latest ``` -The binary is named `mdcx`. +Binary name is `mdcx`. ## Commands -### `convert` — Markdown to Confluence XML +### convert + +Markdown to Confluence XML. ```bash mdcx convert input.md -o output.xml cat input.md | mdcx convert > output.xml ``` -### `embed` — Embed Markdown into a Confluence XML template +### embed -Converts Markdown and inserts it between marker comments in an existing Confluence document, preserving everything outside the markers (metadata table, TOC, changelog, etc.). +Convert Markdown and insert between marker comments in an existing Confluence document, preserving everything outside the markers (metadata table, TOC, changelog, etc.). ```bash mdcx embed input.md --template template.xml -o output.xml ``` -The template must contain marker comments: +Template must contain marker comments: ```xml ``` -### `extract` — Extract Markdown from Confluence XML +### extract -Extracts content between markers and converts back to Markdown. +Extract content between markers from a Confluence XML document and convert back to Markdown. ```bash mdcx extract input.xml -o output.md -mdcx extract input.xml --raw # output raw Confluence XML +mdcx extract input.xml --raw # raw Confluence XML, no conversion ``` -### `pull` — Pull a page from Confluence +### pull + +Fetch a page from Confluence and convert to Markdown. ```bash mdcx pull "https://confluence.example.com/pages/viewpage.action?pageId=12345" -o page.md @@ -53,29 +59,47 @@ mdcx pull "https://confluence.example.com/display/TEAM/Page+Title" -o page.md mdcx pull "https://confluence.example.com/display/TEAM/Page+Title" --raw -o page.xml ``` -### `push` — Push Markdown to a Confluence page +### push + +Convert local Markdown and update a Confluence page. ```bash # Replace entire page body -mdcx push "https://confluence.example.com/display/TEAM/Page+Title" page.md +mdcx push "https://confluence.example.com/display/TEAM/Page" page.md -# Template mode: replace only content between markers -mdcx push "https://confluence.example.com/display/TEAM/Page+Title" page.md --template +# Template mode: only replace content between markers, keep everything else +mdcx push "https://confluence.example.com/display/TEAM/Page" page.md --template # With version message -mdcx push "https://confluence.example.com/display/TEAM/Page+Title" page.md -m "Updated intro section" +mdcx push "https://confluence.example.com/display/TEAM/Page" page.md -m "Updated intro" ``` -## Authentication +### fmt + +Pretty-print Confluence storage XML with syntax highlighting. + +```bash +mdcx fmt page.xml +mdcx fmt page.xml --color=force # force colors even when piped +mdcx fmt page.xml --color=disabled # no colors +mdcx fmt page.xml -o formatted.xml # write to file (colors auto-disabled) +``` + +Colors: tags in blue/magenta/cyan by namespace, attributes in yellow, values in green, CDATA and comments in gray. Enabled by default when output is a terminal. + +Block elements get their own lines with indentation. Short `
  • `, `

    `-`

    `, ``, `` stay on one line when content fits. Lines longer than 120 characters are wrapped at word boundaries (UTF-8 aware). CDATA content is preserved as-is. -For `pull` and `push`, provide a [Personal Access Token](https://confluence.atlassian.com/enterprise/using-personal-access-tokens-1026032365.html) via: +## Authentication -- `--token` flag, or -- `CONFLUENCE_TOKEN` environment variable +`pull` and `push` require a [Personal Access Token](https://confluence.atlassian.com/enterprise/using-personal-access-tokens-1026032365.html): ```bash +# Via environment variable export CONFLUENCE_TOKEN="your-token-here" mdcx pull "https://confluence.example.com/display/TEAM/RFC-42" -o rfc.md + +# Via flag +mdcx pull --token "your-token" "https://confluence.example.com/display/TEAM/RFC-42" -o rfc.md ``` ## Typical workflow @@ -83,16 +107,29 @@ mdcx pull "https://confluence.example.com/display/TEAM/RFC-42" -o rfc.md ```bash export CONFLUENCE_TOKEN="..." -# Pull page to local Markdown +# Pull mdcx pull "https://confluence.example.com/display/TEAM/RFC-42" -o rfc.md -# Edit locally +# Edit vim rfc.md # Push back, preserving template structure mdcx push "https://confluence.example.com/display/TEAM/RFC-42" rfc.md --template -m "Updated requirements" ``` +## Shell completions + +```bash +# Bash +source <(mdcx completion bash) + +# Zsh (add to your .zshrc) +mdcx completion zsh > "${fpath[1]}/_mdcx" + +# Fish +mdcx completion fish | source +``` + ## Supported elements | Markdown | Confluence XML | @@ -105,23 +142,24 @@ mdcx push "https://confluence.example.com/display/TEAM/RFC-42" rfc.md --template | Fenced code blocks | `` with CDATA | | `- item` / `1. item` | `