From 56464ec2312d333aee60a75671cb2848c0c53978 Mon Sep 17 00:00:00 2001
From: Shreyas Mayya <2022smayya@tjhsst.edu>
Date: Sun, 30 Jan 2022 15:13:23 -0500
Subject: [PATCH] feat(destinations): add strikethrough extension

---
 .../destinations/templatetags/markdown.py     |  6 ++-
 .../destinations/templatetags/sanitize.py     |  6 ++-
 .../templatetags/strikethrough.py             | 39 +++++++++++++++++++
 3 files changed, 49 insertions(+), 2 deletions(-)
 create mode 100644 tjdests/apps/destinations/templatetags/strikethrough.py

diff --git a/tjdests/apps/destinations/templatetags/markdown.py b/tjdests/apps/destinations/templatetags/markdown.py
index e3c52be..c6ae66f 100644
--- a/tjdests/apps/destinations/templatetags/markdown.py
+++ b/tjdests/apps/destinations/templatetags/markdown.py
@@ -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()]
+    )
diff --git a/tjdests/apps/destinations/templatetags/sanitize.py b/tjdests/apps/destinations/templatetags/sanitize.py
index 6a13e14..e04f277 100644
--- a/tjdests/apps/destinations/templatetags/sanitize.py
+++ b/tjdests/apps/destinations/templatetags/sanitize.py
@@ -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"]}
 
 
diff --git a/tjdests/apps/destinations/templatetags/strikethrough.py b/tjdests/apps/destinations/templatetags/strikethrough.py
new file mode 100644
index 0000000..ab1df5f
--- /dev/null
+++ b/tjdests/apps/destinations/templatetags/strikethrough.py
@@ -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"
+        )