|
| 1 | +import type { ComponentPropsWithoutRef } from "react"; |
| 2 | +import ReactMarkdown, { type Components } from "react-markdown"; |
| 3 | +import remarkGfm from "remark-gfm"; |
| 4 | + |
| 5 | +const ANCHOR_CLASS = |
| 6 | + "underline underline-offset-2 text-[var(--ds-accent)] hover:text-[var(--ds-accent-hover)] transition-colors duration-[var(--ds-motion-fast)]"; |
| 7 | + |
| 8 | +const CODE_INLINE_CLASS = |
| 9 | + "font-mono bg-[var(--ds-bg-elevated)] text-[var(--ds-fg-primary)] px-1.5 py-0.5 rounded-[var(--ds-radius-sm)] text-[var(--ds-text-xs)] border border-[var(--ds-border)]"; |
| 10 | + |
| 11 | +const CODE_BLOCK_CLASS = |
| 12 | + "font-mono bg-[var(--ds-bg-elevated)] text-[var(--ds-fg-primary)] p-3 rounded-[var(--ds-radius-md)] overflow-x-auto text-[var(--ds-text-xs)] leading-relaxed border border-[var(--ds-border)]"; |
| 13 | + |
| 14 | +type CodeProps = ComponentPropsWithoutRef<"code"> & { inline?: boolean }; |
| 15 | + |
| 16 | +const components: Components = { |
| 17 | + p: ({ children, ...rest }) => ( |
| 18 | + <p |
| 19 | + {...rest} |
| 20 | + className="mb-2 last:mb-0 leading-[1.55] text-[var(--ds-text-sm)] text-[var(--ds-fg-primary)]" |
| 21 | + > |
| 22 | + {children} |
| 23 | + </p> |
| 24 | + ), |
| 25 | + a: ({ children, href, ...rest }) => ( |
| 26 | + <a {...rest} href={href} target="_blank" rel="noreferrer noopener" className={ANCHOR_CLASS}> |
| 27 | + {children} |
| 28 | + </a> |
| 29 | + ), |
| 30 | + strong: ({ children, ...rest }) => ( |
| 31 | + <strong {...rest} className="font-semibold text-[var(--ds-fg-primary)]"> |
| 32 | + {children} |
| 33 | + </strong> |
| 34 | + ), |
| 35 | + em: ({ children, ...rest }) => ( |
| 36 | + <em {...rest} className="italic text-[var(--ds-fg-primary)]"> |
| 37 | + {children} |
| 38 | + </em> |
| 39 | + ), |
| 40 | + ul: ({ children, ...rest }) => ( |
| 41 | + <ul |
| 42 | + {...rest} |
| 43 | + className="mb-2 last:mb-0 ml-4 list-disc space-y-1 text-[var(--ds-text-sm)] marker:text-[var(--ds-fg-subtle)]" |
| 44 | + > |
| 45 | + {children} |
| 46 | + </ul> |
| 47 | + ), |
| 48 | + ol: ({ children, ...rest }) => ( |
| 49 | + <ol |
| 50 | + {...rest} |
| 51 | + className="mb-2 last:mb-0 ml-5 list-decimal space-y-1 text-[var(--ds-text-sm)] marker:text-[var(--ds-fg-subtle)]" |
| 52 | + > |
| 53 | + {children} |
| 54 | + </ol> |
| 55 | + ), |
| 56 | + li: ({ children, ...rest }) => ( |
| 57 | + <li {...rest} className="leading-[1.55] text-[var(--ds-fg-primary)]"> |
| 58 | + {children} |
| 59 | + </li> |
| 60 | + ), |
| 61 | + h1: ({ children, ...rest }) => ( |
| 62 | + <h1 |
| 63 | + {...rest} |
| 64 | + className="mb-2 mt-3 first:mt-0 text-[var(--ds-text-lg)] font-semibold tracking-[-0.01em] text-[var(--ds-fg-primary)]" |
| 65 | + > |
| 66 | + {children} |
| 67 | + </h1> |
| 68 | + ), |
| 69 | + h2: ({ children, ...rest }) => ( |
| 70 | + <h2 |
| 71 | + {...rest} |
| 72 | + className="mb-2 mt-3 first:mt-0 text-[var(--ds-text-md)] font-semibold tracking-[-0.01em] text-[var(--ds-fg-primary)]" |
| 73 | + > |
| 74 | + {children} |
| 75 | + </h2> |
| 76 | + ), |
| 77 | + h3: ({ children, ...rest }) => ( |
| 78 | + <h3 |
| 79 | + {...rest} |
| 80 | + className="mb-1.5 mt-2 first:mt-0 text-[var(--ds-text-sm)] font-semibold text-[var(--ds-fg-primary)]" |
| 81 | + > |
| 82 | + {children} |
| 83 | + </h3> |
| 84 | + ), |
| 85 | + blockquote: ({ children, ...rest }) => ( |
| 86 | + <blockquote |
| 87 | + {...rest} |
| 88 | + className="mb-2 last:mb-0 border-l-2 border-[var(--ds-border-strong)] pl-3 text-[var(--ds-fg-muted)] italic" |
| 89 | + > |
| 90 | + {children} |
| 91 | + </blockquote> |
| 92 | + ), |
| 93 | + hr: () => <hr className="my-3 border-t border-[var(--ds-border)]" />, |
| 94 | + table: ({ children, ...rest }) => ( |
| 95 | + <div className="mb-2 last:mb-0 overflow-x-auto rounded-[var(--ds-radius-sm)] border border-[var(--ds-border)]"> |
| 96 | + <table |
| 97 | + {...rest} |
| 98 | + className="w-full border-collapse text-[var(--ds-text-xs)] text-[var(--ds-fg-primary)]" |
| 99 | + > |
| 100 | + {children} |
| 101 | + </table> |
| 102 | + </div> |
| 103 | + ), |
| 104 | + thead: ({ children, ...rest }) => ( |
| 105 | + <thead {...rest} className="bg-[var(--ds-bg-elevated)] text-left"> |
| 106 | + {children} |
| 107 | + </thead> |
| 108 | + ), |
| 109 | + th: ({ children, ...rest }) => ( |
| 110 | + <th |
| 111 | + {...rest} |
| 112 | + className="border-b border-[var(--ds-border)] px-2.5 py-1.5 font-medium text-[var(--ds-fg-muted)]" |
| 113 | + > |
| 114 | + {children} |
| 115 | + </th> |
| 116 | + ), |
| 117 | + td: ({ children, ...rest }) => ( |
| 118 | + <td |
| 119 | + {...rest} |
| 120 | + className="border-b border-[var(--ds-border)] px-2.5 py-1.5 align-top last:border-b-0" |
| 121 | + > |
| 122 | + {children} |
| 123 | + </td> |
| 124 | + ), |
| 125 | + pre: ({ children, ...rest }) => ( |
| 126 | + <pre {...rest} className={`mb-2 last:mb-0 ${CODE_BLOCK_CLASS}`}> |
| 127 | + {children} |
| 128 | + </pre> |
| 129 | + ), |
| 130 | + code: (props: CodeProps) => { |
| 131 | + const { inline, className = "", children, ...rest } = props; |
| 132 | + if (inline) { |
| 133 | + return ( |
| 134 | + <code {...rest} className={`${CODE_INLINE_CLASS} ${className}`}> |
| 135 | + {children} |
| 136 | + </code> |
| 137 | + ); |
| 138 | + } |
| 139 | + return ( |
| 140 | + <code {...rest} className={className}> |
| 141 | + {children} |
| 142 | + </code> |
| 143 | + ); |
| 144 | + }, |
| 145 | +}; |
| 146 | + |
| 147 | +export interface MarkdownProps { |
| 148 | + children: string; |
| 149 | +} |
| 150 | + |
| 151 | +export function Markdown({ children }: MarkdownProps) { |
| 152 | + return ( |
| 153 | + <ReactMarkdown remarkPlugins={[remarkGfm]} components={components}> |
| 154 | + {children} |
| 155 | + </ReactMarkdown> |
| 156 | + ); |
| 157 | +} |
0 commit comments