feat: rewrite markdown fragment anchors to Confluence-native format during conversion#143
Open
geopanther wants to merge 2 commits into
Open
feat: rewrite markdown fragment anchors to Confluence-native format during conversion#143geopanther wants to merge 2 commits into
geopanther wants to merge 2 commits into
Conversation
…ence format Extracts anchor rewriting logic into md2cf/anchor.py with: - Markdown-to-Confluence anchor mapping (GFM slug -> PageTitle-Heading) - Duplicate heading support with GFM-style -1, -2 suffixes - URL-encoding of special characters in Confluence anchors - id- prefix for anchors starting with non-alpha characters - Rewrites both href="#..." and ac:anchor="..." attributes Includes 33 unit tests covering all helper functions and edge cases.
Integrates rewrite_page_anchors() into pre_process_page(), called after title finalization (including --prefix) so Confluence anchor prefixes match the final page title. Enabled by default; opt out with --no-convert-anchors.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
When md2cf uploads Markdown documents, internal fragment links like
[link](#the-concept)retain the GitHub-Flavored Markdown anchor format. Confluence generates heading anchors differently — usingPageTitle-Headingwith spaces and hyphens stripped — so these links break silently after upload.Solution
This PR rewrites markdown-style fragment anchors to Confluence-native format during conversion, before upload — a purely local string transformation that requires no additional API calls.
New module:
md2cf/anchor.pyA self-contained module with a single public function
rewrite_page_anchors(body, page_title)that:#the-concept) to Confluence anchors (#MyGuide-TheConcept)href="#…"andac:anchor="…"attributes in the bodyHandles edge cases:
-1,-2suffixesid-prefix for anchors starting with non-alpha characters (Confluence convention)CLI integration
A new
--convert-anchors/--no-convert-anchorsflag (enabled by default) controls the feature. The rewriting runs as the last step ofpre_process_page(), after the page title is finalized (including any--prefix), ensuring the Confluence anchor prefix matches the actual page title.Tests
33 unit tests covering all internal helpers and the public API, including edge cases for empty bodies, non-matching fragments, duplicate headings, URL encoding, and the
id-prefix.Changes
md2cf/anchor.pymd2cf/__main__.py--convert-anchorsflag + call inpre_process_page()test_package/unit/test_anchor.py