Mistletoe Markdown Parser

1.5.1 · active · verified Sat Apr 11

Mistletoe is a fast, extensible Markdown parser written in pure Python. It fully supports the CommonMark specification and provides various renderers for output formats like HTML, LaTeX, and even back to Markdown. The library is actively maintained, with the current stable version being 1.5.1, and releases occurring every few months for bug fixes and minor feature enhancements.

Warnings

Install

Imports

Quickstart

This quickstart demonstrates basic Markdown to HTML conversion using the `mistletoe.markdown` function and how to extend `HTMLRenderer` to customize the output, such as adding a CSS class to headings.

from mistletoe import markdown

# Basic Markdown to HTML conversion
markdown_text = "# Hello, Mistletoe!\n\nThis is **bold** text and `code`."
html_output = markdown(markdown_text)
print(f"Basic HTML output:\n{html_output}\n")

# Using a custom renderer to modify output
from mistletoe import Document, HTMLRenderer

class CustomHTMLRenderer(HTMLRenderer):
    def render_heading(self, token):
        # Add a custom class to all headings
        return f'<h{token.level} class="custom-heading">{self.render_inner(token)}</h{token.level}>'

with CustomHTMLRenderer() as renderer:
    document = Document(markdown_text)
    custom_html = renderer.render(document)
print(f"Custom HTML output:\n{custom_html}")

view raw JSON →