mdformat-frontmatter
An `mdformat` plugin for ensuring that YAML front-matter is respected and formatted within Markdown files. It supports YAML front matter typically used by tools like Jekyll. The current version is 2.0.10, and it maintains an active release cadence with regular updates and fixes.
Warnings
- gotcha mdformat-frontmatter explicitly supports only YAML front matter. If you require formatting for TOML or JSON front matter, consider alternative plugins like `mdformat-front-matters` (with an 's').
- gotcha Incorrectly formed YAML front matter blocks may lead to unexpected behavior or formatting issues. The plugin's primary objective is to allow properly formed YAML headers to pass through.
- breaking The formatting style produced by `mdformat` (and thus its plugins) can change between versions. This may lead to inconsistent output if `mdformat` itself is not pinned.
- gotcha Prior to v2.0.7, `ruamel.yaml`'s default line-breaking behavior could introduce unexpected wraps within front matter. This was fixed in v2.0.7 to ensure `ruamel.yaml` does not wrap lines.
Install
-
pip install mdformat mdformat-frontmatter
Imports
- mdformat_frontmatter
This is a plugin that mdformat auto-detects upon installation. No direct import statement for `mdformat_frontmatter` is typically needed by the end-user to apply its formatting functionality.
Quickstart
import mdformat
import os
# Example Markdown with YAML front matter
markdown_content = '''---
title: My Document
date: 2026-04-13
---
# Hello, world!
This is some content below the front matter.
'''.strip()
# Format the content (mdformat auto-detects installed plugins)
formatted_content = mdformat.text(markdown_content)
print("Original content:\n" + markdown_content)
print("\nFormatted content:\n" + formatted_content)
# CLI usage (assuming mdformat and mdformat-frontmatter are installed):
# echo -e "---\ntitle: My Document\n---\n\n# Hello" | mdformat -