Skip to content

Commit d84a480

Browse files
Yidadaalovely
authored and
lovely
committed
fix: ChatGPTNextWeb#2841 dollar sign conflict with latex math
1 parent eaf0881 commit d84a480

File tree

2 files changed

+24
-3
lines changed

2 files changed

+24
-3
lines changed

app/components/markdown.tsx

+24-2
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import RemarkBreaks from "remark-breaks";
55
import RehypeKatex from "rehype-katex";
66
import RemarkGfm from "remark-gfm";
77
import RehypeHighlight from "rehype-highlight";
8-
import { useRef, useState, RefObject, useEffect } from "react";
8+
import { useRef, useState, RefObject, useEffect, useMemo } from "react";
99
import { copyToClipboard } from "../utils";
1010
import mermaid from "mermaid";
1111

@@ -99,7 +99,29 @@ export function PreCode(props: { children: any }) {
9999
);
100100
}
101101

102+
function escapeDollarNumber(text: string) {
103+
let escapedText = "";
104+
105+
for (let i = 0; i < text.length; i += 1) {
106+
let char = text[i];
107+
const nextChar = text[i + 1] || " ";
108+
109+
if (char === "$" && nextChar >= "0" && nextChar <= "9") {
110+
char = "\\$";
111+
}
112+
113+
escapedText += char;
114+
}
115+
116+
return escapedText;
117+
}
118+
102119
function _MarkDownContent(props: { content: string }) {
120+
const escapedContent = useMemo(
121+
() => escapeDollarNumber(props.content),
122+
[props.content],
123+
);
124+
103125
return (
104126
<ReactMarkdown
105127
remarkPlugins={[RemarkMath, RemarkGfm, RemarkBreaks]}
@@ -124,7 +146,7 @@ function _MarkDownContent(props: { content: string }) {
124146
},
125147
}}
126148
>
127-
{props.content}
149+
{escapedContent}
128150
</ReactMarkdown>
129151
);
130152
}

app/constant.ts

-1
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,6 @@ You are ChatGPT, a large language model trained by OpenAI.
8484
Knowledge cutoff: {{cutoff}}
8585
Current model: {{model}}
8686
Current time: {{time}}
87-
8887
Latex inline: $x^2$
8988
Latex block: $$e=mc^2$$
9089
`;

0 commit comments

Comments
 (0)