Skip to content

Commit 8d26eff

Browse files
authored
feat: mathjax regex (#135)
1 parent b86495e commit 8d26eff

File tree

2 files changed

+55
-5
lines changed

2 files changed

+55
-5
lines changed

src/components/AiChat/AiChat.test.tsx

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
/* eslint-disable testing-library/await-async-utils */
33
import { render, screen, waitFor } from "@testing-library/react"
44
import user from "@testing-library/user-event"
5-
import { AiChat } from "./AiChat"
5+
import { AiChat, replaceMathjax } from "./AiChat"
66
import { ThemeProvider } from "../ThemeProvider/ThemeProvider"
77
import * as React from "react"
88
import { AiChatProps } from "./types"
@@ -273,3 +273,32 @@ describe("AiChat", () => {
273273
expect(messages[0]).toHaveTextContent("Starter 1")
274274
})
275275
})
276+
277+
test("replaceMathjax replaces unsupported MathJax syntax", () => {
278+
const input = `Hello \\(E=mc^2\\) and \\(a^2 + b^2 = c^2\\). Also
279+
280+
\\[
281+
F = ma
282+
\\]
283+
284+
and
285+
286+
\\[ PV = NkT \\]
287+
288+
Bye now.
289+
`
290+
291+
const expectedOutput = `Hello $E=mc^2$ and $a^2 + b^2 = c^2$. Also
292+
293+
$$
294+
F = ma
295+
$$
296+
297+
and
298+
299+
$$ PV = NkT $$
300+
301+
Bye now.
302+
`
303+
expect(replaceMathjax(input)).toBe(expectedOutput)
304+
})

src/components/AiChat/AiChat.tsx

Lines changed: 25 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -341,9 +341,13 @@ const AiChatDisplay: FC<AiChatDisplayProps> = ({
341341
<VisuallyHidden as={m.role === "user" ? "h5" : "h6"}>
342342
{m.role === "user" ? "You said: " : "Assistant said: "}
343343
</VisuallyHidden>
344-
<Markdown enableMathjax={useMathJax}>
345-
{m.content}
346-
</Markdown>
344+
{useMathJax ? (
345+
<Markdown enableMathjax={true}>
346+
{replaceMathjax(m.content)}
347+
</Markdown>
348+
) : (
349+
<Markdown>{m.content}</Markdown>
350+
)}
347351
</Message>
348352
</MessageRow>
349353
)
@@ -474,4 +478,21 @@ const AiChat: FC<AiChatProps> = ({
474478
)
475479
}
476480

477-
export { AiChatDisplay, AiChat }
481+
// react-markdown expects Mathjax delimiters to be $...$ or $$...$$
482+
// the prompt for the tutorbot asks for Mathjax tags with $ format but
483+
// the LLM does not get it right all the time
484+
// this function replaces the Mathjax tags with the correct format
485+
// eventually we will probably be able to remove this as LLMs get better
486+
function replaceMathjax(inputString: string): string {
487+
// Replace instances of \(...\) and \[...\] Mathjax tags with $...$
488+
// and $$...$$ tags.
489+
const INLINE_MATH_REGEX = /\\\((.*?)\\\)/g
490+
const DISPLAY_MATH_REGEX = /\\\[(([\s\S]*?))\\\]/g
491+
inputString = inputString.replace(
492+
INLINE_MATH_REGEX,
493+
(_match, p1) => `$${p1}$`,
494+
)
495+
return inputString.replace(DISPLAY_MATH_REGEX, (_match, p1) => `$$${p1}$$`)
496+
}
497+
498+
export { AiChatDisplay, AiChat, replaceMathjax }

0 commit comments

Comments
 (0)