CommonMark compliant Markdown formatter
Project description
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:
- Consistent indentation and whitespace across the board
- Always use ATX style headings
- Move all link references to the bottom of the document (sorted by label)
- Reformat indented code blocks as fenced code blocks
- 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 Semantic Line Breaks.
For a comprehensive description and rationalization of the style, read the style guide.
NOTE: The formatting style produced by mdformat may change in each version. It is recommended to pin mdformat dependency version.
Mdformat also offers an extensible plugin system for both code fence content formatting and parser extensions (like tables).
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.
Options
foo@bar:~$ mdformat --help
usage: mdformat [-h] [--check] [--version] [--number] [paths [paths ...]]
CommonMark compliant Markdown formatter
positional arguments:
paths files to format
optional arguments:
-h, --help show this help message and exit
--check do not apply changes to files
--version show program's version number and exit
--number apply consecutive numbering to ordered lists
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)
Options
Any options available in the CLI are also available in the Python API, with equivalent option names.
For instance, to switch on consecutive numbering of ordered lists, do
import mdformat
mdformat.file("FILENAME.md", options={"number": True})
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.5.4 # Use the ref you want to point at
hooks:
- id: mdformat
# optional
additional_dependencies:
- mdformat-tables
- mdformat-black
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.
Existing plugins
Plugin | Supported languages |
---|---|
mdformat-beautysh | bash , sh |
mdformat-black | python |
mdformat-config | json , toml , yaml |
mdformat-gofmt | go |
mdformat-web | javascript , js , css , html , xml |
Parser extension plugins
Markdown-it-py offers a range of useful extensions to the base CommonMark parser (see the documented list).
Mdformat features a plugin system to support the loading and rendering of such extensions.
For stability, mdformat Python API behavior will not change simply due to a plugin being installed. Extensions will have to be explicitly enabled in addition to being installed:
import mdformat
unformatted = "content...\n"
# Pass in `extensions` here! It is an iterable of extensions that should be loaded
formatted = mdformat.text(unformatted, extensions={"tables"})
Read the contribution guide if you wish to implement a new parser extension plugin.
Existing plugins
Plugin | Syntax Extensions | Description |
---|---|---|
mdformat-gfm | gfm |
Changes target specification to GitHub Flavored Markdown (GFM) |
mdformat-tables | tables |
Adds support for GitHub Flavored Markdown style tables |
mdformat-toc | toc |
Adds the capability to auto-generate a table of contents |
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
File details
Details for the file mdformat-0.5.4.tar.gz
.
File metadata
- Download URL: mdformat-0.5.4.tar.gz
- Upload date:
- Size: 18.9 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/1.1.4 CPython/3.7.9 Linux/5.4.0-1032-azure
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | a8e69498d963ce56a81d1c063a50cc72e33436ad02a378a018c5f0f07789a0bd |
|
MD5 | b8b11cffdf742148e847af9ba2cb985a |
|
BLAKE2b-256 | abc07b7d9479faefddb95fc8a61d977d68aa3a1d508b3ed5a2eb48037d8939fd |
File details
Details for the file mdformat-0.5.4-py3-none-any.whl
.
File metadata
- Download URL: mdformat-0.5.4-py3-none-any.whl
- Upload date:
- Size: 19.4 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/1.1.4 CPython/3.7.9 Linux/5.4.0-1032-azure
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | c791fd86571f85471489440f532ad8e7ca0396bd588d6a7a3b3e0e879cefcc5e |
|
MD5 | 2b667d21f976a7b778e3e0e9a4289000 |
|
BLAKE2b-256 | 4a08975ebee31d26f3b4c402dc6bc1cff0465f16ed8e24d458a8011e5effce9a |