|
| 1 | +import ReactMarkdown from "react-markdown"; |
| 2 | +import remarkGfm from "remark-gfm"; |
| 3 | +import type { ComponentPropsWithoutRef } from "react"; |
| 4 | + |
| 5 | +type MarkdownRendererProps = { |
| 6 | + content: string; |
| 7 | +}; |
| 8 | + |
| 9 | +const markdownComponents = { |
| 10 | + h1: (props: ComponentPropsWithoutRef<"h1">) => ( |
| 11 | + <h1 className="mb-6 text-3xl font-semibold text-gray-900" {...props} /> |
| 12 | + ), |
| 13 | + h2: (props: ComponentPropsWithoutRef<"h2">) => ( |
| 14 | + <h2 className="mb-4 mt-8 text-2xl font-semibold text-gray-900" {...props} /> |
| 15 | + ), |
| 16 | + h3: (props: ComponentPropsWithoutRef<"h3">) => ( |
| 17 | + <h3 className="mb-3 mt-6 text-xl font-semibold text-gray-900" {...props} /> |
| 18 | + ), |
| 19 | + p: (props: ComponentPropsWithoutRef<"p">) => ( |
| 20 | + <p className="mb-4 leading-7 text-gray-700" {...props} /> |
| 21 | + ), |
| 22 | + ul: (props: ComponentPropsWithoutRef<"ul">) => ( |
| 23 | + <ul className="mb-4 ml-6 list-disc space-y-2 text-gray-700" {...props} /> |
| 24 | + ), |
| 25 | + ol: (props: ComponentPropsWithoutRef<"ol">) => ( |
| 26 | + <ol className="mb-4 ml-6 list-decimal space-y-2 text-gray-700" {...props} /> |
| 27 | + ), |
| 28 | + li: (props: ComponentPropsWithoutRef<"li">) => ( |
| 29 | + <li className="leading-7" {...props} /> |
| 30 | + ), |
| 31 | + strong: (props: ComponentPropsWithoutRef<"strong">) => ( |
| 32 | + <strong className="font-semibold text-gray-900" {...props} /> |
| 33 | + ), |
| 34 | + a: (props: ComponentPropsWithoutRef<"a">) => ( |
| 35 | + <a className="text-blue-600 underline underline-offset-2 hover:text-blue-700" {...props} /> |
| 36 | + ), |
| 37 | + blockquote: (props: ComponentPropsWithoutRef<"blockquote">) => ( |
| 38 | + <blockquote className="mb-4 border-l-4 border-gray-300 pl-4 italic text-gray-600" {...props} /> |
| 39 | + ), |
| 40 | + hr: () => <hr className="my-8 border-gray-200" />, |
| 41 | + code: (props: ComponentPropsWithoutRef<"code">) => ( |
| 42 | + <code className="rounded bg-gray-100 px-1.5 py-0.5 font-mono text-sm text-gray-800" {...props} /> |
| 43 | + ), |
| 44 | +}; |
| 45 | + |
| 46 | +export default function MarkdownRenderer({ content }: MarkdownRendererProps) { |
| 47 | + return ( |
| 48 | + <article className="rounded-lg border border-gray-200 bg-white p-6 shadow-sm"> |
| 49 | + <div className="max-w-none"> |
| 50 | + <ReactMarkdown remarkPlugins={[remarkGfm]} components={markdownComponents}> |
| 51 | + {content} |
| 52 | + </ReactMarkdown> |
| 53 | + </div> |
| 54 | + </article> |
| 55 | + ); |
| 56 | +} |
| 57 | + |
0 commit comments