From 149332f9231a96e1a981b2bb20f9b6c617c72f72 Mon Sep 17 00:00:00 2001 From: yousefed Date: Sat, 21 Nov 2026 13:19:02 +0100 Subject: [PATCH] fix(core): the markdown parser does not hang on a line with many pipes 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. --- .../parsers/markdown/detectMarkdown.test.ts | 16 ++++++++ .../api/parsers/markdown/detectMarkdown.ts | 7 +++- .../parsers/markdown/markdownToHtml.test.ts | 37 +++++++++++++++++++ .../api/parsers/markdown/markdownToHtml.ts | 9 ++++- 4 files changed, 65 insertions(+), 4 deletions(-) create mode 100644 packages/core/src/api/parsers/markdown/markdownToHtml.test.ts diff --git a/packages/core/src/api/parsers/markdown/detectMarkdown.test.ts b/packages/core/src/api/parsers/markdown/detectMarkdown.test.ts index 9d8a101b79..af89a4a0e7 100644 --- a/packages/core/src/api/parsers/markdown/detectMarkdown.test.ts +++ b/packages/core/src/api/parsers/markdown/detectMarkdown.test.ts @@ -1,3 +1,4 @@ +import { runInNewContext } from "node:vm"; import { describe, expect, it } from "vite-plus/test"; import { isMarkdown } from "./detectMarkdown.js"; @@ -185,6 +186,21 @@ describe("isMarkdown", () => { expect(isMarkdown("| Header 1 | Header 2\n")).toBe(false); // missing closing pipe expect(isMarkdown("Header 1 | Header 2 |\n")).toBe(false); // missing opening pipe }); + + it("should not detect a row with many pipes and no closing pipe", () => { + const text = "|" + " a |".repeat(60) + " x"; + + // The test runner cannot stop a synchronous call: a check that does not + // end blocks the test run. `runInNewContext` stops the check after + // 5 seconds, and the test fails. + const result: unknown = runInNewContext( + "isMarkdown(text)", + { isMarkdown, text }, + { timeout: 5000 }, + ); + + expect(result).toBe(false); + }); }); describe("Edge cases and combinations", () => { diff --git a/packages/core/src/api/parsers/markdown/detectMarkdown.ts b/packages/core/src/api/parsers/markdown/detectMarkdown.ts index be69f24c80..4e4701aed2 100644 --- a/packages/core/src/api/parsers/markdown/detectMarkdown.ts +++ b/packages/core/src/api/parsers/markdown/detectMarkdown.ts @@ -32,13 +32,16 @@ const blockquote = /(?:^|(\r?\n\r?\n))( {0,3}>[^\n]{1,333}\n){1,999}($|(\r?\n))/; // Table Header -const tableHeader = /^\s*\|(.+\|)+\s*$/m; +// Do not write `(.+\|)+` here or in the table row: it matches the same lines, +// but for a line without the last pipe its time grows exponentially with the +// number of pipes. +const tableHeader = /^\s*\|.+\|\s*$/m; // Table Divider const tableDivider = /^\s*\|(\s*[-:]+[-:]\s*\|)+\s*$/m; // Table Row -const tableRow = /^\s*\|(.+\|)+\s*$/m; +const tableRow = /^\s*\|.+\|\s*$/m; /** * Returns `true` if the source text might be a markdown document. diff --git a/packages/core/src/api/parsers/markdown/markdownToHtml.test.ts b/packages/core/src/api/parsers/markdown/markdownToHtml.test.ts new file mode 100644 index 0000000000..1689a8a504 --- /dev/null +++ b/packages/core/src/api/parsers/markdown/markdownToHtml.test.ts @@ -0,0 +1,37 @@ +// @vitest-environment node +import { runInNewContext } from "node:vm"; +import { describe, expect } from "vite-plus/test"; +import { markdownToHtml } from "./markdownToHtml.js"; + +describe("markdownToHtml", () => { + // A line that starts with a pipe, has 60 more pipes, and does not end with + // a pipe. It is not a table row. + const pipeLine = "|" + " a |".repeat(60) + " x"; + + // The test runner cannot stop a synchronous call: a parse that does not end + // blocks the test run. `runInNewContext` stops the parse after 5 seconds, + // and the test fails. + // + // The result is without the line end after `
`: the test is about the + // time and the structure, not about that detail of the HTML. + function markdownToHtmlWithTimeLimit(markdown: string): string { + const html: unknown = runInNewContext( + "markdownToHtml(markdown)", + { markdownToHtml, markdown }, + { timeout: 5000 }, + ); + return String(html).replaceAll("
\n", "
"); + } + + it("keeps a line with many pipes and no last pipe in the paragraph", () => { + expect(markdownToHtmlWithTimeLimit(`x\n${pipeLine}\n`)).toBe( + `

x
${pipeLine}

`, + ); + }); + + it("keeps a line with many pipes and no last pipe in the quote", () => { + expect(markdownToHtmlWithTimeLimit(`> q\n${pipeLine}\n`)).toBe( + `

q
${pipeLine}

`, + ); + }); +}); diff --git a/packages/core/src/api/parsers/markdown/markdownToHtml.ts b/packages/core/src/api/parsers/markdown/markdownToHtml.ts index 961192df49..3ce813024f 100644 --- a/packages/core/src/api/parsers/markdown/markdownToHtml.ts +++ b/packages/core/src/api/parsers/markdown/markdownToHtml.ts @@ -800,6 +800,11 @@ function isHtmlBlockStart(line: string): boolean { return HTML_BLOCK_TAGS.has(m[1].toLowerCase()); } +// A table row that starts and ends with a pipe. Do not write `(.+\|)+` here: +// it matches the same lines, but for a line without the last pipe its time +// grows exponentially with the number of pipes. +const TABLE_ROW_RE = /^\s*\|.+\|\s*$/; + // ─── Block-Level Tokenizer ────────────────────────────────────────────────── function tokenize(markdown: string): Token[] { @@ -945,7 +950,7 @@ function tokenize(markdown: string): Token[] { if (/^\s*([-*+]|\d+[.)])\s+/.test(cur)) { break; } // list item - if (/^\s*\|(.+\|)+\s*$/.test(cur)) { + if (TABLE_ROW_RE.test(cur)) { break; } // table quoteLines.push(cur); @@ -1110,7 +1115,7 @@ function tokenize(markdown: string): Token[] { if (/^\s*([-*+]|\d+[.)])\s+/.test(nextLine)) { break; } - if (/^\s*\|(.+\|)+\s*$/.test(nextLine)) { + if (TABLE_ROW_RE.test(nextLine)) { break; } if (isHtmlBlockStart(nextLine)) {