From f8811f4e0c292aef0864b6b11b053d0df6b218dc Mon Sep 17 00:00:00 2001 From: miruka Date: Wed, 11 Sep 2019 17:19:24 -0400 Subject: [PATCH] Disable underscore emphasis syntaxes for markdown --- TODO.md | 7 +++---- src/python/html_filter.py | 20 +++++++++++++++++--- 2 files changed, 20 insertions(+), 7 deletions(-) diff --git a/TODO.md b/TODO.md index 597cc4d9..46e3cea0 100644 --- a/TODO.md +++ b/TODO.md @@ -16,6 +16,7 @@ - Time on their own lines - When selecting text and scrolling up, selection stops working after a while - Ensure all the text that should be copied is copied + - Mouse wheel scrolling speed in event list - Pressing backspace in composer sometimes doesn't work - Message order isn't preserved when sending a first message in a E2E @@ -67,7 +68,7 @@ - Combine events so they take less space - After combining is implemented, no need to hide profile changes anymore. - Replies - - Messages editing + - Messages editing and redaction - Code highlighting - Support GIF avatars and images - Adapt shortcuts flicking speed to font size and DPI @@ -127,7 +128,7 @@ - Edit/delete own devices - Request room keys from own other devices - Auto-trust accounts within the same client - - Uploads & proper http thumbnails + - Uploads - Reduce messages ListView cacheBuffer height once http thumbnails downloading is implemented - Read receipts @@ -163,8 +164,6 @@ - Fetch all members when using the filter members bar - Direct chats category - - Markdown: don't turn #things (no space) and `thing\n---` into title, - disable `__` syntax for bold/italic - Better `
` 
   - When inviting someone to direct chat, room is "Empty room" until accepted,
     it should be the peer's display name instead.
diff --git a/src/python/html_filter.py b/src/python/html_filter.py
index 8d545f79..d001bcc1 100644
--- a/src/python/html_filter.py
+++ b/src/python/html_filter.py
@@ -7,8 +7,22 @@ import html_sanitizer.sanitizer as sanitizer
 from html_sanitizer.sanitizer import Sanitizer
 
 
-class MarkdownRenderer(mistune.Renderer):
-    pass
+class MarkdownInlineGrammar(mistune.InlineGrammar):
+    # Enable *word* but not _word_ syntaxes (TODO: config option for that)
+    emphasis        = re.compile(r"^\*((?:\*\*|[^\*])+?)\*(?!\*)")
+    double_emphasis = re.compile(r"^\*{2}([\s\S]+?)\*{2}(?!\*)")
+
+
+class MarkdownInlineLexer(mistune.InlineLexer):
+    grammar_class = MarkdownInlineGrammar
+
+
+    def output_double_emphasis(self, m):
+        return self.renderer.double_emphasis(self.output(m.group(1)))
+
+
+    def output_emphasis(self, m):
+        return self.renderer.emphasis(self.output(m.group(1)))
 
 
 class HtmlFilter:
@@ -41,7 +55,7 @@ class HtmlFilter:
 
         # hard_wrap: convert all \n to 
without required two spaces self._markdown_to_html = mistune.Markdown( - hard_wrap=True, renderer=MarkdownRenderer(), + hard_wrap=True, inline=MarkdownInlineLexer, ) self._markdown_to_html.block.default_rules = [