perf(markdown): skip impossible table parses (#128408)

Co-authored-by: Amp <amp@ampcode.com>
This commit is contained in:
Peter Steinberger
2026-08-23 16:53:58 -07:00
committed by GitHub
parent 2113967a06
commit f33a88ba5d
2 changed files with 21 additions and 2 deletions
+20 -1
View File
@@ -1,11 +1,30 @@
// Markdown Core tests cover tables behavior.
import { describe, expect, it } from "vitest";
import { beforeEach, describe, expect, it, vi } from "vitest";
import { convertMarkdownTables } from "./tables.js";
const markdownToIRWithMetaMock = vi.hoisted(() => vi.fn());
vi.mock("./ir.js", async (importOriginal) => {
const actual = await importOriginal<typeof import("./ir.js")>();
markdownToIRWithMetaMock.mockImplementation(actual.markdownToIRWithMeta);
return { ...actual, markdownToIRWithMeta: markdownToIRWithMetaMock };
});
describe("convertMarkdownTables", () => {
beforeEach(() => {
markdownToIRWithMetaMock.mockClear();
});
it("falls back to code rendering for block mode", () => {
const rendered = convertMarkdownTables("| A | B |\n|---|---|\n| 1 | 2 |", "block");
expect(rendered).toBe("```\n| A | B |\n| --- | --- |\n| 1 | 2 |\n```");
});
it("does not parse ordinary text that cannot contain a table", () => {
const text = "Ordinary iMessage reply with **bold** and _emphasis_.";
expect(convertMarkdownTables(text, "code")).toBe(text);
expect(markdownToIRWithMetaMock).not.toHaveBeenCalled();
});
});
+1 -1
View File
@@ -13,7 +13,7 @@ const MARKDOWN_STYLE_MARKERS = {
/** Converts markdown tables into the configured plaintext/code rendering mode. */
export function convertMarkdownTables(markdown: string, mode: MarkdownTableMode): string {
if (!markdown || mode === "off") {
if (!markdown || mode === "off" || !markdown.includes("|")) {
return markdown;
}
const effectiveMode = mode === "block" ? "code" : mode;