mirror of
https://github.com/openclaw/openclaw.git
synced 2026-08-27 12:56:01 -06:00
perf(markdown): skip impossible table parses (#128408)
Co-authored-by: Amp <amp@ampcode.com>
This commit is contained in:
committed by
GitHub
parent
2113967a06
commit
f33a88ba5d
@@ -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();
|
||||
});
|
||||
});
|
||||
|
||||
@@ -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;
|
||||
|
||||
Reference in New Issue
Block a user