mdformat

1.0.0 · active · verified Fri Apr 10

Mdformat is an opinionated Markdown formatter that can be used to enforce a consistent style in Markdown files. It functions as both a Unix-style command-line tool and a Python library. The current stable version is 1.0.0, and it features an extensible plugin system to support various Markdown extensions and embedded code formatting. The project maintains an active release cadence, with version 1.0.0 released after several minor versions, indicating ongoing development and maintenance.

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to use `mdformat.text()` to format a Markdown string directly. It also shows how to apply formatting options like numbering ordered lists and setting word wrap width. For file-based formatting, `mdformat.file()` can be used, which modifies the file in place.

import mdformat

unformatted_markdown = """
# A Header

This is some **unformatted**  text.

- List item 1
- List item 2

"""

formatted_markdown = mdformat.text(unformatted_markdown)

print("--- Unformatted ---")
print(unformatted_markdown)
print("\n--- Formatted ---")
print(formatted_markdown)

# Example with options
formatted_with_options = mdformat.text(
    "- One\n- Two\n- Three",
    options={
        "number": True, # Apply consecutive numbering to ordered lists
        "wrap": 60      # Set word wrap width to 60 characters
    }
)
print("\n--- Formatted with Options ---")
print(formatted_with_options)

# To format a file in place (requires a file to exist):
# with open("test.md", "w") as f:
#     f.write(unformatted_markdown)
# mdformat.file("test.md")
# with open("test.md", "r") as f:
#     print("\n--- Formatted File ---")
#     print(f.read())

view raw JSON →