Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions packages/core/src/api/parsers/markdown/detectMarkdown.test.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { runInNewContext } from "node:vm";
import { describe, expect, it } from "vite-plus/test";
import { isMarkdown } from "./detectMarkdown.js";

Expand Down Expand Up @@ -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", () => {
Expand Down
7 changes: 5 additions & 2 deletions packages/core/src/api/parsers/markdown/detectMarkdown.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
37 changes: 37 additions & 0 deletions packages/core/src/api/parsers/markdown/markdownToHtml.test.ts
Original file line number Diff line number Diff line change
@@ -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 `<br>`: 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("<br>\n", "<br>");
}

it("keeps a line with many pipes and no last pipe in the paragraph", () => {
expect(markdownToHtmlWithTimeLimit(`x\n${pipeLine}\n`)).toBe(
`<p>x<br>${pipeLine}</p>`,
);
});

it("keeps a line with many pipes and no last pipe in the quote", () => {
expect(markdownToHtmlWithTimeLimit(`> q\n${pipeLine}\n`)).toBe(
`<blockquote><p>q<br>${pipeLine}</p></blockquote>`,
);
});
});
9 changes: 7 additions & 2 deletions packages/core/src/api/parsers/markdown/markdownToHtml.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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[] {
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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)) {
Expand Down
Loading