From cfe7213a953dbcf81fe7c10a22eff0c11d4b5151 Mon Sep 17 00:00:00 2001 From: shreeraj Date: Mon, 16 Nov 2026 10:38:15 +0530 Subject: [PATCH] =?UTF-8?q?=F0=9F=90=9B=20FIX:=20Allow=20an=20HTML=20comme?= =?UTF-8?q?nt=20whose=20text=20ends=20with=20a=20hyphen?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit CommonMark 0.31 only forbids `-->` inside the text of an HTML comment, so `` and `` are valid raw HTML (cmark, commonmark.js and markdown-it 15 all pass them through). The inline HTML regex still used the older grammar, which cannot match a text that ends in `-`, so these comments were rendered as escaped text: MarkdownIt("commonmark").renderInline("") # '<!-- note --->' Match the text lazily up to the first `-->` instead. Unterminated comments are still rejected before the regex runs, so the scan cannot go past a terminator that exists. --- markdown_it/common/html_re.py | 2 +- tests/test_html_inline.py | 17 +++++++++++++++++ 2 files changed, 18 insertions(+), 1 deletion(-) diff --git a/markdown_it/common/html_re.py b/markdown_it/common/html_re.py index 88cddc44..1856c62d 100644 --- a/markdown_it/common/html_re.py +++ b/markdown_it/common/html_re.py @@ -15,7 +15,7 @@ open_tag = "<[A-Za-z][A-Za-z0-9\\-]*" + attribute + "*\\s*\\/?>" close_tag = "<\\/[A-Za-z][A-Za-z0-9\\-]*\\s*>" -comment = "" +comment = "" processing = "<[?][\\s\\S]*?[?]>" declaration = "]*>" cdata = "" diff --git a/tests/test_html_inline.py b/tests/test_html_inline.py index 4f118797..439695e8 100644 --- a/tests/test_html_inline.py +++ b/tests/test_html_inline.py @@ -72,3 +72,20 @@ def match(self, *args, **kwargs): html = MarkdownIt("commonmark").render(src) assert calls, "expected the regex to run for terminated constructs" assert html == "

" + src + "

\n" + + +@pytest.mark.parametrize( + "src", + [ + "", # the comment text ends with "-" + "", + "", + "", + "", + "", + ], +) +def test_comment_text_may_end_with_hyphen(src): + """CommonMark 0.31 only forbids ``-->`` inside the text of an HTML comment.""" + assert MarkdownIt("commonmark").renderInline(src) == src + assert MarkdownIt("commonmark").render(f"x {src} y") == f"

x {src} y

\n"