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.

Features

  • Multi-format support: Handles YAML (---), TOML (+++), and JSON ({...}) front matter
  • Automatic normalization: Formats front matter consistently (preserves key order by default, standardized indentation)
  • Quote preservation: YAML string quotes ("double", 'single', | block literal, > block folded) are preserved by default
  • Configurable sorting: Option to sort keys alphabetically with --sort-front-matter
  • YAML normalization modes: --normalize-front-matter {none,minimal,1.2} — from style-only cleanup to full YAML 1.1 → 1.2 upgrade
  • 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; it auto-recognizes the plugin, no configuration needed. See more on mdformat plugins

pre-commit / prek

repos:
  - repo: https://github.com/executablebooks/mdformat
    rev: 1.0.0
    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

All options are independent and composable — use any combination.

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

YAML Normalization Modes

Use --normalize-front-matter to apply progressively more opinionated YAML normalization. The flag accepts one of three values:

Mode What changes
none (default) Nothing — full round-trip, all styles preserved
minimal Strip unnecessary quotes + null normalization + boolean case
1.2 Everything in minimal + YAML 1.1 boolean word upgrade
# Default — preserve everything
mdformat document.md

# Strip quotes, normalize null and boolean casing
mdformat document.md --normalize-front-matter minimal

# Everything in minimal, plus upgrade YAML 1.1 boolean words
mdformat document.md --normalize-front-matter 1.2

minimal example:

# Before
---
title: "My Post"
draft: True
empty: ~
---

# After --normalize-front-matter minimal
---
title: My Post
draft: true
empty: null
---

1.2 example (additionally converts YAML 1.1 boolean words):

# Before
---
draft: yes
published: no
label: "yes"
---

# After --normalize-front-matter 1.2
---
draft: true
published: false
label: yes
---

Semantically necessary quotes are always preserved (values containing colons, empty strings, etc.). Block scalar styles (| and >) are always preserved. Quoted values like "yes" or 'no' retain their string semantics. Optional surrounding quotes may be removed during normalization, but the value remains a string (not a boolean). Only unquoted yes/no/on/off (and capitalized variants) are treated as YAML 1.1 booleans. TOML and JSON are unaffected.

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

Configuration File

All options can be set in .mdformat.toml instead of passing CLI flags:

[plugin.front_matters]
sort_front_matter = true
normalize_front_matter = "minimal"  # or "1.2" or "none"
strict_front_matter = true

Or via the Python API:

import mdformat

mdformat.text(
    content,
    extensions={"front_matters"},
    options={"plugin": {"front_matters": {"normalize_front_matter": "minimal"}}},
)

HTML Rendering

To hide Front Matter from generated HTML output, import front_matters_plugin from mdit_plugins. For more 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.2.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.2.0
File Size Uploaded Provenance Actions
mdformat_front_matters-2.2.0.tar.gz 18.5 kB With provenance Details for mdformat_front_matters-2.2.0.tar.gz

Built distribution (wheel)

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

Total release size: 34.5 kB

Release files / mdformat_front_matters-2.2.0.tar.gz

Download URL mdformat_front_matters-2.2.0.tar.gz
Size 18.5 kB
Tags Source
SHA-256 checksum
How to use checksums
85caeeee6b7e8b7bfe050b6062bdc5eee7f6bf174686eb58fb29ed5021b0d1f9
BLAKE2b-256 checksum
How to use checksums
6bdeaf4dca5bab816e458e72c20c5f31327be26e8107d5ef765b45d4922e7f6a
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.16

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 2, 2026.

Transparency log

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

Download URL mdformat_front_matters-2.2.0-py3-none-any.whl
Size 16.0 kB
Tags Python 3
SHA-256 checksum
How to use checksums
61db60c42447708751e8af1c3a2e55a8fb24627c1ad7094068f7693731e1f28d
BLAKE2b-256 checksum
How to use checksums
3d002da3eebb6e35a50d56a5adbe0142f1ba83012955ba5934d8a822d0150534
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.16

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 2, 2026.

Transparency log

Security for mdformat-front-matters 2.2.0

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.

Full provenance

Both files in this release have been uploaded with provenance metadata.

Select from 2 files

Attestation for mdformat_front_matters-2.2.0.tar.gz

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 2, 2026.

Transparency log

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

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