From f33a88ba5d8bb7e18b8900fb53b3d03a74f89808 Mon Sep 17 00:00:00 2001 From: Peter Steinberger Date: Sun, 23 Aug 2026 16:53:58 -0700 Subject: [PATCH] perf(markdown): skip impossible table parses (#128408) Co-authored-by: Amp --- packages/markdown-core/src/tables.test.ts | 21 ++++++++++++++++++++- packages/markdown-core/src/tables.ts | 2 +- 2 files changed, 21 insertions(+), 2 deletions(-) diff --git a/packages/markdown-core/src/tables.test.ts b/packages/markdown-core/src/tables.test.ts index 14a66013509b..a3f94e4e441d 100644 --- a/packages/markdown-core/src/tables.test.ts +++ b/packages/markdown-core/src/tables.test.ts @@ -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(); + 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(); + }); }); diff --git a/packages/markdown-core/src/tables.ts b/packages/markdown-core/src/tables.ts index 2cb60f6b7200..d939f66aa8e7 100644 --- a/packages/markdown-core/src/tables.ts +++ b/packages/markdown-core/src/tables.ts @@ -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;