An AI-powered search engine with a generative UI
TypeScript generative-aigenerative-uinextjsollamareactsearxngshadcn-uitailwindcsstavilytypescriptupstashvercel-ai-sdk
Modify message component to render LaTeX equations
Yoshiki Miuraac4434505df4
diff --git a/components/message.tsx b/components/message.tsxindex 7ba97110..e965202c 100644--- a/components/message.tsx+++ b/components/message.tsx@@ -14,13 +14,30 @@ export function BotMessage({ content }: { content: StreamableValue<string> }) {// Currently, sometimes error occurs after finishing the stream.if (error) return <div>Error</div>+ //modify the content to render LaTeX equations+ const processedData = preprocessLaTeX(data || '')+return (<MemoizedReactMarkdownrehypePlugins={[[rehypeExternalLinks, { target: '_blank' }], rehypeKatex]}remarkPlugins={[remarkGfm, remarkMath]}className="prose-sm prose-neutral prose-a:text-accent-foreground/50">- {data}+ {processedData}</MemoizedReactMarkdown>)}++// Preprocess LaTeX equations to be rendered by KaTeX+// ref: https://github.com/remarkjs/react-markdown/issues/785+const preprocessLaTeX = (content: string) => {+ const blockProcessedContent = content.replace(+ /\\\[([\s\S]*?)\\\]/g,+ (_, equation) => `$$${equation}$$`+ )+ const inlineProcessedContent = blockProcessedContent.replace(+ /\\\(([\s\S]*?)\\\)/g,+ (_, equation) => `$${equation}$`+ )+ return inlineProcessedContent+}