mdformat-mkdocs Plugin for MkDocs Formatting

5.1.4 · active · verified Fri Apr 17

mdformat-mkdocs is an mdformat plugin designed to format Markdown files specifically tailored for MkDocs and Material for MkDocs. It handles features like blockquote directives (admonitions) and other MkDocs-specific syntax, ensuring consistent styling when used with the `mdformat` CLI tool. It is actively maintained and generally follows the release cadence of its host library, `mdformat`, releasing updates as needed to maintain compatibility and add features.

Common errors

Warnings

Install

Quickstart

This quickstart demonstrates how to install `mdformat-mkdocs` and use it to format a Markdown file with MkDocs-specific syntax, such as admonitions. The `mdformat` command-line tool is used, and the `--plugins mkdocs` argument explicitly enables the plugin. The example shows how to format a file and then read its content, as `mdformat` typically modifies files in-place.

# 1. Install mdformat-mkdocs
pip install mdformat-mkdocs

# 2. Create an example Markdown file (e.g., 'docs/index.md')
with open('docs/index.md', 'w') as f:
    f.write('## My Doc\n\n!!! note "Hello"\n    This is a note.\n\n<p>Some HTML comment</p>\n')

# 3. Format the file using mdformat with the mkdocs plugin
#    (Note: The 'docs' directory must exist for the file to be created)
import os
if not os.path.exists('docs'):
    os.makedirs('docs')

# Using a subprocess call as mdformat is primarily a CLI tool
import subprocess
result = subprocess.run(['mdformat', '--plugins', 'mkdocs', 'docs/index.md'], capture_output=True, text=True)

# Print the formatted content (mdformat modifies the file in place by default)
with open('docs/index.md', 'r') as f:
    formatted_content = f.read()

print("--- Original (before format) ---")
print("## My Doc\n\n!!! note \"Hello\"\n    This is a note.\n\n<p>Some HTML comment</p>\n")
print("\n--- Formatted Content ---")
print(formatted_content)

# Expected output for the admonition and blank line removal
# Note: mdformat applies its own default styles too
# Expected: 
# ## My Doc
#
# !!! note "Hello"
#     This is a note.
# <!-- Some HTML comment -->

view raw JSON →