Skip to content

🐛 fix(markdown): keep digit-run scans linear - #2296

Merged
gaborbernat merged 1 commit into
tox-dev:mainfrom
gaborbernat:fix/markdown-repeat-linear
Nov 16, 2026
Merged

gaborbernat merged 1 commit into
tox-dev:mainfrom
gaborbernat:fix/markdown-repeat-linear

Conversation

@gaborbernat

Copy link
Copy Markdown
Member

Converting HTML to Markdown took quadratic time on a run of digits when elements that write no Markdown, such as <wbr>, sat between the digits (#2286). Once this merges, conversion time grows linearly with the input, and the Markdown output stays the same.

The Markdown writer checks whether the current line still holds only digits, which decides whether a following 1. or 1) is a list marker that needs escaping. That check scanned back over every digit on the line for each text node after them. It now keeps a cursor over the digits it has verified, so each digit is inspected once. The cursor is saved and restored with the line state of converted elements and table cells, and it restarts from the start of the run when a marker's markup is inserted into the line.

Fixes #2286

The line-number check in the Markdown writer looked back over every
digit written on the current line for each text node that followed
them. Elements that write no Markdown, such as <wbr>, leave the run in
place, so a long run of digits split by such elements took quadratic
time while the output grew linearly.

The check now keeps a cursor over the digits it has verified and resumes
from it, so each digit is inspected once. The cursor is saved and
restored with the line state of converted elements and table cells, and
it is checked again from the run's start when a marker's markup is
inserted into the line. Output is unchanged.
@gaborbernat gaborbernat added the bug Something isn't working label Nov 16, 2026
@gaborbernat
gaborbernat merged commit e79c340 into tox-dev:main Nov 16, 2026
1 of 48 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working

Projects

None yet

Development

Successfully merging this pull request may close these issues.

to_markdown time grows quadratically on digits followed by empty elements

1 participant