Repository navigation
fix(core): do not hang the markdown parser on a line with many pipes - #3391
Conversation
|
The latest updates on your projects. Learn more about Vercel for GitHub.
|
|
No actionable comments were generated in the recent review. Walkthrough
Merge Risk:
|
@blocknote/ariakit
@blocknote/code-block
@blocknote/core
@blocknote/diagram-block
@blocknote/mantine
@blocknote/math-block
@blocknote/react
@blocknote/server-util
@blocknote/shadcn
@blocknote/xl-ai
@blocknote/xl-docx-exporter
@blocknote/xl-email-exporter
@blocknote/xl-multi-column
@blocknote/xl-odt-exporter
@blocknote/xl-pdf-exporter
@blocknote/xl-typst-exporter
commit: |
|
A paste of markdown did not end if a line started with a pipe, had many pipes and did not end with a pipe. An example is a wide table row without the last pipe. The time became about four times longer for each two more pipes: about 2 seconds for a line with 27 pipes, about 34 seconds for a line with 31 pipes. The cause: the test "is this line a table row" was /^\s*\|(.+\|)+\s*$/. The group `(.+\|)+` has a quantifier inside a repeated group. For a line that does not match, the regular expression engine tries each way to divide the line at its pipes. The fix: the test is /^\s*\|.+\|\s*$/. It matches the same lines, and its time is linear. The expression was in four places: - two in markdownToHtml.ts (a line that ends a paragraph, and a line that ends a quote). They now use one constant. - two in detectMarkdown.ts (`isMarkdown`, which the default paste handler calls before it reads the HTML or the plain text). No parse result changes. The tests are plain tests of the result: one in the tests of `isMarkdown`, and two markdown parse cases with a snapshot. They have no time limit. Without the fix, these tests do not end.
89d0786 to
859096b
Compare
main has 91 new commits. Most core conflicts come from core fixes of this branch that went to main as separate pull requests, often in a changed form. For these files, the form of main is the reviewed one. Resolutions: - y/extensions/blockMatchNodes.ts and its browser test, y/utils.ts and its test: the files of main (#3380: blocks pair by ID only). - api/nodeConversions/blockToNode.ts: main (#3383). - api/parsers/html/util/nestedLists.ts and its snapshot: main (#3393, #3404). - blocks/File/block.ts, createLinkWithCaption.ts: main (#3401, #3387). - blocks/Image, Video, Audio: main (#3400). - CheckListItem/block.ts: main (#3388). NumberedListItem/block.ts: main (#3403). - api/exporters/markdown/htmlToMarkdown.ts and api/parsers/markdown/markdownToHtml.ts: main (#3385, #3392, #3394, #3391 and later commits). The parts of the branch that main does not have and that the lens needs come back in separate commits after this merge. - markdownExporter.ts, parseMarkdown.ts, ExportManager.ts, copyExtension.ts, SideMenu/dragging.ts merged without a conflict, but they used the API of the old markdown files of the branch (`escapes`, `target`): main. - serializeBlocksExternalHTML.ts merged without a conflict, but the merge had the toggle child order fix two times and did not compile: the form of main (#3388), with the white space placeholder of the branch. - exportParseEqualityTestInstances.ts: main (the toggle case of #3388). - vite.config.ts: main, with the lint rules of the lens. - tests/vite.config.browser.ts: main, with the lens files. - pnpm-lock.yaml: main, then `vp install`. main moved the shared test document to tests/src/fixtures/exporters (#3420). The lens test helper imports it from there, and the xl-ai build does not include the lens test helpers and benchmarks (a package build cannot read a file outside the package).
Summary
A paste of text did not end if a line started with a pipe, had many pipes and did not end with a pipe. An example is a wide table row without the last pipe: about 2 seconds for 27 pipes, and four times longer for each two more pipes.
The test "is this line a table row" was
/^\s*\|(.+\|)+\s*$/. A repeated group with a quantifier in it takes exponential time for a line that does not match. The test is now/^\s*\|.+\|\s*$/. It matches the same lines, in linear time. The expression was in four places: two in the markdown parser, and two inisMarkdown, which the default paste handler calls for each paste.Testing
isMarkdown, and two markdown parse cases. They test the result and have no time limit. Onmainthey do not end.Summary by CodeRabbit