Refactor: remove PayloadCMS, use MDX for content and TSX for design, use direct email for contact
Some checks failed
Some checks failed
This commit is contained in:
@@ -1,58 +1,37 @@
|
||||
import { getPayload } from "payload";
|
||||
import configPromise from "@payload-config";
|
||||
import fs from "fs";
|
||||
import path from "path";
|
||||
import matter from "gray-matter";
|
||||
|
||||
const POSTS_DIR = path.join(process.cwd(), "src/content/posts");
|
||||
|
||||
export async function getAllPosts() {
|
||||
if (!process.env.DATABASE_URI && !process.env.POSTGRES_URI) {
|
||||
console.warn(
|
||||
"⚠️ Bypassing Payload fetch during Next.js build: DATABASE_URI is missing.",
|
||||
);
|
||||
return [];
|
||||
}
|
||||
|
||||
try {
|
||||
const payload = await getPayload({ config: configPromise });
|
||||
const { docs } = await payload.find({
|
||||
collection: "posts",
|
||||
limit: 1000,
|
||||
sort: "-date",
|
||||
where: {
|
||||
and: [
|
||||
{
|
||||
_status: {
|
||||
equals: "published",
|
||||
},
|
||||
},
|
||||
{
|
||||
date: {
|
||||
less_than_equal: new Date(),
|
||||
},
|
||||
},
|
||||
],
|
||||
},
|
||||
});
|
||||
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 docs.map((doc) => ({
|
||||
title: doc.title as string,
|
||||
description: doc.description as string,
|
||||
date: doc.date as string,
|
||||
tags: (doc.tags || []).map((t) =>
|
||||
typeof t === "object" && t !== null ? t.tag : t,
|
||||
) as string[],
|
||||
slug: doc.slug as string,
|
||||
thumbnail:
|
||||
(doc.featuredImage &&
|
||||
typeof doc.featuredImage === "object" &&
|
||||
doc.featuredImage.url
|
||||
? doc.featuredImage.url
|
||||
: "") || "",
|
||||
body: { code: "" as string },
|
||||
lexicalContent: doc.content || null,
|
||||
}));
|
||||
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.warn(
|
||||
"⚠️ Bypassing Payload fetch during build: Database connection refused.",
|
||||
error,
|
||||
);
|
||||
console.error("Error reading MDX posts:", error);
|
||||
return [];
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user