AutoGPT is the vision of accessible AI for everyone, to use and to build on. Our mission is to provide the tools, so that you can focus on what matters.
fix(frontend): leave dollar signs in indented code blocks unescaped (#14808)
Reinier van der Leereaa12317d74f
diff --git a/autogpt_platform/frontend/src/lib/markdown-math.test.ts b/autogpt_platform/frontend/src/lib/markdown-math.test.tsindex de2b115aa6..2ed0661eed 100644--- a/autogpt_platform/frontend/src/lib/markdown-math.test.ts+++ b/autogpt_platform/frontend/src/lib/markdown-math.test.ts@@ -37,6 +37,99 @@ describe("escapeCurrencyAmounts", () => {);});+ it.each([+ [+ "after a blank line",+ "Run it:\n\n echo $5 and $10\n\nprose $5 and $10",+ ],+ ["at the start of the text", " echo $5 and $10\n\nprose $5 and $10"],+ ["indented with a tab", "\techo $5 and $10\n\nprose $5 and $10"],+ [+ "nested in a list item",+ "- item\n\n echo $5 and $10\n\nprose $5 and $10",+ ],+ ])("skips indented code %s", (_, markdown) => {+ const [code] = markdown.split("\n\nprose");+ expect(escapeCurrencyAmounts(markdown)).toBe(+ `${code}\n\nprose \\$5 and \\$10`,+ );+ });++ it.each([+ ["a thematic break", "---\n echo $5 and $10"],+ ["a setext heading", "Title\n===\n echo $5 and $10"],+ ])("skips indented code right after %s", (_, markdown) => {+ expect(escapeCurrencyAmounts(markdown)).toBe(markdown);+ });++ it.each([+ ["a thematic break", "- item\n ***\n echo $5 and $10"],+ ["a tab-indented thematic break", "-\titem\n\t***\n\t echo $5 and $10"],+ [+ "a setext heading",+ "- item\n para\n ---\n echo $5 and $10",+ ],+ ["an ATX heading", "- item\n # Head\n echo $5 and $10"],+ ["a fenced block", "- item\n\n ```\n echo $5 and $10\n ```"],+ ])("reads %s indented inside a list item as a block", (_, markdown) => {+ expect(escapeCurrencyAmounts(markdown)).toBe(markdown);+ });++ it.each([+ ["```\n``` not a closer\necho $5 and $10\n```"],+ ["~~~\n~~~ x\necho $5 and $10\n~~~"],+ ])(+ "keeps a fence open past a fence line with text after it: %j",+ (markdown) => {+ expect(escapeCurrencyAmounts(markdown)).toBe(markdown);+ },+ );++ it("does not open a backtick fence whose info string has a backtick", () => {+ expect(escapeCurrencyAmounts("```a`b\n$5 and $10")).toBe(+ "```a`b\n\\$5 and \\$10",+ );+ });++ it("does not open a fence indented by a tab, which is indented code", () => {+ expect(escapeCurrencyAmounts("\t```\nprose $5 and $10")).toBe(+ "\t```\nprose \\$5 and \\$10",+ );+ });++ it("measures a tab after a list marker in columns", () => {+ expect(+ escapeCurrencyAmounts("- \titem\n\n continued $5 and $10"),+ ).toBe("- \titem\n\n continued \\$5 and \\$10");+ });++ it.each([+ ["at the top level", " - echo $5 and $10"],+ ["inside a list item", "- item\n\n - echo $5 and $10"],+ ])("skips indented code that starts like a list marker %s", (_, markdown) => {+ expect(escapeCurrencyAmounts(markdown)).toBe(markdown);+ });++ it.each([+ ["- a\n - b\n\n back in a\n\n more $5 and $10"],+ ["1. a\n - b\n\n back in a\n\n more $5 and $10"],+ ["- a\n - b\n - c\n\n back in b\n\n more $5 and $10"],+ ])("returns to the parent list item after a nested one: %j", (markdown) => {+ expect(escapeCurrencyAmounts(markdown)).toBe(+ markdown.replace("$5 and $10", "\\$5 and \\$10"),+ );+ });++ it("escapes indented lines that are not code", () => {+ expect(+ escapeCurrencyAmounts(+ "- item\n\n more $5 and $10\n\npara\n lazy $5 and $10",+ ),+ ).toBe(+ "- item\n\n more \\$5 and \\$10\n\npara\n lazy \\$5 and \\$10",+ );+ });+it("is idempotent, so an already-escaped price is left as one", () => {const once = escapeCurrencyAmounts("It costs $5.");expect(once).toBe("It costs \\$5.");diff --git a/autogpt_platform/frontend/src/lib/markdown-math.ts b/autogpt_platform/frontend/src/lib/markdown-math.tsindex a7d49f234d..beec002b31 100644--- a/autogpt_platform/frontend/src/lib/markdown-math.ts+++ b/autogpt_platform/frontend/src/lib/markdown-math.ts@@ -1,5 +1,11 @@-const FENCE_RE = /^\s{0,3}(`{3,}|~{3,})/;+// A backtick fence's info string may not contain a backtick; a closer carries none.+const OPENING_FENCE_RE = /^(?:(`{3,})[^`]*|(~{3,}).*)$/;+const CLOSING_FENCE_RE = /^(`{3,}|~{3,})[ \t]*$/;const LATEX_SYNTAX_RE = /[\\^_{}]/;+const LIST_MARKER_RE = /^[ \t]*(?:[-*+]|\d{1,9}[.)])[ \t]+/;+const HEADING_RE = /^#{1,6}(?:\s|$)/;+const THEMATIC_BREAK_RE = /^([-*_])(?:[ \t]*\1){2,}[ \t]*$/;+const SETEXT_UNDERLINE_RE = /^(?:=+|-+)[ \t]*$/;// With single-dollar math on, remark-math reads "$5 and $10" as one formula. A "$"// before a digit is therefore currency unless the span up to the next "$" on that@@ -7,13 +13,23 @@ const LATEX_SYNTAX_RE = /[\\^_{}]/;// them literal and every other delimiter to remark-math.export function escapeCurrencyAmounts(markdown: string): string {let openFence: string | null = null;+ let codeIndent: number | null = null;+ // Content column of each open list item, innermost last.+ const listIndents: number[] = [];+ let inParagraph = false;+ const listIndent = () => listIndents.at(-1) ?? 0;return markdown.split("\n").map((line) => {- const fence = FENCE_RE.exec(line)?.[1];+ const indent = indentWidth(line);+ const content = line.trimStart();if (openFence) {+ const fence =+ indent < listIndent() + 4+ ? CLOSING_FENCE_RE.exec(content)?.[1]+ : undefined;if (fence &&fence[0] === openFence[0] &&@@ -24,11 +40,52 @@ export function escapeCurrencyAmounts(markdown: string): string {return line;}+ if (!line.trim()) {+ inParagraph = false;+ return line;+ }++ if (codeIndent !== null && indent >= codeIndent) return line;+ codeIndent = null;++ const marker = LIST_MARKER_RE.exec(line);+ const startsItem = marker !== null && indent < listIndent() + 4;+ // A lazy paragraph line stays in its list item; anything else closes the+ // items it is not indented into.+ if (startsItem || !inParagraph) {+ while (listIndents.length && listIndent() > indent) listIndents.pop();+ }++ // Block markers may be indented up to three columns past the list item's content.+ const startsBlock = indent < listIndent() + 4;+ const opening = startsBlock ? OPENING_FENCE_RE.exec(content) : null;+ const fence = opening ? (opening[1] ?? opening[2]) : undefined;+if (fence) {openFence = fence;+ inParagraph = false;+ return line;+ }++ if (+ startsBlock &&+ (THEMATIC_BREAK_RE.test(content) ||+ (inParagraph && SETEXT_UNDERLINE_RE.test(content)))+ ) {+ inParagraph = false;+ return line;+ }++ if (startsItem) listIndents.push(columns(marker[0]));++ // Indented code cannot interrupt a paragraph, and CommonMark renders a+ // backslash escape inside it literally.+ if (!inParagraph && indent >= listIndent() + 4) {+ codeIndent = listIndent() + 4;return line;}+ inParagraph = !(startsBlock && HEADING_RE.test(content));return escapeCurrencyAmountsInLine(line);}).join("\n");@@ -82,3 +139,14 @@ function isCurrencyAmount(line: string, index: number): boolean {const close = rest.search(/(?<!\\)\$/);return close === -1 || !LATEX_SYNTAX_RE.test(rest.slice(0, close));}++function indentWidth(line: string): number {+ return columns(line.slice(0, line.length - line.trimStart().length));+}++// A tab advances to the next multiple of four columns.+function columns(text: string): number {+ let width = 0;+ for (const char of text) width += char === "\t" ? 4 - (width % 4) : 1;+ return width;+}