Files
agent-chat-ui/src/components/thread/markdown-text.tsx

215 lines
5.3 KiB
TypeScript
Raw Normal View History

2025-02-26 17:09:16 -08:00
"use client";
import "@assistant-ui/react-markdown/styles/dot.css";
import {
CodeHeaderProps,
unstable_memoizeMarkdownComponents as memoizeMarkdownComponents,
useIsMarkdownCodeBlock,
} from "@assistant-ui/react-markdown";
import ReactMarkdown from "react-markdown";
2025-02-26 17:09:16 -08:00
import remarkGfm from "remark-gfm";
import { FC, memo, useState } from "react";
import { CheckIcon, CopyIcon } from "lucide-react";
import { TooltipIconButton } from "@/components/thread/tooltip-icon-button";
2025-02-26 17:09:16 -08:00
import { cn } from "@/lib/utils";
const MarkdownTextImpl = ({ children }: { children: string }) => {
2025-02-26 17:09:16 -08:00
return (
<ReactMarkdown remarkPlugins={[remarkGfm]} components={defaultComponents}>
{children}
</ReactMarkdown>
2025-02-26 17:09:16 -08:00
);
};
export const MarkdownText = memo(MarkdownTextImpl);
const CodeHeader: FC<CodeHeaderProps> = ({ language, code }) => {
const { isCopied, copyToClipboard } = useCopyToClipboard();
const onCopy = () => {
if (!code || isCopied) return;
copyToClipboard(code);
};
return (
<div className="flex items-center justify-between gap-4 rounded-t-lg bg-zinc-900 px-4 py-2 text-sm font-semibold text-white">
<span className="lowercase [&>span]:text-xs">{language}</span>
<TooltipIconButton tooltip="Copy" onClick={onCopy}>
{!isCopied && <CopyIcon />}
{isCopied && <CheckIcon />}
</TooltipIconButton>
</div>
);
};
const useCopyToClipboard = ({
copiedDuration = 3000,
}: {
copiedDuration?: number;
} = {}) => {
const [isCopied, setIsCopied] = useState<boolean>(false);
const copyToClipboard = (value: string) => {
if (!value) return;
navigator.clipboard.writeText(value).then(() => {
setIsCopied(true);
setTimeout(() => setIsCopied(false), copiedDuration);
});
};
return { isCopied, copyToClipboard };
};
const defaultComponents = memoizeMarkdownComponents({
h1: ({ className, ...props }) => (
2025-02-27 14:08:24 -08:00
<h1
className={cn(
"mb-8 scroll-m-20 text-4xl font-extrabold tracking-tight last:mb-0",
className,
)}
{...props}
/>
2025-02-26 17:09:16 -08:00
),
h2: ({ className, ...props }) => (
2025-02-27 14:08:24 -08:00
<h2
className={cn(
"mb-4 mt-8 scroll-m-20 text-3xl font-semibold tracking-tight first:mt-0 last:mb-0",
className,
)}
{...props}
/>
2025-02-26 17:09:16 -08:00
),
h3: ({ className, ...props }) => (
2025-02-27 14:08:24 -08:00
<h3
className={cn(
"mb-4 mt-6 scroll-m-20 text-2xl font-semibold tracking-tight first:mt-0 last:mb-0",
className,
)}
{...props}
/>
2025-02-26 17:09:16 -08:00
),
h4: ({ className, ...props }) => (
2025-02-27 14:08:24 -08:00
<h4
className={cn(
"mb-4 mt-6 scroll-m-20 text-xl font-semibold tracking-tight first:mt-0 last:mb-0",
className,
)}
{...props}
/>
2025-02-26 17:09:16 -08:00
),
h5: ({ className, ...props }) => (
2025-02-27 14:08:24 -08:00
<h5
className={cn(
"my-4 text-lg font-semibold first:mt-0 last:mb-0",
className,
)}
{...props}
/>
2025-02-26 17:09:16 -08:00
),
h6: ({ className, ...props }) => (
2025-02-27 14:08:24 -08:00
<h6
className={cn("my-4 font-semibold first:mt-0 last:mb-0", className)}
{...props}
/>
2025-02-26 17:09:16 -08:00
),
p: ({ className, ...props }) => (
2025-02-27 14:08:24 -08:00
<p
className={cn("mb-5 mt-5 leading-7 first:mt-0 last:mb-0", className)}
{...props}
/>
2025-02-26 17:09:16 -08:00
),
a: ({ className, ...props }) => (
2025-02-27 14:08:24 -08:00
<a
className={cn(
"text-primary font-medium underline underline-offset-4",
className,
)}
{...props}
/>
2025-02-26 17:09:16 -08:00
),
blockquote: ({ className, ...props }) => (
2025-02-27 14:08:24 -08:00
<blockquote
className={cn("border-l-2 pl-6 italic", className)}
{...props}
/>
2025-02-26 17:09:16 -08:00
),
ul: ({ className, ...props }) => (
2025-02-27 14:08:24 -08:00
<ul
className={cn("my-5 ml-6 list-disc [&>li]:mt-2", className)}
{...props}
/>
2025-02-26 17:09:16 -08:00
),
ol: ({ className, ...props }) => (
2025-02-27 14:08:24 -08:00
<ol
className={cn("my-5 ml-6 list-decimal [&>li]:mt-2", className)}
{...props}
/>
2025-02-26 17:09:16 -08:00
),
hr: ({ className, ...props }) => (
<hr className={cn("my-5 border-b", className)} {...props} />
),
table: ({ className, ...props }) => (
2025-02-27 14:08:24 -08:00
<table
className={cn(
"my-5 w-full border-separate border-spacing-0 overflow-y-auto",
className,
)}
{...props}
/>
2025-02-26 17:09:16 -08:00
),
th: ({ className, ...props }) => (
2025-02-27 14:08:24 -08:00
<th
className={cn(
"bg-muted px-4 py-2 text-left font-bold first:rounded-tl-lg last:rounded-tr-lg [&[align=center]]:text-center [&[align=right]]:text-right",
className,
)}
{...props}
/>
2025-02-26 17:09:16 -08:00
),
td: ({ className, ...props }) => (
2025-02-27 14:08:24 -08:00
<td
className={cn(
"border-b border-l px-4 py-2 text-left last:border-r [&[align=center]]:text-center [&[align=right]]:text-right",
className,
)}
{...props}
/>
2025-02-26 17:09:16 -08:00
),
tr: ({ className, ...props }) => (
2025-02-27 14:08:24 -08:00
<tr
className={cn(
"m-0 border-b p-0 first:border-t [&:last-child>td:first-child]:rounded-bl-lg [&:last-child>td:last-child]:rounded-br-lg",
className,
)}
{...props}
/>
2025-02-26 17:09:16 -08:00
),
sup: ({ className, ...props }) => (
2025-02-27 14:08:24 -08:00
<sup
className={cn("[&>a]:text-xs [&>a]:no-underline", className)}
{...props}
/>
2025-02-26 17:09:16 -08:00
),
pre: ({ className, ...props }) => (
2025-02-27 14:08:24 -08:00
<pre
className={cn(
"overflow-x-auto rounded-b-lg bg-black p-4 text-white",
className,
)}
{...props}
/>
2025-02-26 17:09:16 -08:00
),
code: function Code({ className, ...props }) {
const isCodeBlock = useIsMarkdownCodeBlock();
return (
<code
className={cn(!isCodeBlock && "rounded font-semibold", className)}
2025-02-26 17:09:16 -08:00
{...props}
/>
);
},
CodeHeader,
});