feat: Add LaTeX support to the markdown renderer

Adds LaTeX support to the markdown renderer using `rehype-katex` for math equations.

- Replaces block-level LaTeX delimiters `\[ \]` with `$$ $$`.
- Replaces inline LaTeX delimiters `\( \)` with `$ $`.
- Preprocesses the message before rendering to ensure correct delimiters.

This improves the rendering of markdown messages containing mathematical expressions, enhancing the user experience.
This commit is contained in:
n4ze3m 2024-09-08 11:53:15 +05:30
parent c077201591
commit 824ecf0a8c
4 changed files with 49 additions and 22 deletions

BIN
bun.lockb

Binary file not shown.

View File

@ -1,23 +1,41 @@
import "katex/dist/katex.min.css"
import remarkGfm from "remark-gfm" import remarkGfm from "remark-gfm"
import remarkMath from "remark-math" import remarkMath from "remark-math"
import ReactMarkdown from "react-markdown" import ReactMarkdown from "react-markdown"
import rehypeKatex from "rehype-katex"
import "property-information" import "property-information"
import React from "react" import React from "react"
import { CodeBlock } from "./CodeBlock" import { CodeBlock } from "./CodeBlock"
export const preprocessLaTeX = (content: string) => {
// Replace block-level LaTeX delimiters \[ \] with $$ $$
export default function Markdown({ const blockProcessedContent = content.replace(
/\\\[(.*?)\\\]/gs,
(_, equation) => `$$${equation}$$`
)
// Replace inline LaTeX delimiters \( \) with $ $
const inlineProcessedContent = blockProcessedContent.replace(
/\\\((.*?)\\\)/gs,
(_, equation) => `$${equation}$`
)
return inlineProcessedContent
}
function Markdown({
message, message,
className = "prose break-words dark:prose-invert prose-p:leading-relaxed prose-pre:p-0 dark:prose-dark" className = "prose break-words dark:prose-invert prose-p:leading-relaxed prose-pre:p-0 dark:prose-dark"
}: { }: {
message: string message: string
className?: string className?: string
}) { }) {
message = preprocessLaTeX(message)
return ( return (
<React.Fragment> <React.Fragment>
<ReactMarkdown <ReactMarkdown
className={className} className={className}
remarkPlugins={[remarkGfm, remarkMath]} remarkPlugins={[remarkGfm, remarkMath]}
rehypePlugins={[rehypeKatex]}
components={{ components={{
code({ node, inline, className, children, ...props }) { code({ node, inline, className, children, ...props }) {
const match = /language-(\w+)/.exec(className || "") const match = /language-(\w+)/.exec(className || "")
@ -52,3 +70,5 @@ export default function Markdown({
</React.Fragment> </React.Fragment>
) )
} }
export default React.memo(Markdown)

View File

@ -1,35 +1,42 @@
import { useRef, useEffect, useState } from 'react'; import { useRef, useEffect, useState } from "react"
export const useSmartScroll = (messages: any[], streaming: boolean) => { export const useSmartScroll = (messages: any[], streaming: boolean) => {
const containerRef = useRef<HTMLDivElement>(null); const containerRef = useRef<HTMLDivElement>(null)
const [isAtBottom, setIsAtBottom] = useState(true); const [isAtBottom, setIsAtBottom] = useState(true)
useEffect(() => { useEffect(() => {
const container = containerRef.current; const container = containerRef.current
if (!container) return; if (!container) return
const handleScroll = () => { const handleScroll = () => {
const { scrollTop, scrollHeight, clientHeight } = container; const { scrollTop, scrollHeight, clientHeight } = container
setIsAtBottom(scrollHeight - scrollTop - clientHeight < 50); setIsAtBottom(scrollHeight - scrollTop - clientHeight < 50)
}; }
container.addEventListener('scroll', handleScroll); container.addEventListener("scroll", handleScroll)
return () => container.removeEventListener('scroll', handleScroll); return () => container.removeEventListener("scroll", handleScroll)
}, []); }, [])
useEffect(() => { useEffect(() => {
if (messages.length === 0) {
setIsAtBottom(true)
return
}
if (isAtBottom && containerRef.current) { if (isAtBottom && containerRef.current) {
const scrollOptions: ScrollIntoViewOptions = streaming const scrollOptions: ScrollIntoViewOptions = streaming
? { behavior: 'smooth', block: 'end' } ? { behavior: "smooth", block: "end" }
: { behavior: 'auto', block: 'end' }; : { behavior: "auto", block: "end" }
containerRef.current.lastElementChild?.scrollIntoView(scrollOptions); containerRef.current.lastElementChild?.scrollIntoView(scrollOptions)
} }
}, [messages, streaming, isAtBottom]); }, [messages, streaming, isAtBottom])
const scrollToBottom = () => { const scrollToBottom = () => {
containerRef.current?.lastElementChild?.scrollIntoView({ behavior: 'smooth', block: 'end' }); containerRef.current?.lastElementChild?.scrollIntoView({
}; behavior: "smooth",
block: "end"
})
}
return { containerRef, isAtBottom, scrollToBottom }
return { containerRef, isAtBottom, scrollToBottom }; }
};

View File

@ -50,7 +50,7 @@ export default defineConfig({
outDir: "build", outDir: "build",
manifest: { manifest: {
version: "1.2.2", version: "1.2.3",
name: name:
process.env.TARGET === "firefox" process.env.TARGET === "firefox"
? "Page Assist - A Web UI for Local AI Models" ? "Page Assist - A Web UI for Local AI Models"