diff --git a/posts/bootcamp.mdx b/posts/2021/bootcamp.mdx similarity index 100% rename from posts/bootcamp.mdx rename to posts/2021/bootcamp.mdx diff --git a/posts/2s12mod.mdx b/posts/2023/2s12mod.mdx similarity index 100% rename from posts/2s12mod.mdx rename to posts/2023/2s12mod.mdx diff --git a/posts/amazon.mdx b/posts/2023/amazon.mdx similarity index 100% rename from posts/amazon.mdx rename to posts/2023/amazon.mdx diff --git a/posts/atomic-habits-review.mdx b/posts/2023/atomic-habits-review.mdx similarity index 100% rename from posts/atomic-habits-review.mdx rename to posts/2023/atomic-habits-review.mdx diff --git a/posts/cat.mdx b/posts/2023/cat.mdx similarity index 100% rename from posts/cat.mdx rename to posts/2023/cat.mdx diff --git a/posts/doet-review.mdx b/posts/2023/doet-review.mdx similarity index 100% rename from posts/doet-review.mdx rename to posts/2023/doet-review.mdx diff --git a/posts/think-python.mdx b/posts/2023/think-python.mdx similarity index 100% rename from posts/think-python.mdx rename to posts/2023/think-python.mdx diff --git a/posts/building-blog.mdx b/posts/2024/building-blog.mdx similarity index 100% rename from posts/building-blog.mdx rename to posts/2024/building-blog.mdx diff --git a/posts/ddia.mdx b/posts/2024/ddia.mdx similarity index 100% rename from posts/ddia.mdx rename to posts/2024/ddia.mdx diff --git a/posts/books.mdx b/posts/archive/books.mdx similarity index 94% rename from posts/books.mdx rename to posts/archive/books.mdx index e38105f..f6320c5 100644 --- a/posts/books.mdx +++ b/posts/archive/books.mdx @@ -6,7 +6,6 @@ archive: true books to read -- DDIA - eloquent JS - haverbeke - js good parts - crockford diff --git a/posts/productivity-tools.mdx b/posts/archive/productivity-tools.mdx similarity index 100% rename from posts/productivity-tools.mdx rename to posts/archive/productivity-tools.mdx diff --git a/src/app/blog/functions.tsx b/src/app/blog/functions.tsx index dc8708e..68aaf28 100644 --- a/src/app/blog/functions.tsx +++ b/src/app/blog/functions.tsx @@ -3,13 +3,31 @@ import fs from 'fs/promises'; import matter from 'gray-matter'; import path from 'path'; +// Helper function to recursively read all files in a directory +async function readDirRecursive(dir: string): Promise { + let results: string[] = []; + const list = await fs.readdir(dir, { withFileTypes: true }); + for (const file of list) { + const filePath = path.join(dir, file.name); + if (file.isDirectory()) { + // Recursively read subdirectories + results = results.concat(await readDirRecursive(filePath)); + } else if (file.isFile() && file.name.endsWith('.mdx')) { + // Only include .mdx files + results.push(filePath); + } + } + return results; +} + export const getPosts = cache(async (returnArchive = false) => { - const posts = await fs.readdir(path.join(process.cwd(), 'posts')); + const postDir = path.join(process.cwd(), 'posts'); + const postFiles = await readDirRecursive(postDir); - const postPromises = posts.map(async (post) => { - const postContent = await fs.readFile(`./posts/${post}`, 'utf-8'); + const postPromises = postFiles.map(async (filePath) => { + const postContent = await fs.readFile(filePath, 'utf-8'); const { data, content } = matter(postContent); - const id = post.replace(/\.mdx$/, ''); + const id = path.basename(filePath, '.mdx'); // Use only the file name as the slug return { ...data, @@ -32,6 +50,12 @@ export const getPosts = cache(async (returnArchive = false) => { export const getPost = cache( async (slug: string, returnArchive: boolean = false) => { const posts = await getPosts(returnArchive); - return posts.find((post) => post.id === slug); + const post = posts.find((post) => post.id === slug); + + if (!post) { + console.log(`Post not found for slug: ${slug}`); // Debug log + } + + return post; } );