feat(destinations): add strikethrough extension

This commit is contained in:
Shreyas Mayya 2022-01-30 15:13:23 -05:00
parent cef0799efd
commit 56464ec231
No known key found for this signature in database
GPG Key ID: 42522E3641BA2E31
3 changed files with 49 additions and 2 deletions

View File

@ -2,10 +2,14 @@ from markdown import markdown
from django import template
from .strikethrough import StrikethroughExtension
register = template.Library()
@register.filter(name="markdown")
def convert_markdown(text: str):
"""Convert text to markdown HTML."""
return markdown(text, extensions=["extra", "codehilite", "smarty"])
return markdown(
text, extensions=["extra", "codehilite", "smarty", StrikethroughExtension()]
)

View File

@ -5,7 +5,11 @@ from django import template
register = template.Library()
tags = ALLOWED_TAGS + ["h" + str(i) for i in range(1, 7)] + ["div", "p", "pre", "span"]
tags = (
ALLOWED_TAGS
+ ["h" + str(i) for i in range(1, 7)]
+ ["div", "p", "pre", "span", "s"]
)
attrs = {"*": ["class"]}

View File

@ -0,0 +1,39 @@
# -*- coding: utf-8 -*-
# pylint: skip-file
"""Markdown Strikethrough Extension
Extends the Python-Markdown library to support strikethrough text.
Given the text:
The molecular composition of water is ~~HCl~~.
Will output:
<p>The molecular composition of water is <s>HCl</s>.</p>
Based on Markdown Subscript Extension
:website: https://github.com/jambonrose/markdown_subscript_extension
:copyright: Copyright 2014-2018 Andrew Pinkham
:license: Simplified BSD, see LICENSE for details.
This version by Shreyas Mayya.
Pylint was skipped here because python-markdown appears to require
a specific syntax which is at odds with pylint's ruleset.
"""
from __future__ import unicode_literals
from markdown import Extension
from markdown.inlinepatterns import SimpleTagPattern
# match ~~, at least one character that is not ~, and ~~ again
SUBSCRIPT_RE = r"(\~\~)([^(\~)]+)(\~\~)"
def makeExtension(*args, **kwargs): # noqa: N802
"""Inform Markdown of the existence of the extension."""
return StrikethroughExtension(*args, **kwargs)
class StrikethroughExtension(Extension):
"""Extension: text between ~~ characters will be struck through."""
def extendMarkdown(self, md, md_globals): # noqa: N802
"""Insert 's' pattern before 'not_strong' pattern."""
md.inlinePatterns.add(
"strikethrough", SimpleTagPattern(SUBSCRIPT_RE, "s"), "<not_strong"
)