start of landing page, needs 2 intro paragraphs & links, also images
This commit is contained in:
45
src/lib/posts.ts
Normal file
45
src/lib/posts.ts
Normal file
@@ -0,0 +1,45 @@
|
||||
import fs from "fs"
|
||||
import path from "path"
|
||||
import matter from "gray-matter"
|
||||
|
||||
export interface Post {
|
||||
slug: string
|
||||
title: string
|
||||
date: string
|
||||
excerpt?: string
|
||||
content: string
|
||||
}
|
||||
|
||||
const postsDirectory = path.join(process.cwd(), "posts")
|
||||
|
||||
export async function getPost(slug: string): Promise<Post | null> {
|
||||
try {
|
||||
const fullPath = path.join(postsDirectory, `${slug}.md`)
|
||||
|
||||
// Try .md first, then .mdx
|
||||
let fileContents: string
|
||||
if (fs.existsSync(fullPath)) {
|
||||
fileContents = fs.readFileSync(fullPath, "utf8")
|
||||
} else {
|
||||
const mdxPath = path.join(postsDirectory, `${slug}.mdx`)
|
||||
if (fs.existsSync(mdxPath)) {
|
||||
fileContents = fs.readFileSync(mdxPath, "utf8")
|
||||
} else {
|
||||
return null
|
||||
}
|
||||
}
|
||||
|
||||
const { data, content } = matter(fileContents)
|
||||
|
||||
return {
|
||||
slug,
|
||||
title: data.title || slug,
|
||||
date: data.date || "",
|
||||
excerpt: data.excerpt || "",
|
||||
content,
|
||||
}
|
||||
} catch (error) {
|
||||
console.error("Error reading post:", error)
|
||||
return null
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user