Skip to content

Commit

Permalink
organize
Browse files Browse the repository at this point in the history
  • Loading branch information
adnjoo committed May 26, 2024
1 parent 08f8fa3 commit 48d5782
Show file tree
Hide file tree
Showing 12 changed files with 29 additions and 6 deletions.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
1 change: 0 additions & 1 deletion posts/books.mdx → posts/archive/books.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ archive: true

books to read

- DDIA
- eloquent JS - haverbeke
- js good parts - crockford

File renamed without changes.
34 changes: 29 additions & 5 deletions src/app/blog/functions.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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<string[]> {
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,
Expand All @@ -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;
}
);

0 comments on commit 48d5782

Please sign in to comment.