{"id":1543,"library":"markdown2","title":"Markdown2","description":"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.","status":"active","version":"2.5.5","language":"en","source_language":"en","source_url":"https://github.com/trentm/python-markdown2","tags":["markdown","html conversion","text processing","documentation"],"install":[{"cmd":"pip install markdown2","lang":"bash","label":"Install stable version"},{"cmd":"pip install markdown2[all]","lang":"bash","label":"Install with all optional dependencies (e.g., Pygments for code highlighting)"}],"dependencies":[{"reason":"Optional for the 'fenced-code-blocks' extra to provide syntax highlighting.","package":"Pygments","optional":true}],"imports":[{"note":"The primary function for converting Markdown text to HTML.","symbol":"markdown","correct":"from markdown2 import markdown"},{"note":"The class-based interface, useful for configuring a Markdown converter with specific extras or settings.","symbol":"Markdown","correct":"from markdown2 import Markdown"}],"quickstart":{"code":"import markdown2\n\nmarkdown_text = \"\"\"\n# Hello, Markdown2!\n\nThis is a paragraph with *emphasis* and **strong emphasis**.\n\n- List item 1\n- List item 2\n\n```python\nprint('Hello from a code block!')\n```\n\nCheckout the [Markdown2 GitHub page](https://github.com/trentm/python-markdown2).\n\"\"\"\n\n# Basic conversion\nhtml = markdown2.markdown(markdown_text)\nprint(\"--- Basic Conversion ---\")\nprint(html)\n\n# Conversion with an extra (e.g., 'fenced-code-blocks' for syntax highlighting)\nhtml_with_extras = markdown2.markdown(markdown_text, extras=[\"fenced-code-blocks\", \"footnotes\"])\nprint(\"\\n--- Conversion with Extras ---\")\nprint(html_with_extras)\n\n# Using the class-based API\nmarkdowner = markdown2.Markdown(extras=[\"tables\", \"header-ids\"])\nhtml_from_class = markdowner.convert(\"| Header 1 | Header 2 |\\n|----------|----------|\\n| Cell 1   | Cell 2   |\\n\\n### My Section\")\nprint(\"\\n--- Class-based Conversion with Extras ---\")\nprint(html_from_class)","lang":"python","description":"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."},"warnings":[{"fix":"Replace `extras=['code-color']` with `extras=['fenced-code-blocks']` in your `markdown2.markdown()` call or `Markdown` object initialization. Ensure Pygments is installed for syntax highlighting: `pip install Pygments`.","message":"The 'code-color' extra (for Pygments-based syntax highlighting) is deprecated. Use the 'fenced-code-blocks' extra instead, which offers better and more modern syntax highlighting capabilities.","severity":"deprecated","affected_versions":">=2.3.9"},{"fix":"Always pass the HTML output from `markdown2.markdown()` through an HTML sanitization library like 'Bleach' before rendering it in a web browser, especially when dealing with untrusted input. Example: `import bleach; sanitized_html = bleach.clean(html_output)`.","message":"Markdown2 directly outputs HTML, including any raw HTML present in the input Markdown. If processing untrusted user-generated content, ensure you sanitize the resulting HTML with a dedicated HTML sanitizer (e.g., 'Bleach') to prevent Cross-Site Scripting (XSS) vulnerabilities.","severity":"gotcha","affected_versions":"All versions"},{"fix":"Adhere strictly to Markdown syntax guidelines. Ensure blank lines separate block-level elements. Always put a space after `#` for headings. Maintain consistent indentation (usually 2 or 4 spaces) for lists and indented code blocks. Tools like Markdown linters can help catch these issues.","message":"Inconsistent or incorrect Markdown syntax can lead to unexpected rendering. Common pitfalls include missing blank lines between paragraphs or block-level elements, lack of space after heading hashes (`#`), or incorrect indentation for lists and code blocks.","severity":"gotcha","affected_versions":"All versions"}],"env_vars":null,"last_verified":"2026-04-09T00:00:00.000Z","next_check":"2026-07-08T00:00:00.000Z"}