38 lines
1.1 KiB
TypeScript
38 lines
1.1 KiB
TypeScript
import fs from "fs";
|
|
import path from "path";
|
|
import matter from "gray-matter";
|
|
|
|
const POSTS_DIR = path.join(process.cwd(), "content/blog");
|
|
|
|
export async function getAllPosts() {
|
|
try {
|
|
if (!fs.existsSync(POSTS_DIR)) return [];
|
|
|
|
const files = fs.readdirSync(POSTS_DIR);
|
|
const posts = files
|
|
.filter((filename) => filename.endsWith(".mdx"))
|
|
.map((filename) => {
|
|
const filePath = path.join(POSTS_DIR, filename);
|
|
const fileContent = fs.readFileSync(filePath, "utf-8");
|
|
const { data, content } = matter(fileContent);
|
|
|
|
return {
|
|
title: data.title || "",
|
|
description: data.description || "",
|
|
date: data.date || new Date().toISOString(),
|
|
tags: data.tags || [],
|
|
slug: filename.replace(/\.mdx$/, ""),
|
|
thumbnail: data.thumbnail || "",
|
|
body: { code: "" },
|
|
lexicalContent: null,
|
|
mdxContent: content,
|
|
};
|
|
});
|
|
|
|
return posts.sort((a, b) => new Date(b.date).getTime() - new Date(a.date).getTime());
|
|
} catch (error) {
|
|
console.error("Error reading MDX posts:", error);
|
|
return [];
|
|
}
|
|
}
|