diff --git a/server/commands/documentImporter.test.ts b/server/commands/documentImporter.test.ts index f35e23cdf..c7a85cf3b 100644 --- a/server/commands/documentImporter.test.ts +++ b/server/commands/documentImporter.test.ts @@ -213,4 +213,76 @@ describe("documentImporter", () => { expect(error).toEqual("File type executable/zip not supported"); }); + + it("should escape dollar signs in HTML input", async () => { + const user = await buildUser(); + const fileName = "test.html"; + const content = ` + + + + Test + + +

$100

+ + + `; + const response = await documentImporter({ + user, + mimeType: "text/html", + fileName, + content, + ip, + }); + expect(response.text).toEqual("\\$100"); + }); + + it("should not escape dollar signs in inline code in HTML input", async () => { + const user = await buildUser(); + const fileName = "test.html"; + const content = ` + + + + Test + + + echo $foo + + + `; + const response = await documentImporter({ + user, + mimeType: "text/html", + fileName, + content, + ip, + }); + expect(response.text).toEqual("`echo $foo`"); + }); + + it("should not escape dollar signs in code blocks in HTML input", async () => { + const user = await buildUser(); + const fileName = "test.html"; + const content = ` + + + + Test + + +
echo $foo
+ + + `; + const response = await documentImporter({ + user, + mimeType: "text/html", + fileName, + content, + ip, + }); + expect(response.text).toEqual("```\necho $foo\n```"); + }); }); diff --git a/server/commands/documentImporter.ts b/server/commands/documentImporter.ts index f014280be..9b5db20df 100644 --- a/server/commands/documentImporter.ts +++ b/server/commands/documentImporter.ts @@ -63,10 +63,6 @@ async function documentImporter({ // to match our hardbreak parser. text = text.trim().replace(/
/gi, "\\n"); - // Escape any dollar signs in the text to prevent them being interpreted as - // math blocks - text = text.replace(/\$/g, "\\$"); - // Remove any closed and immediately reopened formatting marks text = text.replace(/\*\*\*\*/gi, "").replace(/____/gi, ""); diff --git a/server/utils/turndown/index.ts b/server/utils/turndown/index.ts index 9e8e0ab38..a3ca5a862 100644 --- a/server/utils/turndown/index.ts +++ b/server/utils/turndown/index.ts @@ -57,6 +57,7 @@ const escapes: [RegExp, string][] = [ [/^>/g, "\\>"], [/_/g, "\\_"], [/^(\d+)\. /g, "$1\\. "], + [/\$/g, "\\$"], ]; /**