Skip to main content

An mdformat plugin to format YAML, TOML, or JSON front matter

Project description

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

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_front_matters-2.0.0.tar.gz (12.6 kB view details)

Uploaded Source

Built Distribution

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

mdformat_front_matters-2.0.0-py3-none-any.whl (13.4 kB view details)

Uploaded Python 3

File details

Details for the file mdformat_front_matters-2.0.0.tar.gz.

File metadata

  • Download URL: mdformat_front_matters-2.0.0.tar.gz
  • Upload date:
  • Size: 12.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for mdformat_front_matters-2.0.0.tar.gz
Algorithm Hash digest
SHA256 46efabe93707699120c67d4b33b51fdb4cfe0e040acfb0f8212eb33c9a3e8a9a
MD5 12f82313bb3370ca291ab38f927e7b77
BLAKE2b-256 2c53008805510d3006d9aec6c602fa40305750cb96daba8ea9abae477149909b

See more details on using hashes here.

Provenance

The following attestation bundles were made for mdformat_front_matters-2.0.0.tar.gz:

Publisher: tests.yml on KyleKing/mdformat-front-matters

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_front_matters-2.0.0-py3-none-any.whl.

File metadata

File hashes

Hashes for mdformat_front_matters-2.0.0-py3-none-any.whl
Algorithm Hash digest
SHA256 8ea92d23d1e9427fe6548b9044e5fbf7bcf6c217941f1a9442e0efd8b0a76e61
MD5 0d0116ea35494b883ca2f6e507886761
BLAKE2b-256 00054c197c000dab41f1f58290221a4c02bd57804ff42e9374d3dd0f5408aa40

See more details on using hashes here.

Provenance

The following attestation bundles were made for mdformat_front_matters-2.0.0-py3-none-any.whl:

Publisher: tests.yml on KyleKing/mdformat-front-matters

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