From d1727645892511279103762c796168015fc5767b Mon Sep 17 00:00:00 2001 From: Kevin Flores <101906409+kevincommits@users.noreply.github.com> Date: Thu, 3 Dec 2026 18:23:50 -0500 Subject: [PATCH] =?UTF-8?q?=F0=9F=90=9B=20FIX:=20Escape=20regex=20characte?= =?UTF-8?q?rs=20correctly=20in=20utils.escapeRE?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The replacement string was copied from JavaScript, where "\\$&" inserts a backslash and the matched character. Python's re.sub does not support "$&", so every special character was replaced with the literal text "\$&". --- markdown_it/common/utils.py | 2 +- tests/test_port/test_misc.py | 8 ++++++++ 2 files changed, 9 insertions(+), 1 deletion(-) diff --git a/markdown_it/common/utils.py b/markdown_it/common/utils.py index 5f1bd24b..d11d5029 100644 --- a/markdown_it/common/utils.py +++ b/markdown_it/common/utils.py @@ -152,7 +152,7 @@ def escapeHtml(raw: str) -> str: def escapeRE(string: str) -> str: - string = REGEXP_ESCAPE_RE.sub("\\$&", string) + string = REGEXP_ESCAPE_RE.sub(r"\\\g<0>", string) return string diff --git a/tests/test_port/test_misc.py b/tests/test_port/test_misc.py index b38a896b..b6866ab9 100644 --- a/tests/test_port/test_misc.py +++ b/tests/test_port/test_misc.py @@ -1,4 +1,5 @@ from importlib.util import find_spec +import re import pytest @@ -133,3 +134,10 @@ def test_backtick_closer_ending_at_pos_max(): ("code_inline", "a", "``") ] assert state.pos == 5 + + +def test_escape_re(): + """``utils.escapeRE`` backslash-escapes regex characters, as upstream does.""" + escaped = MarkdownIt().utils.escapeRE("a.b*c$") + assert escaped == r"a\.b\*c\$" + assert re.fullmatch(escaped, "a.b*c$")