|
| 1 | +import fs from "node:fs/promises"; |
| 2 | +import path from "node:path"; |
| 3 | +import type { Metadata } from "next"; |
| 4 | +import { notFound } from "next/navigation"; |
| 5 | +import { BlogMetadata } from "@/src/components/BlogMetadata"; |
| 6 | +import { BlogTags } from "@/src/components/BlogTags"; |
| 7 | +import { BlogTeaserGrid } from "@/src/components/BlogTeaserGrid"; |
| 8 | +import { Pagination } from "@/src/design-system/Pagination"; |
| 9 | +import { SimilarPosts } from "@/src/components/SimilarPosts"; |
| 10 | +import { Typography } from "@/src/design-system/Typography"; |
| 11 | +import { paginate, POSTS_PER_PAGE } from "@/src/helpers/blogPaging"; |
| 12 | +import { |
| 13 | + BLOG_ROOT, |
| 14 | + listBlogPosts, |
| 15 | + resolveBlogFile, |
| 16 | +} from "@/src/helpers/blogPaths"; |
| 17 | +import { findSimilarPosts, listBlogPostSummaries } from "@/src/helpers/blogPosts"; |
| 18 | +import { compileDoc } from "@/src/helpers/compileDoc"; |
| 19 | +import { readFrontmatter } from "@/src/helpers/readFrontmatter"; |
| 20 | +import { estimateReadingTime } from "@/src/helpers/readingTime"; |
| 21 | + |
| 22 | +type BlogFrontmatter = { |
| 23 | + title?: string; |
| 24 | + description?: string; |
| 25 | + author?: string; |
| 26 | + authorUrl?: string; |
| 27 | + authorImageUrl?: string; |
| 28 | + date?: string; |
| 29 | + tags?: string[]; |
| 30 | +}; |
| 31 | + |
| 32 | +type Params = { slug: string[] }; |
| 33 | +type PageProps = { params: Promise<Params> }; |
| 34 | + |
| 35 | +export const dynamicParams = false; |
| 36 | + |
| 37 | +export function generateStaticParams(): Params[] { |
| 38 | + const postParams = listBlogPosts().map<Params>(({ parsed }) => ({ |
| 39 | + slug: [parsed.year, parsed.month, parsed.day, parsed.slug], |
| 40 | + })); |
| 41 | + |
| 42 | + const summaries = listBlogPostSummaries(); |
| 43 | + const totalPages = Math.max( |
| 44 | + 1, |
| 45 | + Math.ceil(summaries.length / POSTS_PER_PAGE), |
| 46 | + ); |
| 47 | + const pageParams: Params[] = []; |
| 48 | + for (let p = 2; p <= totalPages; p++) { |
| 49 | + pageParams.push({ slug: [String(p)] }); |
| 50 | + } |
| 51 | + |
| 52 | + const params = [...postParams, ...pageParams]; |
| 53 | + // output: export requires at least one prerendered path; placeholder |
| 54 | + // renders 404 via notFound() when no content is present. |
| 55 | + return params.length > 0 ? params : [{ slug: ["__empty__"] }]; |
| 56 | +} |
| 57 | + |
| 58 | +export async function generateMetadata({ |
| 59 | + params, |
| 60 | +}: PageProps): Promise<Metadata> { |
| 61 | + const { slug } = await params; |
| 62 | + if (isPaginationSlug(slug)) { |
| 63 | + return { title: "Blog" }; |
| 64 | + } |
| 65 | + const rel = resolveBlogFile(slug); |
| 66 | + if (rel === null) { |
| 67 | + return {}; |
| 68 | + } |
| 69 | + const { title, description } = readFrontmatter(path.join(BLOG_ROOT, rel)); |
| 70 | + return { |
| 71 | + title, |
| 72 | + description, |
| 73 | + }; |
| 74 | +} |
| 75 | + |
| 76 | +export default async function BlogSlugPage({ params }: PageProps) { |
| 77 | + const { slug } = await params; |
| 78 | + |
| 79 | + if (isPaginationSlug(slug)) { |
| 80 | + return renderPagination(Number(slug[0])); |
| 81 | + } |
| 82 | + |
| 83 | + const rel = resolveBlogFile(slug); |
| 84 | + if (rel === null) { |
| 85 | + notFound(); |
| 86 | + } |
| 87 | + |
| 88 | + const absPath = path.join(BLOG_ROOT, rel); |
| 89 | + const [{ content, frontmatter }, raw] = await Promise.all([ |
| 90 | + compileDoc<BlogFrontmatter>(absPath), |
| 91 | + fs.readFile(absPath, "utf-8"), |
| 92 | + ]); |
| 93 | + const readingTime = estimateReadingTime(raw).text; |
| 94 | + |
| 95 | + const summaries = listBlogPostSummaries(); |
| 96 | + const stem = `${slug[0]}-${slug[1]}-${slug[2]}-${slug.slice(3).join("/")}`; |
| 97 | + const current = summaries.find((s) => s.stem === stem); |
| 98 | + const similar = current ? findSimilarPosts(current, summaries) : []; |
| 99 | + const featuredImage = current?.featuredImage ?? null; |
| 100 | + |
| 101 | + return ( |
| 102 | + <main className="px-5 py-8 sm:px-12"> |
| 103 | + <article className="mx-auto max-w-5xl"> |
| 104 | + {featuredImage ? ( |
| 105 | + // eslint-disable-next-line @next/next/no-img-element |
| 106 | + <img |
| 107 | + src={featuredImage} |
| 108 | + alt="" |
| 109 | + loading="eager" |
| 110 | + decoding="async" |
| 111 | + className="mb-6 aspect-[16/9] w-full rounded-lg object-cover" |
| 112 | + /> |
| 113 | + ) : null} |
| 114 | + {frontmatter.title ? ( |
| 115 | + <Typography variant="h1">{frontmatter.title}</Typography> |
| 116 | + ) : null} |
| 117 | + <BlogMetadata |
| 118 | + author={frontmatter.author} |
| 119 | + authorUrl={frontmatter.authorUrl} |
| 120 | + authorImageUrl={frontmatter.authorImageUrl} |
| 121 | + date={frontmatter.date} |
| 122 | + readingTime={readingTime} |
| 123 | + /> |
| 124 | + <BlogTags tags={frontmatter.tags} /> |
| 125 | + {content} |
| 126 | + <SimilarPosts posts={similar} /> |
| 127 | + </article> |
| 128 | + </main> |
| 129 | + ); |
| 130 | +} |
| 131 | + |
| 132 | +function isPaginationSlug(slug: string[]): boolean { |
| 133 | + return slug.length === 1 && /^\d+$/.test(slug[0]); |
| 134 | +} |
| 135 | + |
| 136 | +function renderPagination(pageNum: number) { |
| 137 | + if (!Number.isInteger(pageNum) || pageNum < 2) { |
| 138 | + notFound(); |
| 139 | + } |
| 140 | + const slice = paginate(listBlogPostSummaries(), pageNum); |
| 141 | + if (slice === null) { |
| 142 | + notFound(); |
| 143 | + } |
| 144 | + |
| 145 | + return ( |
| 146 | + <div className="px-5 py-8 sm:px-12"> |
| 147 | + <div className="mx-auto flex max-w-6xl flex-col gap-6"> |
| 148 | + <Typography variant="h1">Blog</Typography> |
| 149 | + <BlogTeaserGrid posts={slice.posts} /> |
| 150 | + <Pagination |
| 151 | + currentPage={slice.currentPage} |
| 152 | + totalPages={slice.totalPages} |
| 153 | + hrefForPage={(p) => (p === 1 ? "/blog" : `/blog/${p}`)} |
| 154 | + /> |
| 155 | + </div> |
| 156 | + </div> |
| 157 | + ); |
| 158 | +} |
0 commit comments