Repository navigation
fix(preset-commonmark): keep remarkLineBreak linear on long runs of spaces - #2504
Merged
Saul-Mirone merged 1 commit intoOct 23, 2026
Merged
Conversation
…paces remarkLineBreak splits text nodes with /[\t ]*(?:\r?\n|\r)/g. On a run of spaces or tabs with no line ending after it, the regex tries a match at every position in the run, and each attempt scans to the end of the run before it fails. Parsing `'a' + ' '.repeat(50_000) + 'b'` took several seconds. Add a lookbehind so that a match can start only at the start of a run. A match inside a run can only succeed when the match at the start of the run succeeds, so the matches stay the same. Closes #2503 Co-Authored-By: Claude Opus 5.5 <[email protected]>
|
@b84330808 is attempting to deploy a commit to the Milkdown Team on Vercel. A member of the Team first needs to authorize it. |
|
@milkdown/components
@milkdown/core
@milkdown/crepe
@milkdown/ctx
@milkdown/exception
@milkdown/kit
@milkdown/prose
@milkdown/transformer
@milkdown/utils
@milkdown/react
@milkdown/vue
@milkdown/plugin-automd
@milkdown/plugin-block
@milkdown/plugin-clipboard
@milkdown/plugin-collab
@milkdown/plugin-cursor
@milkdown/plugin-diff
@milkdown/plugin-emoji
@milkdown/plugin-highlight
@milkdown/plugin-history
@milkdown/plugin-indent
@milkdown/plugin-listener
@milkdown/plugin-prism
@milkdown/plugin-slash
@milkdown/plugin-streaming
@milkdown/plugin-tooltip
@milkdown/plugin-trailing
@milkdown/plugin-upload
@milkdown/preset-commonmark
@milkdown/preset-gfm
@milkdown/theme-nord
commit: |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Closes #2503.
remarkLineBreaksplits text nodes with/[\t ]*(?:\r?\n|\r)/g. When a text node has a long run of spaces or tabs with no line ending after it, the regex retries from every position in the run, and each try scans to the end of the run before failing. So it's quadratic in the run length:'a' + ' '.repeat(50_000) + 'b'takes several seconds, which hitsdefaultValue,replaceAlland markdown paste.This adds the lookbehind @malayyka suggested in the issue,
/(?<![\t ])[\t ]*(?:\r?\n|\r)/g, so a match can only start at the beginning of a run. A match that starts inside a run only succeeds if the one at the start of the run does too, so the matches themselves don't change. Lookbehind is already used instrong.tsandstrike-through.ts, and the build targets ES2018.I used Claude Code to write this change.
How did you test this change?
Added
packages/plugins/preset-commonmark/src/__test__/remark-line-break.spec.ts. It runs the preset's remark plugins on hand-built text nodes, since micromark already strips whitespace before soft line breaks, so parsing markdown wouldn't reach this part of the regex.b\ncsplits correctly within 500 ms. Onmainthese take about 4.9 s and 5.4 s and fail; with the change they take about 10 ms.I also compared the old and new regex on every string up to length 8 over
' ','\t','\r','\n','a', plus random inputs, and the match positions were identical.pnpm test:unit,pnpm test:lintandtsc -bfor preset-commonmark run clean.