MDFormat is an opinionated Markdown formatter that automatically formats Markdown files to a consistent style, improving readability and maintainability.
MDFormat helps maintain consistent Markdown formatting by:
- Enforcing a consistent style across all Markdown files
- Automatically fixing formatting issues
- Supporting various Markdown extensions and flavors
- Integrating with pre-commit hooks and CI/CD pipelines
- Providing plugins for additional formatting rules
MDFormat is included as a development dependency:
# Install with other development dependencies
uv sync --devTo install it directly:
uv pip install mdformatIn this project, MDFormat is used to:
- Ensure consistent formatting across all Markdown documentation
- Automatically fix formatting issues in documentation files
- Run as part of the pre-commit hooks and CI/CD pipeline
- Maintain professional and readable documentation
MDFormat is configured in the pyproject.toml file:
[tool.mdformat]
number = trueThis configuration enables automatic numbering of ordered lists.
To run MDFormat on the project:
# Format a single file
uv run mdformat docs/README.md
# Format multiple files
uv run mdformat docs/*.md
# Format and check if files would be changed
uv run mdformat --check docs/*.md# Check files without modifying them
uv run mdformat --check docs/*.md
# Enable specific plugins
uv run mdformat --wrap=80 --number docs/README.md
# Format with specific extensions
uv run mdformat --enable-extensions=tables,footnotes docs/README.md# My Document
This is a paragraph with extra spaces.
* Unordered list item 1
* Unordered list item 2
* Nested item
1. First item
1. Second item
1. Third item
# My Document
This is a paragraph with extra spaces.
- Unordered list item 1
- Unordered list item 2
- Nested item
1. First item
2. Second item
3. Third itemMDFormat applies several formatting rules:
- Consistent headings: Uses ATX-style headings (
#syntax) - List formatting: Standardizes list markers (
-for unordered lists) - Whitespace: Removes trailing whitespace and ensures consistent spacing
- Line breaks: Normalizes line breaks
- List numbering: Correctly numbers ordered lists (when enabled)
- Table formatting: Aligns table columns (with extensions)
- Code blocks: Ensures proper fencing for code blocks
- Run MDFormat regularly: Include it in your pre-commit hooks to maintain consistent formatting.
- Format before committing: Run MDFormat on documentation files before committing changes.
- Use with other Markdown tools: Combine with Markdown linters for comprehensive documentation quality.
- Enable relevant extensions: Use extensions that match your Markdown flavor (e.g., GitHub Flavored Markdown).
- Check formatting in CI: Verify Markdown formatting as part of your CI pipeline.