fix: HTML import escapes dollar signs in code (#6638) (#6645)

This commit is contained in:
panos--
2024-03-06 14:00:50 +01:00
committed by GitHub
parent 98eba29cdd
commit dce4fd6adc
3 changed files with 73 additions and 4 deletions

View File

@@ -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 = `
<!DOCTYPE html>
<html>
<head>
<title>Test</title>
</head>
<body>
<p>$100</p>
</body>
</html>
`;
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 = `
<!DOCTYPE html>
<html>
<head>
<title>Test</title>
</head>
<body>
<code>echo $foo</code>
</body>
</html>
`;
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 = `
<!DOCTYPE html>
<html>
<head>
<title>Test</title>
</head>
<body>
<pre><code>echo $foo</code></pre>
</body>
</html>
`;
const response = await documentImporter({
user,
mimeType: "text/html",
fileName,
content,
ip,
});
expect(response.text).toEqual("```\necho $foo\n```");
});
});

View File

@@ -63,10 +63,6 @@ async function documentImporter({
// to match our hardbreak parser.
text = text.trim().replace(/<br>/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, "");

View File

@@ -57,6 +57,7 @@ const escapes: [RegExp, string][] = [
[/^>/g, "\\>"],
[/_/g, "\\_"],
[/^(\d+)\. /g, "$1\\. "],
[/\$/g, "\\$"],
];
/**