commit | c1b53c840c6f209d7bcadfd8b07bc0f226d0349f | [log] [tgz] |
---|---|---|
author | Chris Sewell <chrisj_sewell@hotmail.com> | Mon Sep 28 01:18:00 2020 +0100 |
committer | Chris Sewell <chrisj_sewell@hotmail.com> | Mon Sep 28 01:18:00 2020 +0100 |
tree | d0c637410b8de0f6d0783f0ba7dade9e3611721c | |
parent | 540f67bcdfdc4e245f074b5e5ac0916bed08dc9e [diff] |
improve HTML data cleaning
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:
1.
as the ordered list marker if possible, also for noninitial list itemsMdformat 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.
Mdformat also offers an extensible plugin system for both code fence content formatting and parser extensions (like tables).
pip install mdformat
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 -
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.
import mdformat unformatted = "\n\n# A header\n\n" formatted = mdformat.text(unformatted) assert formatted == "# A header\n"
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)
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.3.1 # Use the ref you want to point at hooks: - id: mdformat # optional additional_dependencies: - mdformat-tables - mdformat-black
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.
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.