Architecture Documentation¶
Overview¶
The MkDocs Mermaid to SVG plugin turns Mermaid code fences into static SVG images during a MkDocs build so that PDF export and offline browsing work without client-side JavaScript. During on_config the plugin validates the configuration, honours the optional enabled_if_env gate, and wires up a MermaidProcessor instance. When MkDocs renders pages, on_page_markdown delegates to the processor (unless we are serving live content) and collects every generated asset so that on_post_build can register or clean them via Files.
Key runtime traits:
- Configuration is validated and augmented with derived values such as
log_levelbased on--verboseflags. - The plugin can be disabled entirely through
enabled_if_envor by running MkDocs inservemode. - Generated assets are tracked in-memory so that they can be injected into
Filesand removed whencleanup_generated_imagesis enabled. - When
image_id_enabledis true the plugin validates thatattr_listis present inmarkdown_extensionsand assigns deterministic IDs to successful blocks. - Errors are normalised through
_handle_processing_error, mapping low-level failures onto the typed exception hierarchy inexceptions.py.
Processing Pipeline¶
- MkDocs hook –
MermaidSvgConverterPlugin.on_page_markdownshort-circuits forservemode and otherwise invokes_process_mermaid_diagrams. - Page processing –
MermaidProcessor.process_pageextracts all Mermaid blocks, iterates them with aProcessingContext, and collects rewritten Markdown plus image paths. - Markdown extraction –
MarkdownProcessorfinds both attribute-rich and plain Mermaid fences, parses attributes into dictionaries, and keeps positional information for later replacement. - Image generation – Each
MermaidBlock.generate_imagecall hands the diagram code toMermaidImageGenerator, which resolves the CLI command, prepares temp artifacts, executesmmdc, and validates outputs. - Markdown rewrite – Successful blocks replace their source spans with image Markdown that respects the page depth, docs root, and configured
output_dirviaImagePathResolver.
Sequence Diagram¶
Project Structure¶
mkdocs-mermaid-to-svg/
└── src/
└── mkdocs_mermaid_to_svg/
├── __init__.py # Package init and version exposure
├── _version.py # Version string wiring for mkdocs
├── plugin.py # MermaidSvgConverterPlugin MkDocs entry point and lifecycle hooks
├── processor.py # MermaidProcessor and ProcessingContext coordinating block/image handling
├── markdown_processor.py # MarkdownProcessor + helpers to extract and rewrite Mermaid fences
├── image_generator.py # MermaidImageGenerator plus CLI resolver/executor/artifact manager
├── mermaid_block.py # MermaidBlock & ImagePathResolver for per-block rendering metadata
├── config.py # ConfigManager schema, validation, and file existence checks
├── types.py # LogContext TypedDict shared with logging utilities
├── exceptions.py # Structured exception hierarchy used across the pipeline
├── logging_config.py # Structured logging setup and contextual adapters
└── utils.py # Shared helpers (filenames, temp files, CLI detection, cleanup)
Component Dependencies¶
Mermaid Image IDs¶
MermaidSvgConverterPlugin.on_configthrows aMermaidConfigErrorwhenimage_id_enabledis set but the Markdownattr_listextension is not enabled, preventing broken{#...}literals.- After successful rendering,
MermaidProcessorcallsMermaidBlock.set_render_contextwith a generated ID composed fromimage_id_prefix, the page stem, and a 1-based index. - Individual Mermaid fences can override the generated ID by supplying an
{id: "custom-id"}attribute, which is respected during Markdown replacement.
Class Architecture¶
Configuration Highlights¶
enabled_if_envtoggles the plugin unless a non-empty environment variable is present.output_dircontrols where SVGs are written relative todocs_dirand feeds directly into Markdown rewriting logic.theme,css_file, andmermaid_configinfluence Mermaid CLI rendering; dictionary Mermaid configs are serialised to a temp file.puppeteer_configcan point at an existing file; otherwise the artifact manager writes a Chrome-friendly default and tears it down afterward.mmdc_path(and its fallback logic) is resolved throughMermaidCommandResolver, with results cached across generations.cleanup_generated_imagestoggles post-build removal usingutils.clean_generated_images.log_levelis derived from CLI verbosity orMKDOCS_MERMAID_LOG_LEVELand applied throughlogging_config.setup_plugin_logging.error_on_failguards whether failures raise typed exceptions (MermaidFileError,MermaidImageError, etc.) or silently preserve the original Markdown block.