Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions docs/changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ See the [Contributing Guide](contributing.md) for details.

* For backwards compatibility in some languages, revert the enforcement of CommonMark
style punctuation rules (#1657).
* Fix quadratic processing time in the `fenced_code` extension for documents
with many fenced code blocks (#1660).

## [3.11.1] - 2026-10-13

Expand Down
10 changes: 7 additions & 3 deletions markdown/extensions/fenced_code.py
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,9 @@ def run(self, lines: list[str]) -> list[str]:
self.checked_for_deps = True

text = "\n".join(lines)
# Collect the output in pieces, as rebuilding the full text after each block is quadratic.
pieces: list[str] = []
last = 0
index = 0
while 1:
m = self.FENCED_BLOCK_RE.search(text, index)
Expand Down Expand Up @@ -163,12 +166,13 @@ def run(self, lines: list[str]) -> list[str]:
code = f'<pre{id_attr}{class_attr}><code{lang_attr}{kv_pairs}>{code}</code></pre>'

placeholder = self.md.htmlStash.store(code)
text = f'{text[:m.start()]}\n{placeholder}\n{text[m.end():]}'
pieces.append(f'{text[last:m.start()]}\n{placeholder}\n')
# Continue from after the replaced text in the next iteration.
index = m.start() + 1 + len(placeholder)
index = last = m.end()
else:
break
return text.split("\n")
pieces.append(text[last:])
return "".join(pieces).split("\n")

def handle_attrs(self, attrs: Iterable[tuple[str, str]]) -> tuple[str, list[str], dict[str, Any]]:
""" Return tuple: `(id, [list, of, classes], {configs})` """
Expand Down