2024-11-14 12:16:05 -05:00
|
|
|
import Link from 'next/link';
|
|
|
|
|
import React, { memo } from 'react';
|
2024-11-15 12:18:17 -05:00
|
|
|
import ReactMarkdown, { type Components } from 'react-markdown';
|
2024-11-14 12:16:05 -05:00
|
|
|
import remarkGfm from 'remark-gfm';
|
2024-10-11 18:00:22 +05:30
|
|
|
|
|
|
|
|
const NonMemoizedMarkdown = ({ children }: { children: string }) => {
|
2024-11-15 12:18:17 -05:00
|
|
|
const components: Partial<Components> = {
|
|
|
|
|
// @ts-expect-error
|
|
|
|
|
code: ({ node, inline, className, children, ...props }) => {
|
2024-11-14 12:16:05 -05:00
|
|
|
const match = /language-(\w+)/.exec(className || '');
|
2024-10-11 18:00:22 +05:30
|
|
|
return !inline && match ? (
|
2024-11-15 12:18:17 -05:00
|
|
|
// @ts-expect-error
|
2024-10-11 18:00:22 +05:30
|
|
|
<pre
|
|
|
|
|
{...props}
|
|
|
|
|
className={`${className} text-sm w-[80dvw] md:max-w-[500px] overflow-x-scroll bg-zinc-100 p-3 rounded-lg mt-2 dark:bg-zinc-800`}
|
|
|
|
|
>
|
|
|
|
|
<code className={match[1]}>{children}</code>
|
|
|
|
|
</pre>
|
|
|
|
|
) : (
|
|
|
|
|
<code
|
|
|
|
|
className={`${className} text-sm bg-zinc-100 dark:bg-zinc-800 py-0.5 px-1 rounded-md`}
|
|
|
|
|
{...props}
|
|
|
|
|
>
|
|
|
|
|
{children}
|
|
|
|
|
</code>
|
|
|
|
|
);
|
|
|
|
|
},
|
2024-11-15 12:18:17 -05:00
|
|
|
ol: ({ node, children, ...props }) => {
|
2024-10-11 18:00:22 +05:30
|
|
|
return (
|
|
|
|
|
<ol className="list-decimal list-outside ml-4" {...props}>
|
|
|
|
|
{children}
|
|
|
|
|
</ol>
|
|
|
|
|
);
|
|
|
|
|
},
|
2024-11-15 12:18:17 -05:00
|
|
|
li: ({ node, children, ...props }) => {
|
2024-10-11 18:00:22 +05:30
|
|
|
return (
|
|
|
|
|
<li className="py-1" {...props}>
|
|
|
|
|
{children}
|
|
|
|
|
</li>
|
|
|
|
|
);
|
|
|
|
|
},
|
2024-11-15 12:18:17 -05:00
|
|
|
ul: ({ node, children, ...props }) => {
|
2024-10-11 18:00:22 +05:30
|
|
|
return (
|
|
|
|
|
<ul className="list-decimal list-outside ml-4" {...props}>
|
|
|
|
|
{children}
|
|
|
|
|
</ul>
|
|
|
|
|
);
|
|
|
|
|
},
|
2024-11-15 12:18:17 -05:00
|
|
|
strong: ({ node, children, ...props }) => {
|
2024-10-11 18:00:22 +05:30
|
|
|
return (
|
|
|
|
|
<span className="font-semibold" {...props}>
|
|
|
|
|
{children}
|
|
|
|
|
</span>
|
|
|
|
|
);
|
|
|
|
|
},
|
2024-11-15 12:18:17 -05:00
|
|
|
a: ({ node, children, ...props }) => {
|
2024-10-11 18:00:22 +05:30
|
|
|
return (
|
2024-11-15 12:18:17 -05:00
|
|
|
// @ts-expect-error
|
2024-10-11 18:00:22 +05:30
|
|
|
<Link
|
|
|
|
|
className="text-blue-500 hover:underline"
|
|
|
|
|
target="_blank"
|
|
|
|
|
rel="noreferrer"
|
|
|
|
|
{...props}
|
|
|
|
|
>
|
|
|
|
|
{children}
|
|
|
|
|
</Link>
|
|
|
|
|
);
|
|
|
|
|
},
|
2024-11-15 12:18:17 -05:00
|
|
|
h1: ({ node, children, ...props }) => {
|
2024-10-30 16:01:24 +05:30
|
|
|
return (
|
|
|
|
|
<h1 className="text-3xl font-semibold mt-6 mb-2" {...props}>
|
|
|
|
|
{children}
|
|
|
|
|
</h1>
|
|
|
|
|
);
|
|
|
|
|
},
|
2024-11-15 12:18:17 -05:00
|
|
|
h2: ({ node, children, ...props }) => {
|
2024-10-30 16:01:24 +05:30
|
|
|
return (
|
|
|
|
|
<h2 className="text-2xl font-semibold mt-6 mb-2" {...props}>
|
|
|
|
|
{children}
|
|
|
|
|
</h2>
|
|
|
|
|
);
|
|
|
|
|
},
|
2024-11-15 12:18:17 -05:00
|
|
|
h3: ({ node, children, ...props }) => {
|
2024-10-30 16:01:24 +05:30
|
|
|
return (
|
|
|
|
|
<h3 className="text-xl font-semibold mt-6 mb-2" {...props}>
|
|
|
|
|
{children}
|
|
|
|
|
</h3>
|
|
|
|
|
);
|
|
|
|
|
},
|
2024-11-15 12:18:17 -05:00
|
|
|
h4: ({ node, children, ...props }) => {
|
2024-10-30 16:01:24 +05:30
|
|
|
return (
|
|
|
|
|
<h4 className="text-lg font-semibold mt-6 mb-2" {...props}>
|
|
|
|
|
{children}
|
|
|
|
|
</h4>
|
|
|
|
|
);
|
|
|
|
|
},
|
2024-11-15 12:18:17 -05:00
|
|
|
h5: ({ node, children, ...props }) => {
|
2024-10-30 16:01:24 +05:30
|
|
|
return (
|
|
|
|
|
<h5 className="text-base font-semibold mt-6 mb-2" {...props}>
|
|
|
|
|
{children}
|
|
|
|
|
</h5>
|
|
|
|
|
);
|
|
|
|
|
},
|
2024-11-15 12:18:17 -05:00
|
|
|
h6: ({ node, children, ...props }) => {
|
2024-10-30 16:01:24 +05:30
|
|
|
return (
|
|
|
|
|
<h6 className="text-sm font-semibold mt-6 mb-2" {...props}>
|
|
|
|
|
{children}
|
|
|
|
|
</h6>
|
|
|
|
|
);
|
|
|
|
|
},
|
2024-10-11 18:00:22 +05:30
|
|
|
};
|
|
|
|
|
|
|
|
|
|
return (
|
|
|
|
|
<ReactMarkdown remarkPlugins={[remarkGfm]} components={components}>
|
|
|
|
|
{children}
|
|
|
|
|
</ReactMarkdown>
|
|
|
|
|
);
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
export const Markdown = memo(
|
|
|
|
|
NonMemoizedMarkdown,
|
2024-11-15 13:00:15 -05:00
|
|
|
(prevProps, nextProps) => prevProps.children === nextProps.children,
|
2024-10-11 18:00:22 +05:30
|
|
|
);
|