Skip to main content

CommonMark compliant Markdown formatter

Project description

Build Status codecov.io PyPI version

mdformat

CommonMark compliant Markdown formatter

Mdformat is an opinionated Markdown formatter that can be used to enforce a consistent style in Markdown files. Mdformat is a Unix-style command-line tool as well as a Python library.

The features/opinions of the formatter include:

  • Strip trailing and leading whitespace
  • Always use ATX style headings
  • Consistent indentation for contents of block quotes and list items
  • Reformat reference links as inline links
  • Reformat indented code blocks as fenced code blocks
  • Separate blocks with a single empty line (an exception being tight lists where the separator is a single newline character)
  • End the file in a single newline character
  • Use 1. as the ordered list marker if possible, also for noninitial list items

Mdformat by default will not change word wrapping. The rationale for this is to support techniques like One Sentence Per Line and Semantic Line Breaks.

NOTE: The formatting style produced by mdformat may change in each version. It is recommended to pin mdformat dependency version.

Installing

pip install mdformat

Command line usage

Format files

Format files README.md and CHANGELOG.md in place

mdformat README.md CHANGELOG.md

Format .md files in current working directory recursively

mdformat .

Read Markdown from standard input until EOF. Write formatted Markdown to standard output.

mdformat -

Check formatting

mdformat --check README.md CHANGELOG.md

This will not apply any changes to the files. If a file is not properly formatted, the exit code will be non-zero.

Python API usage

Format text

import mdformat

unformatted = "\n\n# A header\n\n"
formatted = mdformat.text(unformatted)
assert formatted == "# A header\n"

Format a file

Format file README.md in place:

import mdformat

# Input filepath as a string...
mdformat.file("README.md")

# ...or a pathlib.Path object
import pathlib

filepath = pathlib.Path("README.md")
mdformat.file(filepath)

Usage as a pre-commit hook

mdformat can be used as a pre-commit hook. Add the following to your project's .pre-commit-config.yaml to enable this:

- repo: https://github.com/executablebooks/mdformat
  rev: 0.1.2  # Use the ref you want to point at
  hooks:
  - id: mdformat

Code formatter plugins

Mdformat features a plugin system to support formatting of Markdown code blocks where the coding language has been labeled. For instance, if mdformat-black plugin is installed in the environment, mdformat CLI will automatically format Python code blocks with Black.

For stability, mdformat Python API behavior will not change simply due to a plugin being installed. Code formatters will have to be explicitly enabled in addition to being installed:

import mdformat

unformatted = "~~~python\n'''black converts quotes'''\n~~~\n"
# Pass in `codeformatters` here! It is an iterable of coding languages
# that should be formatted
formatted = mdformat.text(unformatted, codeformatters={"python"})
assert formatted == '~~~python\n"""black converts quotes"""\n~~~\n'

Read the contribution guide if you wish to implement a new code formatter plugin.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

mdformat-0.1.2.tar.gz (13.8 kB view details)

Uploaded Source

Built Distribution

mdformat-0.1.2-py3-none-any.whl (14.5 kB view details)

Uploaded Python 3

File details

Details for the file mdformat-0.1.2.tar.gz.

File metadata

  • Download URL: mdformat-0.1.2.tar.gz
  • Upload date:
  • Size: 13.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.0.10 CPython/3.6.9 Linux/5.4.0-47-generic

File hashes

Hashes for mdformat-0.1.2.tar.gz
Algorithm Hash digest
SHA256 3fc134932ecb61faf4da13b19acdf41cb731c196d0d5fc46164d395425de8628
MD5 711768b0b41bf368a88ec7db850d7355
BLAKE2b-256 396ac50207e3efc43f3cc3223b8cf9523e1a87dc38d0fc3d8c21211d189c49df

See more details on using hashes here.

File details

Details for the file mdformat-0.1.2-py3-none-any.whl.

File metadata

  • Download URL: mdformat-0.1.2-py3-none-any.whl
  • Upload date:
  • Size: 14.5 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.0.10 CPython/3.6.9 Linux/5.4.0-47-generic

File hashes

Hashes for mdformat-0.1.2-py3-none-any.whl
Algorithm Hash digest
SHA256 572e74e1479413911d418d27b5580a4e25e6e7b3cb0ce377e49f364e7cc5dce4
MD5 91575dd9a4acac3c1285f6e7722e9406
BLAKE2b-256 c55b4b5b66e4afde86546d2dc40ef7e5000c75b43e29af8f476ab7845ff0a64c

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page