From a0cd939bfd560621b854b7533fa0b28a329dfa75 Mon Sep 17 00:00:00 2001 From: Yidadaa Date: Sun, 12 Nov 2023 19:45:58 +0800 Subject: [PATCH] fix: #2841 dollar sign conflict with latex math --- app/components/markdown.tsx | 26 ++++++++++++++++++++++++-- app/constant.ts | 1 - 2 files changed, 24 insertions(+), 3 deletions(-) diff --git a/app/components/markdown.tsx b/app/components/markdown.tsx index b4cae9a21e1..f3a916cc535 100644 --- a/app/components/markdown.tsx +++ b/app/components/markdown.tsx @@ -5,7 +5,7 @@ import RemarkBreaks from "remark-breaks"; import RehypeKatex from "rehype-katex"; import RemarkGfm from "remark-gfm"; import RehypeHighlight from "rehype-highlight"; -import { useRef, useState, RefObject, useEffect } from "react"; +import { useRef, useState, RefObject, useEffect, useMemo } from "react"; import { copyToClipboard } from "../utils"; import mermaid from "mermaid"; @@ -99,7 +99,29 @@ export function PreCode(props: { children: any }) { ); } +function escapeDollarNumber(text: string) { + let escapedText = ""; + + for (let i = 0; i < text.length; i += 1) { + let char = text[i]; + const nextChar = text[i + 1] || " "; + + if (char === "$" && nextChar >= "0" && nextChar <= "9") { + char = "\\$"; + } + + escapedText += char; + } + + return escapedText; +} + function _MarkDownContent(props: { content: string }) { + const escapedContent = useMemo( + () => escapeDollarNumber(props.content), + [props.content], + ); + return ( - {props.content} + {escapedContent} ); } diff --git a/app/constant.ts b/app/constant.ts index 779c6f7e76f..69d5c511f0a 100644 --- a/app/constant.ts +++ b/app/constant.ts @@ -84,7 +84,6 @@ You are ChatGPT, a large language model trained by OpenAI. Knowledge cutoff: {{cutoff}} Current model: {{model}} Current time: {{time}} - Latex inline: $x^2$ Latex block: $$e=mc^2$$ `;