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}
`, + ); + }); +}); 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)) {q
${pipeLine}