An mdformat plugin to format YAML, TOML, or JSON front matter
Project description
mdformat-front-matters
An mdformat plugin for normalizing YAML, TOML, and JSON front matter in CommonMark documents.
[!NOTE]
mdformat-frontmatterhas 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
strictmode 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
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_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
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
46efabe93707699120c67d4b33b51fdb4cfe0e040acfb0f8212eb33c9a3e8a9a
|
|
| MD5 |
12f82313bb3370ca291ab38f927e7b77
|
|
| BLAKE2b-256 |
2c53008805510d3006d9aec6c602fa40305750cb96daba8ea9abae477149909b
|
Provenance
The following attestation bundles were made for mdformat_front_matters-2.0.0.tar.gz:
Publisher:
tests.yml on KyleKing/mdformat-front-matters
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
mdformat_front_matters-2.0.0.tar.gz -
Subject digest:
46efabe93707699120c67d4b33b51fdb4cfe0e040acfb0f8212eb33c9a3e8a9a - Sigstore transparency entry: 740334193
- Sigstore integration time:
-
Permalink:
KyleKing/mdformat-front-matters@a86d0727e295552d9bad261ec12eb6425b31ceaf -
Branch / Tag:
refs/tags/v2.0.0 - Owner: https://github.com/KyleKing
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
tests.yml@a86d0727e295552d9bad261ec12eb6425b31ceaf -
Trigger Event:
push
-
Statement type:
File details
Details for the file mdformat_front_matters-2.0.0-py3-none-any.whl.
File metadata
- Download URL: mdformat_front_matters-2.0.0-py3-none-any.whl
- Upload date:
- Size: 13.4 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 |
8ea92d23d1e9427fe6548b9044e5fbf7bcf6c217941f1a9442e0efd8b0a76e61
|
|
| MD5 |
0d0116ea35494b883ca2f6e507886761
|
|
| BLAKE2b-256 |
00054c197c000dab41f1f58290221a4c02bd57804ff42e9374d3dd0f5408aa40
|
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
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
mdformat_front_matters-2.0.0-py3-none-any.whl -
Subject digest:
8ea92d23d1e9427fe6548b9044e5fbf7bcf6c217941f1a9442e0efd8b0a76e61 - Sigstore transparency entry: 740334196
- Sigstore integration time:
-
Permalink:
KyleKing/mdformat-front-matters@a86d0727e295552d9bad261ec12eb6425b31ceaf -
Branch / Tag:
refs/tags/v2.0.0 - Owner: https://github.com/KyleKing
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
tests.yml@a86d0727e295552d9bad261ec12eb6425b31ceaf -
Trigger Event:
push
-
Statement type: