From 1e7608c478be272d0c28c0d1012c908a0bb1e269 Mon Sep 17 00:00:00 2001 From: Dallin Romney Date: Mon, 2 Dec 2024 02:21:48 -0500 Subject: [PATCH 1/2] double dollar sign latex fix --- .../markdown/StyledMarkdownPreview.tsx | 6 +- .../markdown/utils/fixDoubleDollarLatex.ts | 13 ++ .../utils/fixDoubleDollarLatext.test.ts | 124 ++++++++++++++++++ 3 files changed, 142 insertions(+), 1 deletion(-) create mode 100644 gui/src/components/markdown/utils/fixDoubleDollarLatex.ts create mode 100644 gui/src/components/markdown/utils/fixDoubleDollarLatext.test.ts diff --git a/gui/src/components/markdown/StyledMarkdownPreview.tsx b/gui/src/components/markdown/StyledMarkdownPreview.tsx index 70be76bdc..510f44016 100644 --- a/gui/src/components/markdown/StyledMarkdownPreview.tsx +++ b/gui/src/components/markdown/StyledMarkdownPreview.tsx @@ -25,6 +25,7 @@ import SymbolLink from "./SymbolLink"; import { SyntaxHighlightedPre } from "./SyntaxHighlightedPre"; import { patchNestedMarkdown } from "./utils/patchNestedMarkdown"; import { useAppSelector } from "../../redux/hooks"; +import { fixDoubleDollarNewLineLatex } from "./utils/fixDoubleDollarLatex"; const StyledMarkdown = styled.div<{ fontSize?: number; @@ -286,7 +287,10 @@ const StyledMarkdownPreview = memo(function StyledMarkdownPreview( }); useEffect(() => { - setMarkdownSource(patchNestedMarkdown(props.source ?? "")); + setMarkdownSource( + // some patches to source markdown are applied here: + fixDoubleDollarNewLineLatex(patchNestedMarkdown(props.source ?? "")), + ); }, [props.source]); return ( diff --git a/gui/src/components/markdown/utils/fixDoubleDollarLatex.ts b/gui/src/components/markdown/utils/fixDoubleDollarLatex.ts new file mode 100644 index 000000000..53c110553 --- /dev/null +++ b/gui/src/components/markdown/utils/fixDoubleDollarLatex.ts @@ -0,0 +1,13 @@ +/* +Currently double dollar latext with new lines causes truncation error +*/ +export function fixDoubleDollarNewLineLatex(text: string): string { + // Match LaTeX blocks delimited by $$ with newlines + const pattern = /\$\$(?:\r?\n)(.*?)(?:\r?\n)\$\$/gms; + + // Replace each matched block with $$ without newline + const replacedText = text.replace(pattern, "$$$$$1$$$$"); + + console.log("RESULT", replacedText); + return replacedText; +} diff --git a/gui/src/components/markdown/utils/fixDoubleDollarLatext.test.ts b/gui/src/components/markdown/utils/fixDoubleDollarLatext.test.ts new file mode 100644 index 000000000..c3c932657 --- /dev/null +++ b/gui/src/components/markdown/utils/fixDoubleDollarLatext.test.ts @@ -0,0 +1,124 @@ +import { fixDoubleDollarNewLineLatex } from "./fixDoubleDollarLatex"; // Adjust the import based on your file structure + +/* + These tests are generated by claude sonnet, audit if failing. +*/ +describe("fixDoubleDollarNewLineLatex", () => { + it("replaces newlined $$ delimiters for a single LaTeX block", () => { + const input = ` + Here is some text. + + $$ + f(x) = ax^2 + bx + c + $$ + + End of text. + `; + const expectedOutput = ` + Here is some text. + + $$f(x) = ax^2 + bx + c$$ + + End of text. + `; + expect(fixDoubleDollarNewLineLatex(input)).toBe(expectedOutput); + }); + + it("does not modify text without LaTeX blocks", () => { + const input = ` + This is a simple text block. + `; + const expectedOutput = ` + This is a simple text block. + `; + expect(fixDoubleDollarNewLineLatex(input)).toBe(expectedOutput); + }); + + it("does not replace single $ delimiters", () => { + const input = ` + Price is $100. + `; + const expectedOutput = ` + Price is $100. + `; + expect(fixDoubleDollarNewLineLatex(input)).toBe(expectedOutput); + }); + + it("processes multiple LaTeX blocks separately", () => { + const input = ` + $$\nE = mc^2\n$$ + + Some text. + + $$\na^2 + b^2 = c^2\n$$ + `; + const expectedOutput = ` + $$E = mc^2$$ + + Some text. + + $$a^2 + b^2 = c^2$$ + `; + expect(fixDoubleDollarNewLineLatex(input)).toBe(expectedOutput); + }); + + it("leaves inline $$ delimiters untouched", () => { + const input = ` + You earned $$1,000 today. + `; + const expectedOutput = ` + You earned $$1,000 today. + `; + expect(fixDoubleDollarNewLineLatex(input)).toBe(expectedOutput); + }); + + it("handles no LaTeX correctly", () => { + const input = `Just a regular string, nothing fancy.`; + const expectedOutput = `Just a regular string, nothing fancy.`; + expect(fixDoubleDollarNewLineLatex(input)).toBe(expectedOutput); + }); + + it("ignores malformed LaTeX that lacks newlines", () => { + const input = ` + $$f(x) = ax^2 + bx + c$$ + `; + const expectedOutput = ` + $$f(x) = ax^2 + bx + c$$ + `; + expect(fixDoubleDollarNewLineLatex(input)).toBe(expectedOutput); + }); + + it("keeps text spacing unchanged except for newline removal", () => { + const input = ` + Start + + $$\nA = \pi r^2\n$$ + + End + `; + const expectedOutput = ` + Start + + $$A = \pi r^2$$ + + End + `; + expect(fixDoubleDollarNewLineLatex(input)).toBe(expectedOutput); + }); + + it("works with a completely empty string", () => { + const input = ""; + const expectedOutput = ""; + expect(fixDoubleDollarNewLineLatex(input)).toBe(expectedOutput); + }); + + it("ignores non-matching but similar text", () => { + const input = ` + Look at this: $$$Profit$$$ + `; + const expectedOutput = ` + Look at this: $$$Profit$$$ + `; + expect(fixDoubleDollarNewLineLatex(input)).toBe(expectedOutput); + }); +}); From 47f3d1e9bcc38c82adb77631bd86821c288b2a37 Mon Sep 17 00:00:00 2001 From: Dallin Romney Date: Mon, 2 Dec 2024 19:26:55 -0500 Subject: [PATCH 2/2] latex truncation fix cleanup --- .../markdown/utils/fixDoubleDollarLatex.ts | 2 +- .../utils/fixDoubleDollarLatext.test.ts | 124 ------------------ 2 files changed, 1 insertion(+), 125 deletions(-) delete mode 100644 gui/src/components/markdown/utils/fixDoubleDollarLatext.test.ts diff --git a/gui/src/components/markdown/utils/fixDoubleDollarLatex.ts b/gui/src/components/markdown/utils/fixDoubleDollarLatex.ts index 53c110553..98b09f596 100644 --- a/gui/src/components/markdown/utils/fixDoubleDollarLatex.ts +++ b/gui/src/components/markdown/utils/fixDoubleDollarLatex.ts @@ -1,5 +1,6 @@ /* Currently double dollar latext with new lines causes truncation error +So this pseudo-remark plugin removes internal newlines with double dollar signs */ export function fixDoubleDollarNewLineLatex(text: string): string { // Match LaTeX blocks delimited by $$ with newlines @@ -8,6 +9,5 @@ export function fixDoubleDollarNewLineLatex(text: string): string { // Replace each matched block with $$ without newline const replacedText = text.replace(pattern, "$$$$$1$$$$"); - console.log("RESULT", replacedText); return replacedText; } diff --git a/gui/src/components/markdown/utils/fixDoubleDollarLatext.test.ts b/gui/src/components/markdown/utils/fixDoubleDollarLatext.test.ts deleted file mode 100644 index c3c932657..000000000 --- a/gui/src/components/markdown/utils/fixDoubleDollarLatext.test.ts +++ /dev/null @@ -1,124 +0,0 @@ -import { fixDoubleDollarNewLineLatex } from "./fixDoubleDollarLatex"; // Adjust the import based on your file structure - -/* - These tests are generated by claude sonnet, audit if failing. -*/ -describe("fixDoubleDollarNewLineLatex", () => { - it("replaces newlined $$ delimiters for a single LaTeX block", () => { - const input = ` - Here is some text. - - $$ - f(x) = ax^2 + bx + c - $$ - - End of text. - `; - const expectedOutput = ` - Here is some text. - - $$f(x) = ax^2 + bx + c$$ - - End of text. - `; - expect(fixDoubleDollarNewLineLatex(input)).toBe(expectedOutput); - }); - - it("does not modify text without LaTeX blocks", () => { - const input = ` - This is a simple text block. - `; - const expectedOutput = ` - This is a simple text block. - `; - expect(fixDoubleDollarNewLineLatex(input)).toBe(expectedOutput); - }); - - it("does not replace single $ delimiters", () => { - const input = ` - Price is $100. - `; - const expectedOutput = ` - Price is $100. - `; - expect(fixDoubleDollarNewLineLatex(input)).toBe(expectedOutput); - }); - - it("processes multiple LaTeX blocks separately", () => { - const input = ` - $$\nE = mc^2\n$$ - - Some text. - - $$\na^2 + b^2 = c^2\n$$ - `; - const expectedOutput = ` - $$E = mc^2$$ - - Some text. - - $$a^2 + b^2 = c^2$$ - `; - expect(fixDoubleDollarNewLineLatex(input)).toBe(expectedOutput); - }); - - it("leaves inline $$ delimiters untouched", () => { - const input = ` - You earned $$1,000 today. - `; - const expectedOutput = ` - You earned $$1,000 today. - `; - expect(fixDoubleDollarNewLineLatex(input)).toBe(expectedOutput); - }); - - it("handles no LaTeX correctly", () => { - const input = `Just a regular string, nothing fancy.`; - const expectedOutput = `Just a regular string, nothing fancy.`; - expect(fixDoubleDollarNewLineLatex(input)).toBe(expectedOutput); - }); - - it("ignores malformed LaTeX that lacks newlines", () => { - const input = ` - $$f(x) = ax^2 + bx + c$$ - `; - const expectedOutput = ` - $$f(x) = ax^2 + bx + c$$ - `; - expect(fixDoubleDollarNewLineLatex(input)).toBe(expectedOutput); - }); - - it("keeps text spacing unchanged except for newline removal", () => { - const input = ` - Start - - $$\nA = \pi r^2\n$$ - - End - `; - const expectedOutput = ` - Start - - $$A = \pi r^2$$ - - End - `; - expect(fixDoubleDollarNewLineLatex(input)).toBe(expectedOutput); - }); - - it("works with a completely empty string", () => { - const input = ""; - const expectedOutput = ""; - expect(fixDoubleDollarNewLineLatex(input)).toBe(expectedOutput); - }); - - it("ignores non-matching but similar text", () => { - const input = ` - Look at this: $$$Profit$$$ - `; - const expectedOutput = ` - Look at this: $$$Profit$$$ - `; - expect(fixDoubleDollarNewLineLatex(input)).toBe(expectedOutput); - }); -});