Archive
Posts Tagged ‘markdown’
python-markdown: add support for strikethrough
September 7, 2015
1 comment
Problem
In a webapp of mine I use markdown with the excellent Python-Markdown package. However, it doesn’t support strikethrough by default.
Solution
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))
Input:
TODO list --------- * ~~strikethrough in Python-Markdown~~
Output:
TODO list
* strikethrough in Python-Markdown
Categories: python
markdown, strikethrough
