|
| 1 | +import * as React from "react" |
| 2 | + |
| 3 | +import type { MDXComponents } from "mdx/types" |
| 4 | +import Link from "next/link" |
| 5 | +import { codeToHtml } from "shiki" |
| 6 | + |
| 7 | +import classNames from "@/utils/classNames" |
| 8 | + |
| 9 | +interface Props { |
| 10 | + children: React.ReactNode |
| 11 | +} |
| 12 | + |
| 13 | +const BlogWrapper: React.FC<Props> = ({ children }) => { |
| 14 | + return ( |
| 15 | + <main className="mx-auto my-24 w-full max-w-[680px] px-4 md:px-0"> |
| 16 | + <article |
| 17 | + className={classNames( |
| 18 | + "prose prose-zinc prose-img:mx-auto", |
| 19 | + "prose-p:opacity-85 prose-blockquote:opacity-85", |
| 20 | + "prose-headings:opacity-85 prose-headings:font-semibold prose-headings:tracking-tight", |
| 21 | + "prose-h1:text-3xl prose-h1:leading-snug", |
| 22 | + "prose-pre:p-0 prose-pre:bg-[#20252B] prose-code:text-sm prose-pre:px-3", |
| 23 | + "prose-table:rounded-xl prose-table:overflow-hidden", |
| 24 | + "prose-thead:border-zinc-300 prose-th:bg-zinc-200 prose-tr:bg-zinc-100 prose-tr:border-zinc-300 ", |
| 25 | + "prose-th:py-3.5 prose-th:px-3 prose-td:py-3.5 prose-td:px-3" |
| 26 | + )} |
| 27 | + > |
| 28 | + {children} |
| 29 | + </article> |
| 30 | + </main> |
| 31 | + ) |
| 32 | +} |
| 33 | + |
| 34 | +const components: MDXComponents = { |
| 35 | + code: async ({ |
| 36 | + className = "", |
| 37 | + children, |
| 38 | + ...props |
| 39 | + }: React.ComponentPropsWithoutRef<"code">) => { |
| 40 | + const isInline = !className.includes("language-") |
| 41 | + |
| 42 | + const codeHTML = await codeToHtml(children as string, { |
| 43 | + lang: className != null ? className.replace(/language-/, "") : "text", |
| 44 | + theme: "plastic", |
| 45 | + }) |
| 46 | + |
| 47 | + if (isInline) { |
| 48 | + return ( |
| 49 | + <code |
| 50 | + className="rounded bg-zinc-100 px-1.5 py-0.5 font-semibold text-blue-600 before:hidden after:hidden" |
| 51 | + {...props} |
| 52 | + > |
| 53 | + {(children as string).replaceAll("`", "")} |
| 54 | + </code> |
| 55 | + ) |
| 56 | + } else { |
| 57 | + return <code dangerouslySetInnerHTML={{ __html: codeHTML }} {...props} /> |
| 58 | + } |
| 59 | + }, |
| 60 | + a: ({ |
| 61 | + href = "", |
| 62 | + children, |
| 63 | + ...props |
| 64 | + }: React.ComponentPropsWithoutRef<"a">) => { |
| 65 | + if (href.startsWith("/")) { |
| 66 | + return ( |
| 67 | + <Link |
| 68 | + href={href} |
| 69 | + className={"underline underline-offset-2 hover:no-underline"} |
| 70 | + {...props} |
| 71 | + > |
| 72 | + {children} |
| 73 | + </Link> |
| 74 | + ) |
| 75 | + } |
| 76 | + if (href.startsWith("#")) { |
| 77 | + return ( |
| 78 | + <a |
| 79 | + href={href} |
| 80 | + className={"underline underline-offset-2 hover:no-underline"} |
| 81 | + {...props} |
| 82 | + > |
| 83 | + {children} |
| 84 | + </a> |
| 85 | + ) |
| 86 | + } |
| 87 | + return ( |
| 88 | + <a |
| 89 | + href={href} |
| 90 | + target="_blank" |
| 91 | + rel="noopener noreferrer" |
| 92 | + className={"underline underline-offset-2 hover:no-underline"} |
| 93 | + {...props} |
| 94 | + > |
| 95 | + {children} |
| 96 | + </a> |
| 97 | + ) |
| 98 | + }, |
| 99 | +} |
| 100 | + |
| 101 | +export function useMDXComponents( |
| 102 | + otherComponents: MDXComponents |
| 103 | +): MDXComponents { |
| 104 | + return { |
| 105 | + ...otherComponents, |
| 106 | + ...components, |
| 107 | + wrapper: BlogWrapper, |
| 108 | + } |
| 109 | +} |
0 commit comments