Markdown2

2.5.5 · active · verified Thu Apr 09

Markdown2 is a fast and complete Python implementation of Markdown, designed to closely match the behavior of the original Perl-implemented Markdown.pl. It offers a core Markdown parser and numerous extensions, known as 'extras,' for enhanced functionality like syntax highlighting, tables, and header IDs. The library is actively maintained with periodic releases and currently supports Python 3.9 and newer.

Warnings

Install

Imports

Quickstart

This quickstart demonstrates basic Markdown conversion using the `markdown()` function and how to enable 'extras' (extensions) for enhanced features like fenced code blocks. It also shows the class-based `Markdown` API for more persistent configuration.

import markdown2

markdown_text = """
# Hello, Markdown2!

This is a paragraph with *emphasis* and **strong emphasis**.

- List item 1
- List item 2

```python
print('Hello from a code block!')
```

Checkout the [Markdown2 GitHub page](https://github.com/trentm/python-markdown2).
"""

# Basic conversion
html = markdown2.markdown(markdown_text)
print("--- Basic Conversion ---")
print(html)

# Conversion with an extra (e.g., 'fenced-code-blocks' for syntax highlighting)
html_with_extras = markdown2.markdown(markdown_text, extras=["fenced-code-blocks", "footnotes"])
print("\n--- Conversion with Extras ---")
print(html_with_extras)

# Using the class-based API
markdowner = markdown2.Markdown(extras=["tables", "header-ids"])
html_from_class = markdowner.convert("| Header 1 | Header 2 |\n|----------|----------|\n| Cell 1   | Cell 2   |\n\n### My Section")
print("\n--- Class-based Conversion with Extras ---")
print(html_from_class)

view raw JSON →