From 9fa2816d4b427f21fff07695f31d33c572949300 Mon Sep 17 00:00:00 2001 From: miruka Date: Tue, 30 Mar 2021 17:42:19 -0400 Subject: [PATCH] Require a space after # for markdown titles Lets us types stuff like #room-aliases without having to escape the # or get a bad surprise --- src/backend/html_markdown.py | 26 ++++++++++++++++++++------ 1 file changed, 20 insertions(+), 6 deletions(-) diff --git a/src/backend/html_markdown.py b/src/backend/html_markdown.py index aa0c1321..93836bb1 100644 --- a/src/backend/html_markdown.py +++ b/src/backend/html_markdown.py @@ -21,8 +21,6 @@ from .color import SVGColor class MarkdownInlineGrammar(mistune.InlineGrammar): """Markdown inline elements syntax modifications for the Mistune parser. - Modifications: - - Disable underscores for bold/italics (e.g. `__bold__`) - Add syntax for coloring text: `(text)`, @@ -42,6 +40,15 @@ class MarkdownInlineGrammar(mistune.InlineGrammar): ) +class MarkdownBlockGrammar(mistune.BlockGrammar): + """Markdown block elements syntax modifications for the Mistune parser. + + - Require a space after # for titles + """ + + heading = re.compile(r"^ *(#{1,6}) +([^\n]+?) *#* *(?:\n+|$)") + + class MarkdownInlineLexer(mistune.InlineLexer): """Apply the changes from `MarkdownInlineGrammar` for Mistune.""" @@ -74,6 +81,12 @@ class MarkdownInlineLexer(mistune.InlineLexer): return self.renderer.color(color, text) +class MarkdownBlockLexer(mistune.BlockLexer): + """Apply the changes from `MarkdownBlockGrammar` for Mistune.""" + + grammar_class = MarkdownBlockGrammar + + class MarkdownRenderer(mistune.Renderer): def color(self, color: str, text: str) -> str: """Render given text with a color using ``.""" @@ -186,10 +199,11 @@ class HTMLProcessor: # hard_wrap: convert all \n to
without required two spaces # escape: escape HTML characters in the input string, e.g. tags self._markdown_to_html = mistune.Markdown( - hard_wrap=True, - escape=True, - inline=MarkdownInlineLexer, - renderer=MarkdownRenderer(), + hard_wrap = True, + escape = True, + inline = MarkdownInlineLexer, + block = MarkdownBlockLexer, + renderer = MarkdownRenderer(), ) self._markdown_to_html.block.default_rules = [