mdformat-hooks
An mdformat plugin for running shell commands as post-processing hooks. This allows you to integrate external tools like mdsf.
Installation
Add this package wherever you use mdformat and the plugin will be auto-recognized. The only configuration required is specifying a 'post command'. See additional information on mdformat plugins here
pre-commit / prek
repos:
- repo: https://github.com/executablebooks/mdformat
rev: 1.0.0
hooks:
- id: mdformat
additional_dependencies:
- mdformat-hooks
uvx
uvx --with mdformat-hooks mdformat
Or with pipx:
pipx install mdformat
pipx inject mdformat mdformat-hooks
Usage
Command Line
You can use mdformat-hooks via the command line with the following options:
# Run a post-processing command (e.g., mdsf for additional formatting)
mdformat --post-command "mdsf format --stdin" document.md
# Run a post command with custom timeout
mdformat --post-command "mdsf format --stdin" --timeout 60 document.md
Configuration File
You can configure hooks in your .mdformat.toml file:
[plugin.hooks]
post_command = "mdsf format --stdin"
timeout = 30
strict_hooks = true # Optional: fail on command errors (useful for CI)
Python API
import mdformat
# Format with post-processing hook
formatted = mdformat.text(
markdown_text,
extensions={"hooks"},
options={
"plugin": {
"hooks": {
"post_command": "mdsf format --stdin",
"timeout": 30,
"strict_hooks": True, # Optional: fail on command errors
}
}
},
)
How It Works
- mdformat: The text is formatted by mdformat as usual
- Post-command: If configured, the formatted text is passed to the post-command via stdin for additional processing
Error Handling
By default, mdformat-hooks uses graceful error handling:
- If a command fails (non-zero exit code), the original text is returned and an error is printed to stderr
- If a command times out, the original text is returned and a timeout message is printed to stderr
- All errors are non-fatal to ensure your formatting workflow continues
Strict Mode: Enable strict mode to make command failures halt formatting (useful in CI/CD):
mdformat --post-command "mdsf format --stdin" --strict-hooks document.md
In strict mode, any non-zero exit code, timeout, or exception will raise an error and stop formatting.
Configuration Options
post_command: Shell command to run after mdformat processingtimeout: Maximum time in seconds for the command to execute (default: 30)strict_hooks: Fail formatting if command returns non-zero exit code (default: false)
Examples
Using with mdsf
mdsf is a fast markdown code block formatter that supports hundreds of formatting tools:
[plugin.hooks]
post_command = "mdsf format --stdin"
Chaining Multiple Tools
Since commands run in shell, you can chain multiple operations as long as the tool reads from STDIN and writes to STDOUT:
[plugin.hooks]
post_command = "mdsf format --stdin | typos-cli - --write-changes"
Contributing
See CONTRIBUTING.md
Release files for mdformat-hooks 0.1.0
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | |
|---|---|---|---|
| mdformat_hooks-0.1.0.tar.gz | 7.3 kB | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| mdformat_hooks-0.1.0-py3-none-any.whl | Python 3 | none | any | Details |
Total release size: 14.6 kB
Release files / mdformat_hooks-0.1.0.tar.gz
| Download URL | mdformat_hooks-0.1.0.tar.gz |
|---|---|
| Size | 7.3 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
d0b40298e08b8c64cb9ef1c047d6b12f13c00d5ff2c87a039ae959144b4a4799
|
|
BLAKE2b-256 checksum How to use checksums |
08ce698e205a7c4315ed7fdbed4877f38d3bbaae0aa78de81f7453df8319f006
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/6.1.0 CPython/3.13.7
|
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
PyPI Publish Attestation
PyPI verified that this artifact, at this checksum, originated from the publisher listed below.
Signed by GitHub Actions, verified by PyPI on Nov 29, 2025.
Transparency logRelease files / mdformat_hooks-0.1.0-py3-none-any.whl
| Download URL | mdformat_hooks-0.1.0-py3-none-any.whl |
|---|---|
| Size | 7.2 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
08e975ebef10b9330519868f140dade9df8a0a2c6af9820f9604662567cc5072
|
|
BLAKE2b-256 checksum How to use checksums |
e73fa01ba889e29682528762bb086d1121e0909df92a86aff28bd3be094acca6
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/6.1.0 CPython/3.13.7
|
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
PyPI Publish Attestation
PyPI verified that this artifact, at this checksum, originated from the publisher listed below.
Signed by GitHub Actions, verified by PyPI on Nov 29, 2025.
Transparency log