Require a space after # for markdown titles

Lets us types stuff like #room-aliases without having to escape the # or
get a bad surprise
This commit is contained in:
miruka 2021-03-30 17:42:19 -04:00
parent c948c75d36
commit 9fa2816d4b

View File

@ -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: `<color>(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 `<span data-mx-color=#hex>`."""
@ -186,10 +199,11 @@ class HTMLProcessor:
# hard_wrap: convert all \n to <br> 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 = [