An mdformat plugin for running shell commands as pre/post processing hooks
Project description
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
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
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file mdformat_hooks-0.1.0.tar.gz.
File metadata
- Download URL: mdformat_hooks-0.1.0.tar.gz
- Upload date:
- Size: 7.3 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
d0b40298e08b8c64cb9ef1c047d6b12f13c00d5ff2c87a039ae959144b4a4799
|
|
| MD5 |
bb53418cf43f5f357ff0842099b0643f
|
|
| BLAKE2b-256 |
08ce698e205a7c4315ed7fdbed4877f38d3bbaae0aa78de81f7453df8319f006
|
Provenance
The following attestation bundles were made for mdformat_hooks-0.1.0.tar.gz:
Publisher:
tests.yml on KyleKing/mdformat-hooks
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
mdformat_hooks-0.1.0.tar.gz -
Subject digest:
d0b40298e08b8c64cb9ef1c047d6b12f13c00d5ff2c87a039ae959144b4a4799 - Sigstore transparency entry: 731274685
- Sigstore integration time:
-
Permalink:
KyleKing/mdformat-hooks@c4235b234ac816811bc6d76cabb69cb27eff89a0 -
Branch / Tag:
refs/tags/v0.1.0 - Owner: https://github.com/KyleKing
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
tests.yml@c4235b234ac816811bc6d76cabb69cb27eff89a0 -
Trigger Event:
push
-
Statement type:
File details
Details for the file mdformat_hooks-0.1.0-py3-none-any.whl.
File metadata
- Download URL: mdformat_hooks-0.1.0-py3-none-any.whl
- Upload date:
- Size: 7.2 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
08e975ebef10b9330519868f140dade9df8a0a2c6af9820f9604662567cc5072
|
|
| MD5 |
107df344ea867efbd90908afb5b86153
|
|
| BLAKE2b-256 |
e73fa01ba889e29682528762bb086d1121e0909df92a86aff28bd3be094acca6
|
Provenance
The following attestation bundles were made for mdformat_hooks-0.1.0-py3-none-any.whl:
Publisher:
tests.yml on KyleKing/mdformat-hooks
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
mdformat_hooks-0.1.0-py3-none-any.whl -
Subject digest:
08e975ebef10b9330519868f140dade9df8a0a2c6af9820f9604662567cc5072 - Sigstore transparency entry: 731274688
- Sigstore integration time:
-
Permalink:
KyleKing/mdformat-hooks@c4235b234ac816811bc6d76cabb69cb27eff89a0 -
Branch / Tag:
refs/tags/v0.1.0 - Owner: https://github.com/KyleKing
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
tests.yml@c4235b234ac816811bc6d76cabb69cb27eff89a0 -
Trigger Event:
push
-
Statement type: