Skip to main content

mdformat-front-matters

Build Status PyPI version

An mdformat plugin for normalizing YAML, TOML, and JSON front matter in CommonMark documents.

[!NOTE] mdformat-frontmatter has additional duplicate key detection, but did not support mdformat v1 (butler54/mdformat-frontmatter #37) nor TOML and JSON at the time (https://github.com/butler54/mdformat-frontmatter/issues/22#issuecomment-1815433725)

Along with the 's', the extra dash is intentional to try to prevent typo errors.

Features

  • Multi-format support: Handles YAML (---), TOML (+++), and JSON ({...}) front matter
  • Automatic normalization: Formats front matter consistently (preserves key order by default, standardized indentation)
  • Configurable sorting: Option to sort keys alphabetically with --sort-front-matter
  • Error resilient: Preserves original content if parsing fails. Will error only if strict mode is set
  • Zero configuration: Works out of the box with mdformat

Examples

YAML Front Matter:

---
title: My Document
date: 2024-01-01
tags:
  - example
  - demo
---

# Content

With --sort-front-matter, becomes:

---
date: 2024-01-01
tags:
  - example
  - demo
title: My Document
---

# Content

TOML Front Matter:

+++
title = "My Document"
date = 2024-01-01
tags = ["example", "demo"]
+++

# Content

JSON Front Matter:

{
    "title": "My Document",
    "date": "2024-01-01",
    "tags": ["example", "demo"]
}

# Content

mdformat Usage

Add this package wherever you use mdformat and the plugin will be auto-recognized. No additional configuration necessary. See additional information on mdformat plugins here

pre-commit / prek

repos:
  - repo: https://github.com/executablebooks/mdformat
    rev: 0.7.19
    hooks:
      - id: mdformat
        additional_dependencies:
          - mdformat-front-matters

uvx

uvx --with mdformat-front-matters mdformat

Or with pipx:

pipx install mdformat
pipx inject mdformat mdformat-front-matters

Configuration Options

Key Sorting

By default, front matter keys preserve their original order. To sort keys alphabetically for consistency, use the --sort-front-matter flag.

# Default behavior - preserves original key order
mdformat document.md

# Sort keys alphabetically
mdformat document.md --sort-front-matter

Strict Mode

Enable strict mode to fail on invalid front matter instead of preserving it. Useful for CI/CD pipelines.

mdformat document.md --strict-front-matter

In strict mode:

  • Invalid front matter raises an error
  • Front matter without valid key-value pairs raises an error
  • Ensures your documents have correctly formatted metadata

Example usage in pre-commit:

repos:
  - repo: https://github.com/executablebooks/mdformat
    rev: 0.7.19
    hooks:
      - id: mdformat
        args: [--strict-front-matter]
        additional_dependencies:
          - mdformat-front-matters

HTML Rendering

To hide Front Matter from generated HTML output, front_matters_plugin can be imported from mdit_plugins. For more guidance on MarkdownIt, see the docs: https://markdown-it-py.readthedocs.io/en/latest/using.html#the-parser

from markdown_it import MarkdownIt

from mdformat_front_matters.mdit_plugins import front_matters_plugin

md = MarkdownIt()
md.use(front_matters_plugin)

text = """
+++
title = "Example"
draft = false
+++
# Example
"""
md.render(text)
# <h1>Example</h1>

-->

Contributing

See CONTRIBUTING.md

Metadata

Release files for mdformat-front-matters 2.0.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for mdformat-front-matters 2.0.0
File Size Uploaded
mdformat_front_matters-2.0.0.tar.gz 12.6 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for mdformat-front-matters 2.0.0
File Interpreter ABI Platform
mdformat_front_matters-2.0.0-py3-none-any.whl Python 3 none any Details

Total release size: 26.0 kB

Release files / mdformat_front_matters-2.0.0.tar.gz

Download URL mdformat_front_matters-2.0.0.tar.gz
Size 12.6 kB
Tags Source
SHA-256 checksum
How to use checksums
46efabe93707699120c67d4b33b51fdb4cfe0e040acfb0f8212eb33c9a3e8a9a
BLAKE2b-256 checksum
How to use checksums
2c53008805510d3006d9aec6c602fa40305750cb96daba8ea9abae477149909b
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 Dec 4, 2025.

Transparency log

Release files / mdformat_front_matters-2.0.0-py3-none-any.whl

Download URL mdformat_front_matters-2.0.0-py3-none-any.whl
Size 13.4 kB
Tags Python 3
SHA-256 checksum
How to use checksums
8ea92d23d1e9427fe6548b9044e5fbf7bcf6c217941f1a9442e0efd8b0a76e61
BLAKE2b-256 checksum
How to use checksums
00054c197c000dab41f1f58290221a4c02bd57804ff42e9374d3dd0f5408aa40
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 Dec 4, 2025.

Transparency log

Release history Release notifications | RSS feed

This release

2.0.0 This release

2 release files

1.1.0

2 release files

1.0.0

2 release files

0.1.0

2 release files

0.0.1

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page