fix: #2841 dollar sign conflict with latex math

This commit is contained in:
Yidadaa 2023-11-12 19:45:58 +08:00
parent b52e237044
commit a0cd939bfd
2 changed files with 24 additions and 3 deletions

View File

@ -5,7 +5,7 @@ import RemarkBreaks from "remark-breaks";
import RehypeKatex from "rehype-katex"; import RehypeKatex from "rehype-katex";
import RemarkGfm from "remark-gfm"; import RemarkGfm from "remark-gfm";
import RehypeHighlight from "rehype-highlight"; 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 { copyToClipboard } from "../utils";
import mermaid from "mermaid"; 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 }) { function _MarkDownContent(props: { content: string }) {
const escapedContent = useMemo(
() => escapeDollarNumber(props.content),
[props.content],
);
return ( return (
<ReactMarkdown <ReactMarkdown
remarkPlugins={[RemarkMath, RemarkGfm, RemarkBreaks]} remarkPlugins={[RemarkMath, RemarkGfm, RemarkBreaks]}
@ -124,7 +146,7 @@ function _MarkDownContent(props: { content: string }) {
}, },
}} }}
> >
{props.content} {escapedContent}
</ReactMarkdown> </ReactMarkdown>
); );
} }

View File

@ -84,7 +84,6 @@ You are ChatGPT, a large language model trained by OpenAI.
Knowledge cutoff: {{cutoff}} Knowledge cutoff: {{cutoff}}
Current model: {{model}} Current model: {{model}}
Current time: {{time}} Current time: {{time}}
Latex inline: $x^2$ Latex inline: $x^2$
Latex block: $$e=mc^2$$ Latex block: $$e=mc^2$$
`; `;