File size: 6,816 Bytes
a8aec61 5b73d4d a8aec61 5b73d4d a8aec61 5b73d4d a8aec61 |
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 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 |
'use client'
import { FC, useState } from 'react'
import Image from 'next/image'
import Link from 'next/link'
import { cn } from '@/lib/utils'
import type {
UnorderedListProps,
OrderedListProps,
EmphasizedTextProps,
ItalicTextProps,
StrongTextProps,
BoldTextProps,
DeletedTextProps,
UnderlinedTextProps,
HorizontalRuleProps,
BlockquoteProps,
AnchorLinkProps,
HeadingProps,
ImgProps,
ParagraphProps,
TableHeaderCellProps,
TableProps,
TableHeaderProps,
TableBodyProps,
TableRowProps,
TableCellProps,
PreparedTextProps
} from './types'
import { HEADING_SIZES } from '../Heading/constants'
import { PARAGRAPH_SIZES } from '../Paragraph/constants'
const filterProps = (props: object) => {
const newProps = { ...props }
if ('node' in newProps) {
delete newProps.node
}
return newProps
}
const UnorderedList = ({ className, ...props }: UnorderedListProps) => (
<ul
className={cn(
className,
PARAGRAPH_SIZES.body,
'flex list-disc flex-col pl-10'
)}
{...filterProps(props)}
/>
)
const OrderedList = ({ className, ...props }: OrderedListProps) => (
<ol
className={cn(
className,
PARAGRAPH_SIZES.body,
'flex list-decimal flex-col pl-10'
)}
{...filterProps(props)}
/>
)
const Paragraph = ({ className, ...props }: ParagraphProps) => (
<p className={cn(className, PARAGRAPH_SIZES.body)} {...filterProps(props)} />
)
const EmphasizedText = ({ className, ...props }: EmphasizedTextProps) => (
<em
className={cn(className, 'text-sm font-semibold')}
{...filterProps(props)}
/>
)
const ItalicText = ({ className, ...props }: ItalicTextProps) => (
<i
className={cn(className, 'italic', PARAGRAPH_SIZES.body)}
{...filterProps(props)}
/>
)
const StrongText = ({ className, ...props }: StrongTextProps) => (
<strong
className={cn(className, 'text-sm font-semibold')}
{...filterProps(props)}
/>
)
const BoldText = ({ className, ...props }: BoldTextProps) => (
<b
className={cn(className, 'text-sm font-semibold')}
{...filterProps(props)}
/>
)
const UnderlinedText = ({ className, ...props }: UnderlinedTextProps) => (
<u
className={cn(className, 'underline', PARAGRAPH_SIZES.body)}
{...filterProps(props)}
/>
)
const DeletedText = ({ className, ...props }: DeletedTextProps) => (
<del
className={cn(className, 'text-muted line-through', PARAGRAPH_SIZES.body)}
{...filterProps(props)}
/>
)
const HorizontalRule = ({ className, ...props }: HorizontalRuleProps) => (
<hr
className={cn(className, 'mx-auto w-48 border-b border-border')}
{...filterProps(props)}
/>
)
const InlineCode: FC<PreparedTextProps> = ({ children }) => {
return (
<code className="relative whitespace-pre-wrap rounded-sm bg-background-secondary/50 p-1">
{children}
</code>
)
}
const Blockquote = ({ className, ...props }: BlockquoteProps) => (
<blockquote
className={cn(className, 'italic', PARAGRAPH_SIZES.body)}
{...filterProps(props)}
/>
)
const AnchorLink = ({ className, ...props }: AnchorLinkProps) => (
<a
className={cn(className, 'cursor-pointer text-xs underline')}
target="_blank"
rel="noopener noreferrer"
{...filterProps(props)}
/>
)
const Heading1 = ({ className, ...props }: HeadingProps) => (
<h1 className={cn(className, HEADING_SIZES[3])} {...filterProps(props)} />
)
const Heading2 = ({ className, ...props }: HeadingProps) => (
<h2 className={cn(className, HEADING_SIZES[3])} {...filterProps(props)} />
)
const Heading3 = ({ className, ...props }: HeadingProps) => (
<h3 className={cn(className, PARAGRAPH_SIZES.lead)} {...filterProps(props)} />
)
const Heading4 = ({ className, ...props }: HeadingProps) => (
<h4 className={cn(className, PARAGRAPH_SIZES.lead)} {...filterProps(props)} />
)
const Heading5 = ({ className, ...props }: HeadingProps) => (
<h5
className={cn(className, PARAGRAPH_SIZES.title)}
{...filterProps(props)}
/>
)
const Heading6 = ({ className, ...props }: HeadingProps) => (
<h6
className={cn(className, PARAGRAPH_SIZES.title)}
{...filterProps(props)}
/>
)
const Img = ({ src, alt }: ImgProps) => {
const [error, setError] = useState(false)
if (!src) return null
return (
<div className="w-full max-w-xl">
{error ? (
<div className="flex h-40 flex-col items-center justify-center gap-2 rounded-md bg-secondary/50 text-muted">
<Paragraph className="text-primary">Image unavailable</Paragraph>
<Link
href={typeof src === 'string' ? src : '#'}
target="_blank"
className="max-w-md truncate underline"
>
{typeof src === 'string' ? src : 'Invalid source'}
</Link>
</div>
) : (
<Image
src={typeof src === 'string' ? src : ''}
width={1280}
height={720}
alt={alt ?? 'Rendered image'}
className="size-full rounded-md object-cover"
onError={() => setError(true)}
unoptimized
/>
)}
</div>
)
}
const Table = ({ className, ...props }: TableProps) => (
<div className="w-full max-w-[560px] overflow-hidden rounded-md border border-border">
<div className="w-full overflow-x-auto">
<table className={cn(className, 'w-full')} {...filterProps(props)} />
</div>
</div>
)
const TableHead = ({ className, ...props }: TableHeaderProps) => (
<thead
className={cn(
className,
'rounded-md border-b border-border bg-transparent p-2 text-left text-sm font-[600]'
)}
{...filterProps(props)}
/>
)
const TableHeadCell = ({ className, ...props }: TableHeaderCellProps) => (
<th
className={cn(className, 'p-2 text-sm font-[600]')}
{...filterProps(props)}
/>
)
const TableBody = ({ className, ...props }: TableBodyProps) => (
<tbody className={cn(className, 'text-xs')} {...filterProps(props)} />
)
const TableRow = ({ className, ...props }: TableRowProps) => (
<tr
className={cn(className, 'border-b border-border last:border-b-0')}
{...filterProps(props)}
/>
)
const TableCell = ({ className, ...props }: TableCellProps) => (
<td
className={cn(className, 'whitespace-nowrap p-2 font-[400]')}
{...filterProps(props)}
/>
)
export const components = {
h1: Heading1,
h2: Heading2,
h3: Heading3,
h4: Heading4,
h5: Heading5,
h6: Heading6,
ul: UnorderedList,
ol: OrderedList,
em: EmphasizedText,
i: ItalicText,
strong: StrongText,
b: BoldText,
u: UnderlinedText,
del: DeletedText,
hr: HorizontalRule,
blockquote: Blockquote,
code: InlineCode,
a: AnchorLink,
img: Img,
p: Paragraph,
table: Table,
thead: TableHead,
th: TableHeadCell,
tbody: TableBody,
tr: TableRow,
td: TableCell
}
|