Use this file to discover Exa products, pricing, evaluations, comparisons, use cases, agent interfaces, and technical documentation before exploring further.
Python Markdown parser libraries turn Markdown text into HTML or into a tree of tokens that code can inspect. A parser either renders HTML directly or exposes the parsed document as tokens before any rendering happens. These libraries let documentation sites and AI chat apps display formatted text written in Markdown.
Popularity
markdown-it-py has the most monthly downloads in this comparison, at 593.6M.
def read_content(filename):
"""Read content and metadata from file into a dictionary."""
# Read file content. text = fread(filename)
# Read metadata and save it in a dictionary. date_slug = os.path.basename(filename).split('.')[0]
match = re.search(r'^(?:(\d\d\d\d-\d\d-\d\d)-)?(.+)$', date_slug)
content = {
'date': match.group(1) or '1970-01-01',
'slug': match.group(2),
}
# Read headers. end = 0
for key, val, end in read_headers(text):
content[key] = val
# Separate content from headers. text = text[end:]
# Convert Markdown content to HTML. if filename.endswith(('.md', '.mkd', '.mkdn', '.mdown', '.markdown')):
try:
if _test == 'ImportError':
raise ImportError('Error forced by test')
import commonmark
text = commonmark.commonmark(text)
except ImportError as e:
log('WARNING: Cannot render Markdown in {}: {}', filename, str(e))
# Update the dictionary with content and RFC 2822 date. content.update({
'content': text,
'rfc_2822_date': rfc_2822_format(content['date'])
})
return content
mistletoe
Parses Markdown with an extensible API in pure Python.
def _parse(
markdown: str, options: pyromark.Options, *, latex_escape: bool
) -> tuple[str, tuple]:
"""Preprocess and parse Markdown; return the parsed text and its ranged events.
Two delimiter readings that the Markdown extensions allow are rarely what
chat text means, so their delimiters are escaped and the text parsed again
until neither remains. Escaped delimiters never match again, so this ends.
- Inline math whose closing ``$`` is directly followed by a digit, as in
``$10.49 (~$11.20)``, is two prices (pandoc's rule for ``$`` math).
- Strikethrough takes ``~~``; a single ``~`` reads as "approximately".
"""
text = _escape_latex(markdown) if latex_escape else markdown
text = _preprocess_spoilers(text, options)
# pyromark >= 0.10 returns a one-shot iterator; materialize it so the # delimiter scan and EventWalker can both iterate. events = tuple(pyromark.events_with_range(text, options=options))
while offsets := _literal_delimiters(text, events):
text = _escape_at(text, offsets)
events = tuple(pyromark.events_with_range(text, options=options))
return text, events
FAQ
markdown-it-py suits applications that need CommonMark parsing and access to the token stream. Python-Markdown suits projects that follow the original Markdown rules and rely on its extensions. mistune suits applications that want plugins and custom renderers.
Python-Markdown is a Python implementation of John Gruber's original Markdown that converts text to HTML. Its maintainers do not aim for CommonMark compatibility.
markdown-it-py is a Python port of markdown-it. It exposes the parsed tokens before rendering and follows CommonMark as its baseline.
mistune is a Markdown parser with plugins and renderers. Its maintainers state that the current release follows the CommonMark specification.