Spaces:
Running
Running
File size: 5,479 Bytes
5012205 d8c725b 5012205 d8c725b 5012205 d8c725b 5012205 d8c725b 5012205 d8c725b 5012205 d8c725b 5012205 d8c725b 5012205 d8c725b 5012205 d8c725b 5012205 d8c725b 5012205 d8c725b 5012205 d8c725b 5012205 d8c725b 5012205 d8c725b 5012205 d8c725b 5012205 d8c725b 5012205 d8c725b 5012205 d8c725b 5012205 d8c725b 5012205 d8c725b 5012205 d8c725b 5012205 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 |
/* eslint-disable @typescript-eslint/no-unused-vars */
import Link from "next/link";
import React, { memo } from "react";
import ReactMarkdown, { type Components } from "react-markdown";
import remarkGfm from "remark-gfm";
import { cn } from "@/lib/utils";
const components: Partial<Components> = {
pre: ({ children, ...props }) => (
<pre className="overflow-x-auto rounded-lg bg-zinc-100 dark:bg-zinc-800/50 black:bg-zinc-800/50 p-2.5 my-1.5 text-sm" {...props}>
{children}
</pre>
),
code: ({ children, className, ...props }: React.HTMLProps<HTMLElement> & { className?: string }) => {
const match = /language-(\w+)/.exec(className || '');
const isInline = !match && !className;
if (isInline) {
return (
<code
className="px-1 py-0.5 rounded-md bg-zinc-100 dark:bg-zinc-800/50 black:bg-zinc-800/50 text-zinc-700 dark:text-zinc-300 black:text-zinc-300 text-[0.9em] font-mono"
{...props}
>
{children}
</code>
);
}
return (
<code className={cn("block font-mono text-sm", className)} {...props}>
{children}
</code>
);
},
ol: ({ node, children, ...props }) => (
<ol className="list-decimal list-outside ml-4 space-y-0.5 my-1.5" {...props}>
{children}
</ol>
),
ul: ({ node, children, ...props }) => (
<ul className="list-disc list-outside ml-4 space-y-0.5 my-1.5" {...props}>
{children}
</ul>
),
li: ({ node, children, ...props }) => (
<li className="leading-normal" {...props}>
{children}
</li>
),
p: ({ node, children, ...props }) => (
<p className="leading-relaxed my-1" {...props}>
{children}
</p>
),
strong: ({ node, children, ...props }) => (
<strong className="font-semibold" {...props}>
{children}
</strong>
),
em: ({ node, children, ...props }) => (
<em className="italic" {...props}>
{children}
</em>
),
blockquote: ({ node, children, ...props }) => (
<blockquote
className="border-l-2 border-zinc-200 dark:border-zinc-700 black:border-zinc-700 pl-3 my-1.5 italic text-zinc-600 dark:text-zinc-400 black:text-zinc-400"
{...props}
>
{children}
</blockquote>
),
a: ({ node, children, ...props }) => (
// @ts-expect-error error
<Link
className="text-blue-500 hover:underline hover:text-blue-600 dark:text-blue-400 dark:hover:text-blue-300 black:text-blue-400 black:hover:text-blue-300 transition-colors"
target="_blank"
rel="noreferrer"
{...props}
>
{children}
</Link>
),
h1: ({ node, children, ...props }) => (
<h1 className="text-2xl font-semibold mt-3 mb-1.5 text-zinc-800 dark:text-zinc-200 black:text-zinc-200" {...props}>
{children}
</h1>
),
h2: ({ node, children, ...props }) => (
<h2 className="text-xl font-semibold mt-2.5 mb-1.5 text-zinc-800 dark:text-zinc-200 black:text-zinc-200" {...props}>
{children}
</h2>
),
h3: ({ node, children, ...props }) => (
<h3 className="text-lg font-semibold mt-2 mb-1 text-zinc-800 dark:text-zinc-200 black:text-zinc-200" {...props}>
{children}
</h3>
),
h4: ({ node, children, ...props }) => (
<h4 className="text-base font-semibold mt-2 mb-1 text-zinc-800 dark:text-zinc-200 black:text-zinc-200" {...props}>
{children}
</h4>
),
h5: ({ node, children, ...props }) => (
<h5 className="text-sm font-semibold mt-2 mb-1 text-zinc-800 dark:text-zinc-200 black:text-zinc-200" {...props}>
{children}
</h5>
),
h6: ({ node, children, ...props }) => (
<h6 className="text-xs font-semibold mt-2 mb-0.5 text-zinc-800 dark:text-zinc-200 black:text-zinc-200" {...props}>
{children}
</h6>
),
table: ({ node, children, ...props }) => (
<div className="my-1.5 overflow-x-auto">
<table className="min-w-full divide-y divide-zinc-200 dark:divide-zinc-700 black:divide-zinc-700" {...props}>
{children}
</table>
</div>
),
thead: ({ node, children, ...props }) => (
<thead className="bg-zinc-50 dark:bg-zinc-800/50 black:bg-zinc-800/50" {...props}>
{children}
</thead>
),
tbody: ({ node, children, ...props }) => (
<tbody className="divide-y divide-zinc-200 dark:divide-zinc-700 black:divide-zinc-700 bg-white dark:bg-transparent black:bg-transparent" {...props}>
{children}
</tbody>
),
tr: ({ node, children, ...props }) => (
<tr className="transition-colors hover:bg-zinc-50 dark:hover:bg-zinc-800/30 black:hover:bg-zinc-800/30" {...props}>
{children}
</tr>
),
th: ({ node, children, ...props }) => (
<th
className="px-3 py-1.5 text-left text-xs font-medium text-zinc-500 dark:text-zinc-400 black:text-zinc-400 uppercase tracking-wider"
{...props}
>
{children}
</th>
),
td: ({ node, children, ...props }) => (
<td className="px-3 py-1.5 text-sm" {...props}>
{children}
</td>
),
hr: ({ node, ...props }) => (
<hr className="my-1.5 border-zinc-200 dark:border-zinc-700 black:border-zinc-700" {...props} />
),
};
const remarkPlugins = [remarkGfm];
const NonMemoizedMarkdown = ({ children }: { children: string }) => {
return (
<ReactMarkdown remarkPlugins={remarkPlugins} components={components}>
{children}
</ReactMarkdown>
);
};
export const Markdown = memo(
NonMemoizedMarkdown,
(prevProps, nextProps) => prevProps.children === nextProps.children,
); |