Skip to content

Commit

Permalink
Merge pull request ChatGPTNextWeb#4186 from MrrDrr/formula_rendering
Browse files Browse the repository at this point in the history
support \(...\) and \[...\] style math formula
  • Loading branch information
Dean-YZG authored Mar 25, 2024
2 parents f1b4c08 + 524c9be commit a4e4286
Showing 1 changed file with 19 additions and 1 deletion.
20 changes: 19 additions & 1 deletion app/components/markdown.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -116,9 +116,27 @@ function escapeDollarNumber(text: string) {
return escapedText;
}

function escapeBrackets(text: string) {
const pattern =
/(```[\s\S]*?```|`.*?`)|\\\[([\s\S]*?[^\\])\\\]|\\\((.*?)\\\)/g;
return text.replace(
pattern,
(match, codeBlock, squareBracket, roundBracket) => {
if (codeBlock) {
return codeBlock;
} else if (squareBracket) {
return `$$${squareBracket}$$`;
} else if (roundBracket) {
return `$${roundBracket}$`;
}
return match;
},
);
}

function _MarkDownContent(props: { content: string }) {
const escapedContent = useMemo(
() => escapeDollarNumber(props.content),
() => escapeBrackets(escapeDollarNumber(props.content)),
[props.content],
);

Expand Down

0 comments on commit a4e4286

Please sign in to comment.