Skip to main content

An mdformat plugin for running shell commands as pre/post processing hooks

Project description

mdformat-hooks

Build Status PyPI version

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

  1. mdformat: The text is formatted by mdformat as usual
  2. 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 processing
  • timeout: 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


Download files

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

Source Distribution

mdformat_hooks-0.1.0.tar.gz (7.3 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

mdformat_hooks-0.1.0-py3-none-any.whl (7.2 kB view details)

Uploaded Python 3

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

Hashes for mdformat_hooks-0.1.0.tar.gz
Algorithm Hash digest
SHA256 d0b40298e08b8c64cb9ef1c047d6b12f13c00d5ff2c87a039ae959144b4a4799
MD5 bb53418cf43f5f357ff0842099b0643f
BLAKE2b-256 08ce698e205a7c4315ed7fdbed4877f38d3bbaae0aa78de81f7453df8319f006

See more details on using hashes here.

Provenance

The following attestation bundles were made for mdformat_hooks-0.1.0.tar.gz:

Publisher: tests.yml on KyleKing/mdformat-hooks

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

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

Hashes for mdformat_hooks-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 08e975ebef10b9330519868f140dade9df8a0a2c6af9820f9604662567cc5072
MD5 107df344ea867efbd90908afb5b86153
BLAKE2b-256 e73fa01ba889e29682528762bb086d1121e0909df92a86aff28bd3be094acca6

See more details on using hashes here.

Provenance

The following attestation bundles were made for mdformat_hooks-0.1.0-py3-none-any.whl:

Publisher: tests.yml on KyleKing/mdformat-hooks

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Supported by

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