Modern Markdown parser for Python 3.14t — CommonMark compliant, free-threading ready, typed AST
Project description
ฅᨐฅ Patitas
The secure, typed Markdown parser for modern Python.
from patitas import Markdown
md = Markdown()
html = md("# Hello **World**")
Why Patitas?
| Patitas | mistune | markdown-it-py | |
|---|---|---|---|
| ReDoS-proof | ✅ O(n) FSM lexer | ❌ Regex-based | ✅ Token-based |
| CommonMark | 0.31.2 ✅ | Partial | 0.31.2 ✅ |
| Free-threading | ✅ Python 3.14t safe | ✅ Works | ❌ Crashes |
| Typed AST | ✅ Frozen dataclasses | ❌ Dict[str, Any] |
❌ Token objects |
| Dependencies | Zero | Zero | Zero |
| Directives | ✅ MyST syntax | RST-style | Plugin required |
Patitas is the only CommonMark-compliant parser with typed AST that works safely under Python 3.14t free-threading.
Installation
pip install patitas
Requires Python 3.14+
Optional extras:
pip install patitas[syntax] # Syntax highlighting via Rosettes
pip install patitas[all] # All optional features
Quick Start
| Function | Description |
|---|---|
parse(source) |
Parse Markdown to typed AST |
parse_notebook(content, source_path?) |
Parse Jupyter .ipynb to (markdown, metadata) |
parse_incremental(new, prev, ...) |
Re-parse only the changed region (O(change)) |
render(doc) |
Render AST to HTML |
Markdown() |
All-in-one parser and renderer |
Notebook support
Parse Jupyter notebooks (.ipynb) to Markdown content and metadata — zero dependencies, stdlib JSON only:
from patitas import parse_notebook
with open("demo.ipynb") as f:
content, metadata = parse_notebook(f.read(), "demo.ipynb")
# content: Markdown string (cells → fenced code, outputs → HTML)
# metadata: title, type, notebook{kernel_name, cell_count}, etc.
Security
Patitas is immune to ReDoS attacks.
Traditional Markdown parsers use regex patterns vulnerable to catastrophic backtracking:
# Malicious input that can freeze regex-based parsers
evil = "a](" + "\\)" * 10000
# mistune: hangs for seconds/minutes
# Patitas: completes in milliseconds (O(n) guaranteed)
Patitas uses a hand-written finite state machine lexer:
- Single character lookahead — No backtracking, ever
- Linear time guaranteed — Processing time scales with input length
- Safe for untrusted input — Use in web apps, APIs, user-facing tools
Learn more about Patitas security →
Performance
652 CommonMark examples (single thread):
| Parser | Time | Thread-safe? |
|---|---|---|
| mistune | ~12ms | ✅ |
| Patitas | ~26ms | ✅ |
| markdown-it-py | ~26ms | ❌ Crashes under free-threading |
Incremental parsing — for a 1-char edit in a ~100KB doc, parse_incremental is ~200x faster than full re-parse (~160µs vs ~32ms).
# From repo (after uv sync --group dev):
python benchmarks/benchmark_vs_mistune.py
# All benchmarks including incremental:
pytest benchmarks/benchmark_vs_mistune.py benchmarks/benchmark_incremental.py -v --benchmark-only
Key insights:
- mistune is faster on typical workloads — regex engines are highly optimized
- Patitas scales linearly — ~2.5x speedup with 4 threads under Python 3.14t free-threading
- markdown-it-py crashes under free-threading (race condition in URL encoding)
- Incremental parsing — O(change) re-parse for editor-style workflows
Patitas prioritizes safety over raw speed: O(n) guaranteed parsing, typed AST, full thread-safety, and incremental re-parse.
Features
| Feature | Description |
|---|---|
| CommonMark | Full 0.31.2 spec compliance (652 examples) |
| Typed AST | Immutable frozen dataclasses with slots |
| Plugins | Tables, footnotes, math, strikethrough, task lists |
| Directives | MyST-style blocks (admonition, dropdown, tabs) |
| Roles | Inline semantic markup |
| Incremental | Re-parse only changed blocks — O(change) not O(document) |
| Thread-safe | Zero shared mutable state, free-threading ready |
Usage
Basic Parsing
from patitas import parse, render
# Parse to AST
doc = parse("# Hello **World**")
# Render to HTML
html = render(doc)
# <h1 id="hello-world">Hello <strong>World</strong></h1>
Typed AST — IDE autocomplete, catch errors at dev time
from patitas import parse
from patitas.nodes import Heading, Paragraph, Strong
doc = parse("# Hello **World**")
heading = doc.children[0]
# Full type safety
assert isinstance(heading, Heading)
assert heading.level == 1
# IDE knows the types!
for child in heading.children:
if isinstance(child, Strong):
print(f"Bold text: {child.children}")
All nodes are @dataclass(frozen=True, slots=True) — immutable and memory-efficient.
Directives — MyST-style blocks
:::{note}
This is a note admonition.
:::
:::{warning}
This is a warning.
:::
:::{dropdown} Click to expand
Hidden content here.
:::
:::{tab-set}
:::{tab-item} Python
Python code here.
:::
:::{tab-item} JavaScript
JavaScript code here.
:::
:::
Custom Directives — Extend with your own
from patitas import Markdown, create_registry_with_defaults
from patitas.directives.decorator import directive
# Define a custom directive with the @directive decorator
@directive("alert")
def render_alert(node, children: str, sb) -> None:
sb.append(f'<div class="alert">{children}</div>')
# Extend defaults with your directive
builder = create_registry_with_defaults() # Has admonition, dropdown, tabs
builder.register(render_alert())
# Use it
md = Markdown(directive_registry=builder.build())
html = md(":::{alert} This is important!\n:::")
Syntax Highlighting
With pip install patitas[syntax]:
from patitas import Markdown
md = Markdown(highlight=True)
html = md("""
```python
def hello():
print("Highlighted!")
""")
Uses [Rosettes](https://github.com/lbliii/rosettes) for O(n) highlighting.
</details>
<details>
<summary><strong>Free-Threading</strong> — Python 3.14t</summary>
```python
from concurrent.futures import ThreadPoolExecutor
from patitas import parse
documents = ["# Doc " + str(i) for i in range(1000)]
with ThreadPoolExecutor() as executor:
# Safe to parse in parallel — no shared mutable state
results = list(executor.map(parse, documents))
Patitas is designed for Python 3.14t's free-threading mode (PEP 703).
Migrate from mistune
# Before (mistune)
import mistune
md = mistune.create_markdown()
html = md(source)
# After (patitas) — same API!
from patitas import Markdown
md = Markdown()
html = md(source)
Key differences:
- Patitas uses MyST directive syntax (
:::{note}) vs mistune's RST (.. note::) - Patitas AST is typed dataclasses vs mistune's
Dict[str, Any] - Patitas is ReDoS-proof; mistune uses regex
The Bengal Ecosystem
A structured reactive stack — every layer written in pure Python for 3.14t free-threading.
| ᓚᘏᗢ | Bengal | Static site generator | Docs |
| ∿∿ | Purr | Content runtime | — |
| ⌁⌁ | Chirp | Web framework | Docs |
| =^..^= | Pounce | ASGI server | Docs |
| )彡 | Kida | Template engine | Docs |
| ฅᨐฅ | Patitas | Markdown parser ← You are here | Docs |
| ⌾⌾⌾ | Rosettes | Syntax highlighter | Docs |
Python-native. Free-threading ready. No npm required.
Development
git clone https://github.com/lbliii/patitas.git
cd patitas
uv sync --group dev
pytest
Run benchmarks (after uv sync --group dev):
pip install mistune markdown-it-py # optional: for parser comparison
python benchmarks/benchmark_vs_mistune.py
License
MIT License — see LICENSE for details.
Project details
Release history Release notifications | RSS feed
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 patitas-0.3.0.tar.gz.
File metadata
- Download URL: patitas-0.3.0.tar.gz
- Upload date:
- Size: 208.1 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
8ec5cbe5e385c7b8f0cabbfacf6772d6decdeb561971285bf551d04c1940cc9d
|
|
| MD5 |
c5488f55f6d25594092ddff24ee7326a
|
|
| BLAKE2b-256 |
5031d2de958ec72ea85e49e9cb0c85fc077c1d38a354e032ec8f0bf512d5113f
|
Provenance
The following attestation bundles were made for patitas-0.3.0.tar.gz:
Publisher:
python-publish.yml on lbliii/patitas
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
patitas-0.3.0.tar.gz -
Subject digest:
8ec5cbe5e385c7b8f0cabbfacf6772d6decdeb561971285bf551d04c1940cc9d - Sigstore transparency entry: 953264339
- Sigstore integration time:
-
Permalink:
lbliii/patitas@de1d088177ee8ef2110857a6d826c41c46d5b36f -
Branch / Tag:
refs/tags/v0.3.0 - Owner: https://github.com/lbliii
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
python-publish.yml@de1d088177ee8ef2110857a6d826c41c46d5b36f -
Trigger Event:
release
-
Statement type:
File details
Details for the file patitas-0.3.0-py3-none-any.whl.
File metadata
- Download URL: patitas-0.3.0-py3-none-any.whl
- Upload date:
- Size: 204.5 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 |
af95d1a0391a4ed20661f74a126d55f9b70294b5c0943a65289c5f11dd4ba717
|
|
| MD5 |
c6be4b869353344fc1ef188bf4ae59db
|
|
| BLAKE2b-256 |
749f1df8796624ddef3504beb0f2ba5e73866294eb1fb914db6c49bd16c87122
|
Provenance
The following attestation bundles were made for patitas-0.3.0-py3-none-any.whl:
Publisher:
python-publish.yml on lbliii/patitas
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
patitas-0.3.0-py3-none-any.whl -
Subject digest:
af95d1a0391a4ed20661f74a126d55f9b70294b5c0943a65289c5f11dd4ba717 - Sigstore transparency entry: 953264341
- Sigstore integration time:
-
Permalink:
lbliii/patitas@de1d088177ee8ef2110857a6d826c41c46d5b36f -
Branch / Tag:
refs/tags/v0.3.0 - Owner: https://github.com/lbliii
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
python-publish.yml@de1d088177ee8ef2110857a6d826c41c46d5b36f -
Trigger Event:
release
-
Statement type: