22
33import { Text } from "@/packages/ui" ;
44import { useMDXComponent } from "next-contentlayer/hooks" ;
5- import React , { HTMLAttributes } from "react" ;
5+ import React , { AnchorHTMLAttributes , HTMLAttributes } from "react" ;
66import { ComponentShowcase } from "./ComponentShowcase" ;
77import { cn } from "@/lib/utils" ;
88import { ComponentSource } from "./ComponentSource" ;
99import { CodeBlock } from "./CodeBlock" ;
10+ import Link from "next/link" ;
1011
11- const components = {
12+ const components = ( type : "doc" | "blog" ) => ( {
1213 h1 : ( props : HTMLAttributes < HTMLHeadingElement > ) => (
1314 < Text as = "h1" { ...props } />
1415 ) ,
15- h2 : ( props : HTMLAttributes < HTMLHeadingElement > ) => (
16- < Text as = "h2" className = "border-b lg:text-3xl pb-1 mb-6" { ...props } />
17- ) ,
16+ h2 : ( props : HTMLAttributes < HTMLHeadingElement > ) =>
17+ type === "blog" ? (
18+ < Text as = "h2" className = "mb-4" { ...props } />
19+ ) : (
20+ < Text as = "h2" className = "border-b pb-1 mb-6" { ...props } />
21+ ) ,
1822 h3 : ( props : HTMLAttributes < HTMLHeadingElement > ) => (
1923 < Text as = "h3" className = "mb-4" { ...props } />
2024 ) ,
@@ -23,6 +27,50 @@ const components = {
2327 ) ,
2428 h5 : ( props : HTMLAttributes < HTMLHeadElement > ) => < Text as = "h5" { ...props } /> ,
2529 h6 : ( props : HTMLAttributes < HTMLHeadElement > ) => < Text as = "h6" { ...props } /> ,
30+ p : ( props : HTMLAttributes < HTMLHeadElement > ) =>
31+ type === "blog" ? (
32+ < Text { ...props } className = "text-lg text-zinc-600" />
33+ ) : (
34+ < Text { ...props } />
35+ ) ,
36+ li : ( props : HTMLAttributes < HTMLHeadElement > ) =>
37+ type === "blog" ? (
38+ < Text
39+ as = "li"
40+ { ...props }
41+ className = "text-lg text-zinc-600 ml-4 lg:ml-8 mb-2"
42+ />
43+ ) : (
44+ < Text as = "li" { ...props } />
45+ ) ,
46+ img : ( props : HTMLAttributes < HTMLImageElement > ) => (
47+ < img className = "mx-auto w-full max-w-[600px] my-8" { ...props } />
48+ ) ,
49+ a : ( props : AnchorHTMLAttributes < HTMLAnchorElement > ) => {
50+ const { href, target, rel, ...rest } = props ;
51+
52+ if ( ! href ) {
53+ return < a { ...rest } /> ;
54+ }
55+
56+ const isExternal = href . startsWith ( "http" ) ;
57+
58+ return isExternal ? (
59+ < a
60+ href = { href }
61+ target = { target || "_blank" }
62+ rel = { rel || "noopener noreferrer" }
63+ className = "underline underline-offset-4 hover:decoration-primary-500"
64+ { ...rest }
65+ />
66+ ) : (
67+ < Link
68+ href = { href }
69+ className = "underline underline-offset-4 hover:decoration-primary-500"
70+ { ...rest }
71+ />
72+ ) ;
73+ } ,
2674 pre : CodeBlock ,
2775 code : ( {
2876 className,
@@ -41,10 +89,16 @@ const components = {
4189 ) ,
4290 ComponentShowcase,
4391 ComponentSource,
44- } ;
92+ } ) ;
4593
46- export default function MDX ( { code } : { code : string } ) {
94+ export default function MDX ( {
95+ code,
96+ type = "doc" ,
97+ } : {
98+ code : string ;
99+ type ?: "doc" | "blog" ;
100+ } ) {
47101 const Content = useMDXComponent ( code ) ;
48102
49- return < Content components = { components } /> ;
103+ return < Content components = { components ( type ) } /> ;
50104}
0 commit comments