Plugin Architecture¶
Understanding how mkdocs-to-confluence converts and publishes your documentation to Confluence.
Overview¶
The plugin integrates with the MkDocs build pipeline, intercepting the build process to convert Markdown to Confluence storage format and upload pages via the Confluence REST API.
Build Pipeline Integration¶
MkDocs Hooks¶
The plugin uses MkDocs event hooks to integrate into the build process:
mkdocs build
↓
on_config() → Validate plugin configuration
↓
on_files() → Discover documentation files
↓
on_page_markdown() → Process each page's Markdown
↓
on_page_content() → Convert to Confluence format
↓
on_post_build() → Upload to Confluence
Key Phases¶
1. Configuration Phase (on_config)
- Validates required settings (host_url, space, credentials)
- Establishes authentication session
- Resolves parent page hierarchy
2. Discovery Phase (on_files)
- Maps MkDocs navigation to Confluence page hierarchy
- Resolves parent-child relationships
- Builds dependency tree for correct upload order
3. Conversion Phase (on_page_content)
- Converts Markdown to Confluence storage format (XHTML)
- Extracts image references
- Transforms links for Confluence
4. Upload Phase (on_post_build)
- Checks if pages already exist (by title)
- Compares content to detect changes
- Creates or updates pages
- Uploads attachments (images)
- Maintains page hierarchy
Content Conversion¶
Markdown to Confluence Storage Format¶
Confluence uses a storage format based on XHTML with custom macros. The plugin:
- Parses Markdown using mistune 3.x parser
- Converts to XHTML with Confluence-specific transformations
- Wraps code blocks in Confluence code macros
- Converts admonitions to Confluence info/warning panels
- Transforms tables to Confluence table format
- Handles images as attachment references
Example transformation:
Becomes:
<h1>My Page</h1>
<p>Some <strong>bold</strong> text.</p>
<ac:structured-macro ac:name="code">
<ac:parameter ac:name="language">python</ac:parameter>
<ac:plain-text-body><![CDATA[print("hello")]]></ac:plain-text-body>
</ac:structured-macro>
Image Handling¶
Images are handled as Confluence attachments:
- Extract image references from Markdown
- Resolve file paths relative to docs directory
- Calculate SHA1 hash of image content
- Compare hash with existing attachment (if any)
- Upload only if changed or new
- Update page content with attachment references
Confluence API Integration¶
REST API Operations¶
The plugin uses Confluence's REST API v1:
Page Operations:
- GET /rest/api/content?title={title}&spaceKey={space} - Find existing page
- POST /rest/api/content - Create new page
- PUT /rest/api/content/{id} - Update existing page
Attachment Operations:
- GET /rest/api/content/{id}/child/attachment - List attachments
- POST /rest/api/content/{id}/child/attachment - Upload attachment
- PUT /rest/api/content/{id}/child/attachment/{attachmentId}/data - Update attachment
Authentication Methods¶
Basic Auth (API Token)
Bearer Auth (OAuth)
Page Hierarchy¶
Pages are created with parent-child relationships:
The plugin: 1. Resolves parent page by name 2. Creates pages in dependency order (parents before children) 3. Waits for newly created parents before creating children 4. Supports arbitrary nesting depth
Change Detection¶
Content Normalization¶
To avoid unnecessary updates, content is normalized before comparison:
- Strip whitespace variations
- Remove Confluence metadata (version info, timestamps)
- Normalize HTML formatting
- Compare normalized content
Only if content differs after normalization is an update performed.
Benefits¶
- Reduces Confluence version history clutter
- Avoids triggering watches/notifications unnecessarily
- Improves build performance
- Respects Confluence rate limits
Dry Run Mode¶
Local Export¶
Instead of uploading to Confluence, pages are exported to filesystem:
confluence-export/
├── metadata.json # All pages metadata
├── Page_Title.html # Converted HTML
└── attachments/
└── image.png # Copied images
Use Cases¶
- Preview converted content
- Debug conversion issues
- Test configuration changes
- Review hierarchy structure
Error Handling¶
Retry Logic¶
The plugin implements retry logic for transient failures:
- Network errors → Retry with exponential backoff
- Rate limits → Wait and retry
- Parent page not ready → Poll until available
Failure Modes¶
Hard Failures (build stops): - Invalid credentials - Missing required configuration - Space not found - Parent page not found
Soft Failures (logged, continue): - Individual page upload failure - Attachment upload failure - Content comparison failure
Performance Considerations¶
Parallel Operations¶
Where possible, operations run in parallel: - Attachment uploads for same page - Content conversion for multiple pages
Caching¶
- API responses cached during build
- Page ID lookups cached
- Parent chain resolution cached
Rate Limiting¶
Respects Confluence API rate limits: - Delays between requests - Exponential backoff on 429 responses - Configurable timeouts
Security¶
Credential Management¶
Credentials should use environment variables:
Never hardcode credentials in mkdocs.yml.
TLS/SSL¶
All API requests use HTTPS. Certificate validation is enforced.
Token Scopes¶
API tokens should have minimal required permissions: - Read/write access to target space - Create/update pages - Upload attachments
Next Steps¶
- Key Concepts - Understanding pages, hierarchy, and content
- Configuration Reference - All configuration options
- Troubleshooting - Common issues