From 3a98f73d71c7906e9ea83864a2d3eeef967ae6bb Mon Sep 17 00:00:00 2001 From: H0llyW00dzZ <priv8@btz.pm> Date: Thu, 23 Nov 2023 14:32:58 +0700 Subject: [PATCH 1/2] Refactor & Fix UI/UX Page [Markdown] [LaTex] [+] refactor(markdown.tsx): remove unused escapeDollarMathNumber function [+] fix(markdown.tsx): fix escaping of single dollar sign followed by a number --- app/components/markdown.tsx | 44 ++++++++++--------------------------- 1 file changed, 11 insertions(+), 33 deletions(-) diff --git a/app/components/markdown.tsx b/app/components/markdown.tsx index 916ec2a43ad..4ae385f52f1 100644 --- a/app/components/markdown.tsx +++ b/app/components/markdown.tsx @@ -107,44 +107,25 @@ function escapeDollarNumber(text: string) { let char = text[i]; const nextChar = text[i + 1] || " "; - if (char === "$") { - isInMathExpression = !isInMathExpression; - } - + // Check for double dollar signs and preserve them without changing. if (char === "$" && nextChar === "$") { - char = "$$"; // Preserve the double dollar sign - i += 1; // Skip the next dollar sign since we have already included it - } else if (char === "$" && nextChar >= "0" && nextChar <= "9" && !isInMathExpression) { - char = "$" + nextChar; - i += 1; // Skip the next character since we have already included it + escapedText += "$$"; // Add both dollar signs to the escaped text. + i += 1; // Skip the next dollar sign since we have already processed it. + continue; // Continue to the next character. } - escapedText += char; - } - - return escapedText; -} -// used to be $ any (will refactor this later) -function escapeDollarMathNumber(text: string) { - let escapedText = ""; - let isInMathExpression = false; - - for (let i = 0; i < text.length; i += 1) { - let char = text[i]; - const nextChar = text[i + 1] || " "; + // If it's a single dollar sign followed by a number, escape it. + if (char === "$" && !isInMathExpression && nextChar >= "0" && nextChar <= "9") { + escapedText += "$" + nextChar; + i += 1; // Skip the next character since we have already included it. + continue; // Continue to the next character. + } + // Toggle the math expression state with a single dollar sign. if (char === "$") { isInMathExpression = !isInMathExpression; } - if (char === "$" && nextChar === "$") { - char = "$$"; // Preserve the double dollar sign - i += 1; // Skip the next dollar sign since we have already included it - } else if (char === "$" && nextChar >= "0" && nextChar <= "9" && !isInMathExpression) { - char = "$" + nextChar; - i += 1; // Skip the next character since we have already included it - } - escapedText += char; } @@ -154,9 +135,6 @@ function escapeDollarMathNumber(text: string) { function _MarkDownContent(props: { content: string }) { const escapedContent = useMemo(() => { let processedContent = props.content; - if (processedContent.includes("$")) { - processedContent = escapeDollarMathNumber(processedContent); - } processedContent = escapeDollarNumber(processedContent); return processedContent; }, [props.content]); From 97297281a3eab3276797392deb535c173c8b60df Mon Sep 17 00:00:00 2001 From: H0llyW00dzZ <priv8@btz.pm> Date: Thu, 23 Nov 2023 14:52:20 +0700 Subject: [PATCH 2/2] Fix UI/UX Page [Markdown] [LaTex] [+] fix(markdown.tsx): escape single dollar sign followed by a number in escapeDollarNumber function [+] chore(markdown.tsx): add comment to explain the purpose of isInMathExpression variable --- app/components/markdown.tsx | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/app/components/markdown.tsx b/app/components/markdown.tsx index 4ae385f52f1..247ac5ffb35 100644 --- a/app/components/markdown.tsx +++ b/app/components/markdown.tsx @@ -116,7 +116,7 @@ function escapeDollarNumber(text: string) { // If it's a single dollar sign followed by a number, escape it. if (char === "$" && !isInMathExpression && nextChar >= "0" && nextChar <= "9") { - escapedText += "$" + nextChar; + escapedText += "\\$" + nextChar; i += 1; // Skip the next character since we have already included it. continue; // Continue to the next character. } @@ -126,6 +126,7 @@ function escapeDollarNumber(text: string) { isInMathExpression = !isInMathExpression; } + // Add the current character to the escaped text. escapedText += char; }