Просмотр исходного кода

Closes #7531: Add Markdown support for strikethrough formatting

jeremystretch 4 лет назад
Родитель
Сommit
6a369ac985

+ 1 - 0
docs/release-notes/version-3.0.md

@@ -6,6 +6,7 @@
 
 * [#2101](https://github.com/netbox-community/netbox/issues/2101) - Add missing `q` filters for necessary models
 * [#7424](https://github.com/netbox-community/netbox/issues/7424) - Add virtual chassis filters for device components
+* [#7531](https://github.com/netbox-community/netbox/issues/7531) - Add Markdown support for strikethrough formatting
 * [#7542](https://github.com/netbox-community/netbox/issues/7542) - Add optional VLAN group column to prefixes table
 * [#7803](https://github.com/netbox-community/netbox/issues/7803) - Improve live reloading of custom scripts
 * [#7810](https://github.com/netbox-community/netbox/issues/7810) - Add IEEE 802.15.1 interface type

+ 16 - 0
netbox/utilities/markdown.py

@@ -0,0 +1,16 @@
+import markdown
+from markdown.inlinepatterns import SimpleTagPattern
+
+STRIKE_RE = r'(~{2})(.+?)(~{2})'
+
+
+class StrikethroughExtension(markdown.Extension):
+    """
+    A python-markdown extension which support strikethrough formatting (e.g. "~~text~~").
+    """
+    def extendMarkdown(self, md):
+        md.inlinePatterns.register(
+            markdown.inlinepatterns.SimpleTagPattern(STRIKE_RE, 'del'),
+            'strikethrough',
+            200
+        )

+ 2 - 1
netbox/utilities/templatetags/helpers.py

@@ -15,6 +15,7 @@ from django.utils.safestring import mark_safe
 from markdown import markdown
 
 from utilities.forms import get_selected_values, TableConfigForm
+from utilities.markdown import StrikethroughExtension
 from utilities.utils import foreground_color
 
 register = template.Library()
@@ -54,7 +55,7 @@ def render_markdown(value):
     value = re.sub(pattern, '[\\1]: \\3', value, flags=re.IGNORECASE)
 
     # Render Markdown
-    html = markdown(value, extensions=['fenced_code', 'tables'])
+    html = markdown(value, extensions=['fenced_code', 'tables', StrikethroughExtension()])
 
     return mark_safe(html)