From 064d1849bbf1be464a2a1e73bb1c9c12acbe9b2a Mon Sep 17 00:00:00 2001 From: Vivid Date: Sun, 31 May 2026 04:01:58 +0000 Subject: [PATCH 1/2] Fix quadratic-time linebreak parsing This is a quick hack to fix issue #219. The regex pattern used for parsing linebreaks combined with the use of `re.finditer` runs in quadratic time when the parsing input doesn't contain a newline character. By checking for the presence of a newline character the problematic pattern can be short-circuited. Ideally a proper fix would fix the regex pattern itself, but this is a useful fix to have in place until the pattern is fixed. Fixes #219 --- marko/inline.py | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/marko/inline.py b/marko/inline.py index d4f1db6..817a265 100644 --- a/marko/inline.py +++ b/marko/inline.py @@ -86,6 +86,16 @@ def __init__(self, match: _Match) -> None: self.soft = not match.group(1).startswith((" ", "\\")) self.children = "\n" + @classmethod + def find(cls, text: str, *, source: Source) -> Iterator[_Match]: + """This method should return an iterable containing matches of this element.""" + # HACK: short circuit to avoid quadratic runtime when text doesn't have a linebreak. + # ideally the regex pattern should be rewritten, but this works for now. + # see issue #219 + if '\n' not in text: + return [] + return super().find(text, source=source) + class InlineHTML(InlineElement): priority = 7 From f01a565b07c50aefa8ed6f8a13c2cbae42f8c22b Mon Sep 17 00:00:00 2001 From: Frost Ming Date: Mon, 1 Jun 2026 13:05:38 +0800 Subject: [PATCH 2/2] Apply suggestion from @frostming --- marko/inline.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/marko/inline.py b/marko/inline.py index 817a265..4ea803a 100644 --- a/marko/inline.py +++ b/marko/inline.py @@ -92,7 +92,7 @@ def find(cls, text: str, *, source: Source) -> Iterator[_Match]: # HACK: short circuit to avoid quadratic runtime when text doesn't have a linebreak. # ideally the regex pattern should be rewritten, but this works for now. # see issue #219 - if '\n' not in text: + if "\n" not in text: return [] return super().find(text, source=source)