1- import fs from "fs" ;
2- import path from "path" ;
31import matter from "gray-matter" ;
42import type { Metadata } from "next" ;
53import Image from "next/image" ;
64import Link from "next/link" ;
75import { ArticleBody } from "@/components/content/article-body" ;
86import { formatDate } from "@/components/content/format-date" ;
97import { Button , Eyebrow , container } from "@/components/site/page-kit" ;
8+ import { contentSlugs , readContent } from "@/lib/site-files" ;
109import { SITE_NAME , blogPostingJsonLd , breadcrumbJsonLd , excerptOf , jsonLdString , pageTitle , snippetOf , withSocial } from "@/lib/seo" ;
1110
1211const backLink =
@@ -17,12 +16,7 @@ const backLink =
1716export const dynamicParams = false ;
1817
1918export function generateStaticParams ( ) {
20- const dir = path . join ( process . cwd ( ) , "content" , "blog" ) ;
21- if ( ! fs . existsSync ( dir ) ) return [ ] ;
22- return fs
23- . readdirSync ( dir )
24- . filter ( ( f ) => f . endsWith ( ".md" ) )
25- . map ( ( f ) => ( { slug : f . replace ( / \. m d $ / , "" ) } ) ) ;
19+ return contentSlugs ( "blog" ) . map ( ( slug ) => ( { slug } ) ) ;
2620}
2721
2822export async function generateMetadata ( {
@@ -31,9 +25,9 @@ export async function generateMetadata({
3125 params : Promise < { slug : string } > ;
3226} ) : Promise < Metadata > {
3327 const { slug } = await params ;
34- const filePath = path . join ( process . cwd ( ) , "content" , " blog", ` ${ slug } .md` ) ;
35- if ( ! fs . existsSync ( filePath ) ) return { title : "Post not found" , robots : { index : false } } ;
36- const { data, content } = matter ( fs . readFileSync ( filePath , "utf-8" ) ) ;
28+ const raw = readContent ( " blog", slug ) ;
29+ if ( raw === undefined ) return { title : "Post not found" , robots : { index : false } } ;
30+ const { data, content } = matter ( raw ) ;
3731 const date = data . date ? new Date ( data . date ) : undefined ;
3832 const description = snippetOf ( data . description ) ?? excerptOf ( content ) ;
3933 return withSocial ( {
@@ -62,14 +56,9 @@ export default async function BlogPost({
6256} ) {
6357 const { slug } = await params ;
6458
65- const filePath = path . join (
66- process . cwd ( ) ,
67- "content" ,
68- "blog" ,
69- `${ slug } .md`
70- ) ;
59+ const fileContent = readContent ( "blog" , slug ) ;
7160
72- if ( ! fs . existsSync ( filePath ) ) {
61+ if ( fileContent === undefined ) {
7362 return (
7463 < main className = "bg-white" >
7564 < div className = { `${ container } py-24 md:py-32` } >
@@ -88,7 +77,6 @@ export default async function BlogPost({
8877 ) ;
8978 }
9079
91- const fileContent = fs . readFileSync ( filePath , "utf-8" ) ;
9280 const { data, content } = matter ( fileContent ) ;
9381 const date = formatDate ( data . date instanceof Date ? data . date . toISOString ( ) : data . date ) ;
9482
0 commit comments