Home > python > python-markdown: add support for strikethrough

python-markdown: add support for strikethrough

In a webapp of mine I use markdown with the excellent Python-Markdown package. However, it doesn’t support strikethrough by default.

The good news is that you can add 3rd-party extensions to Python-Markdown. With the extension “mdx_del_ins” you can use the <del> and <ins> tags.

Here is a Python function that converts markdown to HTML:

import bleach
from markdown import markdown

def md_to_html(md):
    Markdown to HTML conversion.
    allowed_tags = ['a', 'abbr', 'acronym', 'b',
                    'blockquote', 'code', 'em',
                    'i', 'li', 'ol', 'pre', 'strong',
                    'ul', 'h1', 'h2', 'h3', 'p', 'br', 'ins', 'del']
    return bleach.linkify(bleach.clean(
        markdown(md, output_format='html', extensions=['nl2br', 'del_ins']),
        tags=allowed_tags, strip=True))


TODO list
* ~~strikethrough in Python-Markdown~~

TODO list
* strikethrough in Python-Markdown

Categories: python Tags: ,
  1. June 13, 2017 at 19:50

    Thanks for the writeup, I used it in my Django project, it works like a charm


  1. No trackbacks yet.

Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s

%d bloggers like this: