Skip to content

Commit

Permalink
feat: enhance markdown component with LaTeX support and update toolti…
Browse files Browse the repository at this point in the history
…p rendering
  • Loading branch information
athrael-soju committed Jan 20, 2025
1 parent 53f98dd commit 5769c09
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 14 deletions.
22 changes: 17 additions & 5 deletions components/markdown.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
import 'katex/dist/katex.min.css';
import Link from 'next/link';
import React, { memo, useMemo, useState } from 'react';
import ReactMarkdown, { type Components } from 'react-markdown';
import remarkGfm from 'remark-gfm';
import remarkMath from 'remark-math';
import rehypeKatex from 'rehype-katex';
import { CodeBlock } from './code-block';

const components: Partial<Components> = {
Expand Down Expand Up @@ -93,17 +96,26 @@ const components: Partial<Components> = {
},
};

const remarkPlugins = [remarkGfm];

const NonMemoizedMarkdown = ({ children }: { children: string }) => {
// Replace LaTeX delimiters with delimiters supported by rehype-katex.
const processedText = children
.replace(/\\\[/g, `$$$`)
.replace(/\\\]/g, `$$$`)
.replace(/\\\(/g, `$$$`)
.replace(/\\\)/g, `$$$`);

return (
<ReactMarkdown remarkPlugins={remarkPlugins} components={components}>
{children}
<ReactMarkdown
remarkPlugins={[remarkGfm, [remarkMath, { singleDollarTextMath: false }]]}
rehypePlugins={[[rehypeKatex, { output: 'html' }]]}
components={components}
>
{processedText}
</ReactMarkdown>
);
};

export const Markdown = memo(
NonMemoizedMarkdown,
(prevProps, nextProps) => prevProps.children === nextProps.children,
(prevProps, nextProps) => prevProps.children === nextProps.children
);
20 changes: 11 additions & 9 deletions components/ui/tooltip.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,17 @@ const TooltipContent = React.forwardRef<
React.ElementRef<typeof TooltipPrimitive.Content>,
React.ComponentPropsWithoutRef<typeof TooltipPrimitive.Content>
>(({ className, sideOffset = 4, ...props }, ref) => (
<TooltipPrimitive.Content
ref={ref}
sideOffset={sideOffset}
className={cn(
'z-50 overflow-hidden rounded-md border bg-popover px-3 py-1.5 text-sm text-popover-foreground shadow-md animate-in fade-in-0 zoom-in-95 data-[state=closed]:animate-out data-[state=closed]:fade-out-0 data-[state=closed]:zoom-out-95 data-[side=bottom]:slide-in-from-top-2 data-[side=left]:slide-in-from-right-2 data-[side=right]:slide-in-from-left-2 data-[side=top]:slide-in-from-bottom-2',
className,
)}
{...props}
/>
<TooltipPrimitive.Portal>
<TooltipPrimitive.Content
ref={ref}
sideOffset={sideOffset}
className={cn(
'z-50 overflow-hidden rounded-md border bg-popover px-3 py-1.5 text-sm text-popover-foreground shadow-md animate-in fade-in-0 zoom-in-95 data-[state=closed]:animate-out data-[state=closed]:fade-out-0 data-[state=closed]:zoom-out-95 data-[side=bottom]:slide-in-from-top-2 data-[side=left]:slide-in-from-right-2 data-[side=right]:slide-in-from-left-2 data-[side=top]:slide-in-from-bottom-2',
className
)}
{...props}
/>
</TooltipPrimitive.Portal>
));
TooltipContent.displayName = TooltipPrimitive.Content.displayName;

Expand Down

0 comments on commit 5769c09

Please sign in to comment.