Skip to content

Commit

Permalink
feat: support Latex (#5001)
Browse files Browse the repository at this point in the history
Co-authored-by: crazywoola <[email protected]>
  • Loading branch information
ZuzooVn and crazywoola authored Jun 18, 2024
1 parent c7641be commit 3828d4c
Showing 1 changed file with 8 additions and 2 deletions.
10 changes: 8 additions & 2 deletions web/app/components/base/markdown.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,11 @@ const getCorrectCapitalizationLanguageName = (language: string) => {

return language.charAt(0).toUpperCase() + language.substring(1)
}

const preprocessLaTeX = (content: string) =>
content.replace(/\\\[(.*?)\\\]/gs, (_, equation) => `$$${equation}$$`)
.replace(/\\\((.*?)\\\)/gs, (_, equation) => `$${equation}$`)

export function PreCode(props: { children: any }) {
const ref = useRef<HTMLPreElement>(null)

Expand Down Expand Up @@ -82,12 +87,13 @@ const useLazyLoad = (ref: RefObject<Element>): boolean => {

export function Markdown(props: { content: string; className?: string }) {
const [isSVG, setIsSVG] = useState(false)
const latexContent = preprocessLaTeX(props.content)
return (
<div className={cn(props.className, 'markdown-body')}>
<ReactMarkdown
remarkPlugins={[[RemarkMath, { singleDollarTextMath: false }], RemarkGfm, RemarkBreaks]}
rehypePlugins={[
RehypeKatex,
RehypeKatex as any,
]}
components={{
code({ inline, className, children, ...props }) {
Expand Down Expand Up @@ -179,7 +185,7 @@ export function Markdown(props: { content: string; className?: string }) {
linkTarget='_blank'
>
{/* Markdown detect has problem. */}
{props.content}
{latexContent}
</ReactMarkdown>
</div>
)
Expand Down

0 comments on commit 3828d4c

Please sign in to comment.