Frontmatter API Reference¶
Auto-generated API documentation for the frontmatter module.
frontmatter
¶
Frontmatter parser and serializer — zero dependencies, stdlib only, Python 3.10+.
Part of zerodep: https://github.com/Oaklight/zerodep Copyright (c) 2026 Peng Ding. MIT License.
Parse and serialize file-header metadata (frontmatter) in YAML, TOML, or JSON
format. YAML --- frontmatter is the de facto standard used by Jekyll, Hugo,
Astro, MkDocs, Obsidian, and many other tools.
Example::
from frontmatter import loads, dumps, Document
# Parse
doc = loads("""---
title: Hello World
tags: [python, zerodep]
---
# Hello
This is the content.
""")
print(doc.metadata) # {'title': 'Hello World', 'tags': ['python', 'zerodep']}
print(doc.content) # '# Hello\n\nThis is the content.\n'
# Serialize
doc = Document({"title": "New Post"}, "Some content.")
print(dumps(doc))
# TOML frontmatter (Python 3.11+)
doc = loads("""+++
title = "Hello"
+++
Content here.
""")
# JSON frontmatter
doc = loads('{"title": "Hello"}\nContent here.')
Requires Python 3.10+.
FrontmatterError
¶
HandlerError
¶
Bases: FrontmatterError
Raised when a requested handler is not available.
Attributes:
| Name | Type | Description |
|---|---|---|
handler |
Name of the handler that caused the error. |
Source code in frontmatter/frontmatter.py
Document
dataclass
¶
A document with frontmatter metadata and body content.
Attributes:
| Name | Type | Description |
|---|---|---|
metadata |
dict[str, Any]
|
Parsed frontmatter key-value pairs. |
content |
str
|
The body text after the frontmatter block. |
Source code in frontmatter/frontmatter.py
detect_handler(text)
¶
Detect the frontmatter format of a text string.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
text
|
str
|
The full document text. |
required |
Returns:
| Type | Description |
|---|---|
str | None
|
|
str | None
|
is detected. |
Source code in frontmatter/frontmatter.py
check(text)
¶
Check whether a text string contains frontmatter.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
text
|
str
|
The full document text. |
required |
Returns:
| Type | Description |
|---|---|
bool
|
|
loads(text, *, handler=None)
¶
Parse a text string with frontmatter.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
text
|
str
|
The full document text. |
required |
handler
|
str | None
|
Force a specific format ( |
None
|
Returns:
| Type | Description |
|---|---|
Document
|
A |
Raises:
| Type | Description |
|---|---|
FrontmatterError
|
If parsing fails. |
HandlerError
|
If the requested handler is not available. |
Example::
doc = loads("---\ntitle: Hello\n---\nBody text.")
doc.metadata # {'title': 'Hello'}
doc.content # 'Body text.'
Source code in frontmatter/frontmatter.py
load(source, *, handler=None)
¶
Parse a file with frontmatter.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
source
|
IO[str] | str | Path
|
A file path ( |
required |
handler
|
str | None
|
Force a specific format. If |
None
|
Returns:
| Type | Description |
|---|---|
Document
|
A |
Example::
doc = load("post.md")
doc = load(Path("post.md"))
with open("post.md") as f:
doc = load(f)
Source code in frontmatter/frontmatter.py
dumps(doc, *, handler='yaml', **kwargs)
¶
Serialize a Document to a string with frontmatter.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
doc
|
Document
|
The document to serialize. |
required |
handler
|
str
|
Output format ( |
'yaml'
|
**kwargs
|
Any
|
Passed to the underlying serializer. |
{}
|
Returns:
| Type | Description |
|---|---|
str
|
The full document text with frontmatter and body. |
Example::
doc = Document({"title": "Hello"}, "Body text.")
text = dumps(doc)
# ---
# title: Hello
# ---
# Body text.
Source code in frontmatter/frontmatter.py
dump(doc, dest, *, handler='yaml', **kwargs)
¶
Serialize a Document to a file with frontmatter.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
doc
|
Document
|
The document to serialize. |
required |
dest
|
IO[str] | str | Path
|
A file path ( |
required |
handler
|
str
|
Output format. Defaults to |
'yaml'
|
**kwargs
|
Any
|
Passed to the underlying serializer. |
{}
|
Example::
dump(doc, "post.md")
dump(doc, Path("post.md"), handler="toml")
with open("post.md", "w") as f:
dump(doc, f)