Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: support math formula #1857

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions app/[locale]/layout.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import { Inter } from "next/font/google"
import { cookies } from "next/headers"
import { ReactNode } from "react"
import "./globals.css"
import "katex/dist/katex.min.css"

const inter = Inter({ subsets: ["latin"] })
const APP_NAME = "Chatbot UI"
Expand Down
12 changes: 11 additions & 1 deletion components/messages/message-markdown.tsx
Original file line number Diff line number Diff line change
@@ -1,18 +1,28 @@
import React, { FC } from "react"
import remarkGfm from "remark-gfm"
import remarkMath from "remark-math"
import rehypeKatex from "rehype-katex";
import { MessageCodeBlock } from "./message-codeblock"
import { MessageMarkdownMemoized } from "./message-markdown-memoized"

interface MessageMarkdownProps {
content: string
}

const replaceMathDelimiters = (content: string) => {
return content
.replace(/\\\(/g, "$")
.replace(/\\\)/g, "$")
.replace(/\\\[/g, "\n$$")
.replace(/\\\]/g, "$$\n");
};

export const MessageMarkdown: FC<MessageMarkdownProps> = ({ content }) => {
return (
<MessageMarkdownMemoized
className="prose dark:prose-invert prose-p:leading-relaxed prose-pre:p-0 min-w-full space-y-6 break-words"
remarkPlugins={[remarkGfm, remarkMath]}
rehypePlugins={[rehypeKatex]}
components={{
p({ children }) {
return <p className="mb-2 last:mb-0">{children}</p>
Expand Down Expand Up @@ -59,7 +69,7 @@ export const MessageMarkdown: FC<MessageMarkdownProps> = ({ content }) => {
}
}}
>
{content}
{replaceMathDelimiters(content)}
</MessageMarkdownMemoized>
)
}
217 changes: 216 additions & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,7 @@
"react-markdown": "^9.0.1",
"react-syntax-highlighter": "^15.5.0",
"react-textarea-autosize": "^8.5.3",
"rehype-katex": "^7.0.1",
"remark-gfm": "^4.0.0",
"remark-math": "^6.0.0",
"sonner": "^1.3.1",
Expand Down