diff --git a/tildes/tests/test_markdown.py b/tildes/tests/test_markdown.py index 758f3c9..63c0995 100644 --- a/tildes/tests/test_markdown.py +++ b/tildes/tests/test_markdown.py @@ -356,3 +356,12 @@ def test_group_ref_inside_code_ignored(): processed = convert_markdown_to_safe_html(markdown) assert "]*?)" alt="([^"<>]*?)" />') + @histogram_timer("markdown_processing") def convert_markdown_to_safe_html(markdown: str) -> str: @@ -177,6 +179,9 @@ def postprocess_markdown_html(html: str) -> str: # apply syntax highlighting to code blocks html = apply_syntax_highlighting(html) + # replace elements that were generated by `![ ]( )` Markdown syntax + html = strip_image_elements(html) + return html @@ -223,6 +228,20 @@ def apply_syntax_highlighting(html: str) -> str: return html +def strip_image_elements(html: str) -> str: + """Replace all elements generated from Markdown with . + + The Markdown syntax `![alt text](/url)` creates an image tag. Except for the leading + `!`, the syntax is identical to that of links. We can pretend we never parsed it as + a Markdown image by replacing all image tags with links. + + alt text + ==> + !alt text + """ + return STRIP_IMAGE_ELEMENTS_REGEX.sub(r'!\2', html) + + class LinkifyFilter(Filter): """html5lib Filter to convert custom text patterns to links.