|
| 1 | +import fs from "fs"; |
| 2 | +import path from "path"; |
| 3 | +import matter from "gray-matter"; |
| 4 | +import ReactMarkdown from "react-markdown"; |
| 5 | +import remarkGfm from "remark-gfm"; |
| 6 | +import rehypeRaw from "rehype-raw"; |
| 7 | +import rehypeHighlight from "rehype-highlight"; |
| 8 | +import "highlight.js/styles/github-dark.css"; |
| 9 | +import { Metadata } from "next"; |
| 10 | +import { ClockIcon } from "lucide-react"; |
| 11 | + |
| 12 | +const BLOG_DIR = path.join(process.cwd(), "content/blog"); |
| 13 | + |
| 14 | +async function getBlogContent(year: string, slug: string) { |
| 15 | + const filePath = path.join(BLOG_DIR, year, `${slug}.md`); |
| 16 | + const fileContent = fs.readFileSync(filePath, "utf8"); |
| 17 | + const { content, data } = matter(fileContent); |
| 18 | + return { content, meta: data }; |
| 19 | +} |
| 20 | + |
| 21 | +export async function generateMetadata({ |
| 22 | + params, |
| 23 | +}: any): Promise<Metadata> { |
| 24 | + const { year, slug } = await params; |
| 25 | + const { meta } = await getBlogContent(year, slug); |
| 26 | + return { |
| 27 | + title: meta.title, |
| 28 | + description: meta.description || `Read ${meta.title} on our blog`, |
| 29 | + }; |
| 30 | +} |
| 31 | + |
| 32 | +export default async function BlogPage({ params }: any) { |
| 33 | + const { year, slug } = await params; |
| 34 | + const { content, meta } = await getBlogContent(year, slug); |
| 35 | + |
| 36 | + const minuteRead = content.length / 200; |
| 37 | + |
| 38 | + return ( |
| 39 | + <div className="min-h-screen md:py-10 p-2"> |
| 40 | + <article className="max-w-3xl dark:bg-neutral-900 bg-neutral-100 shadow-sm rounded-xl py-6 mx-auto px-4 sm:px-6 lg:px-8"> |
| 41 | + <header className="mb-8"> |
| 42 | + <h1 className="text-3xl sm:text-4xl font-extrabold text-neutral-900 dark:text-white mb-2"> |
| 43 | + {meta.title} |
| 44 | + </h1> |
| 45 | + <div className="flex flex-wrap items-center text-xs text-neutral-500 dark:text-neutral-400 space-x-4"> |
| 46 | + {content && ( |
| 47 | + <div className="flex items-center"> |
| 48 | + <ClockIcon className="w-4 h-4 mr-1" /> |
| 49 | + <span>{minuteRead} min read</span> |
| 50 | + </div> |
| 51 | + )} |
| 52 | + </div> |
| 53 | + </header> |
| 54 | + <div className="prose prose-lg dark:prose-invert max-w-none"> |
| 55 | + <ReactMarkdown |
| 56 | + remarkPlugins={[remarkGfm]} |
| 57 | + rehypePlugins={[rehypeRaw, rehypeHighlight]} |
| 58 | + components={{ |
| 59 | + a: ({ href, children }) => ( |
| 60 | + <a |
| 61 | + href={href} |
| 62 | + className="text-blue-600 dark:text-blue-400 hover:underline" |
| 63 | + target="_blank" |
| 64 | + rel="noopener noreferrer" |
| 65 | + > |
| 66 | + {children} |
| 67 | + </a> |
| 68 | + ), |
| 69 | + img: ({ src, alt }) => ( |
| 70 | + <img |
| 71 | + src={src || ""} |
| 72 | + alt={alt || ""} |
| 73 | + className="rounded-lg shadow-md w-full h-auto" |
| 74 | + /> |
| 75 | + ), |
| 76 | + code: ({ |
| 77 | + node, |
| 78 | + inline, |
| 79 | + className, |
| 80 | + children, |
| 81 | + ...props |
| 82 | + }: { |
| 83 | + node?: any; |
| 84 | + inline?: boolean; |
| 85 | + className?: string; |
| 86 | + children?: React.ReactNode; |
| 87 | + [key: string]: any; |
| 88 | + }) => { |
| 89 | + const match = /language-(\w+)/.exec(className || ""); |
| 90 | + return !inline && match ? ( |
| 91 | + <div className="relative"> |
| 92 | + <pre |
| 93 | + className={`${className} rounded-lg p-4 overflow-x-auto`} |
| 94 | + > |
| 95 | + <code className={`language-${match[1]}`} {...props}> |
| 96 | + {children} |
| 97 | + </code> |
| 98 | + </pre> |
| 99 | + <div className="absolute top-2 right-2 text-xs text-neutral-400 dark:text-neutral-500 uppercase"> |
| 100 | + {match[1]} |
| 101 | + </div> |
| 102 | + </div> |
| 103 | + ) : ( |
| 104 | + <code |
| 105 | + className="bg-neutral-100 dark:bg-neutral-800 rounded px-1 py-0.5" |
| 106 | + {...props} |
| 107 | + > |
| 108 | + {children} |
| 109 | + </code> |
| 110 | + ); |
| 111 | + }, |
| 112 | + blockquote: ({ children }) => ( |
| 113 | + <blockquote className="border-l-4 border-blue-500 pl-4 italic"> |
| 114 | + {children} |
| 115 | + </blockquote> |
| 116 | + ), |
| 117 | + }} |
| 118 | + > |
| 119 | + {content} |
| 120 | + </ReactMarkdown> |
| 121 | + </div> |
| 122 | + </article> |
| 123 | + </div> |
| 124 | + ); |
| 125 | +} |
0 commit comments