Blog works

This commit is contained in:
Timothy Pidashev
2024-06-05 18:41:29 -07:00
parent 4c97f4f52d
commit f96629a6b4
9 changed files with 1495 additions and 117 deletions

33
src/lib/mdx.js Normal file
View File

@@ -0,0 +1,33 @@
import fs from "fs";
import path from "path";
import { compileMDX } from "next-mdx-remote/rsc";
import rehypeHighlight from "rehype-highlight";
import remarkGfm from "remark-gfm";
const contentDir = path.join(process.cwd(), "app/blog/posts");
export async function getBlogBySlug(slug) {
const filePath = path.join(contentDir, `${slug}.mdx`);
const source = fs.readFileSync(filePath, "utf8");
const { frontmatter, content } = await compileMDX({
source,
options: {
parseFrontmatter: true,
mdxOptions: {
remarkPlugins: [remarkGfm],
rehypePlugins: [rehypeHighlight],
},
},
});
console.log("Frontmatter:", frontmatter); // Debug log
console.log("Content:", content); // Debug log
return { frontmatter, content, slug };
}
export async function getAllBlogSlugs() {
const files = fs.readdirSync(contentDir);
return files.map(file => ({
slug: path.parse(file).name,
}));
}