From 1a07ff0ffd40f3415d79d375b21f278a9ebfd6ac Mon Sep 17 00:00:00 2001 From: rattatwinko Date: Mon, 23 Jun 2025 14:00:21 +0200 Subject: [PATCH] works now --- src/app/api/admin/posts/route.ts | 4 +- src/app/api/posts/route.ts | 90 ++++++++++++++++++-------------- 2 files changed, 52 insertions(+), 42 deletions(-) diff --git a/src/app/api/admin/posts/route.ts b/src/app/api/admin/posts/route.ts index f475e88..023b4da 100644 --- a/src/app/api/admin/posts/route.ts +++ b/src/app/api/admin/posts/route.ts @@ -50,7 +50,7 @@ export async function POST(request: Request) { export async function GET(request: Request) { // Return the current pinned.json object try { - const pinnedPath = path.join(postsDirectory, 'pinned.json'); + const pinnedPath = path.join(process.cwd(), 'posts', 'pinned.json'); console.log('Reading pinned.json from:', pinnedPath); let pinnedData = { pinned: [], folderEmojis: {} }; if (fs.existsSync(pinnedPath)) { @@ -73,7 +73,7 @@ export async function PATCH(request: Request) { if (!Array.isArray(pinned) || typeof folderEmojis !== 'object') { return NextResponse.json({ error: 'Invalid pinned or folderEmojis data' }, { status: 400 }); } - const pinnedPath = path.join(postsDirectory, 'pinned.json'); + const pinnedPath = path.join(process.cwd(), 'posts', 'pinned.json'); console.log('Saving pinned.json to:', pinnedPath); console.log('Saving data:', { pinned, folderEmojis }); fs.writeFileSync(pinnedPath, JSON.stringify({ pinned, folderEmojis }, null, 2), 'utf8'); diff --git a/src/app/api/posts/route.ts b/src/app/api/posts/route.ts index b9a328d..af5fd5e 100644 --- a/src/app/api/posts/route.ts +++ b/src/app/api/posts/route.ts @@ -12,19 +12,6 @@ import { getPostsDirectory } from '@/lib/postsDirectory'; const postsDirectory = getPostsDirectory(); -const pinnedPath = path.join(postsDirectory, 'pinned.json'); -let pinnedData: { pinned: string[]; folderEmojis: Record } = { pinned: [], folderEmojis: {} }; -if (fs.existsSync(pinnedPath)) { - try { - const raw = fs.readFileSync(pinnedPath, 'utf8'); - pinnedData = JSON.parse(raw); - if (!pinnedData.pinned) pinnedData.pinned = []; - if (!pinnedData.folderEmojis) pinnedData.folderEmojis = {}; - } catch { - pinnedData = { pinned: [], folderEmojis: {} }; - } -} - // Function to get file creation date function getFileCreationDate(filePath: string): Date { const stats = fs.statSync(filePath); @@ -62,7 +49,53 @@ marked.setOptions({ renderer, }); -async function getPostByPath(filePath: string, relPath: string) { +// Replace top-level pinnedData logic with a function +function getPinnedData() { + const pinnedPath = path.join(process.cwd(), 'posts', 'pinned.json'); + let pinnedData = { pinned: [], folderEmojis: {} }; + if (fs.existsSync(pinnedPath)) { + try { + const raw = fs.readFileSync(pinnedPath, 'utf8'); + pinnedData = JSON.parse(raw); + if (!pinnedData.pinned) pinnedData.pinned = []; + if (!pinnedData.folderEmojis) pinnedData.folderEmojis = {}; + } catch { + pinnedData = { pinned: [], folderEmojis: {} }; + } + } + return pinnedData; +} + +// Update readPostsDir to accept pinnedData as an argument +async function readPostsDir(dir: string, relDir = '', pinnedData: { pinned: string[]; folderEmojis: Record }): Promise { + const entries = fs.readdirSync(dir, { withFileTypes: true }); + const folders: any[] = []; + const posts: any[] = []; + + for (const entry of entries) { + const fullPath = path.join(dir, entry.name); + const relPath = relDir ? path.join(relDir, entry.name) : entry.name; + + if (entry.isDirectory()) { + const children = await readPostsDir(fullPath, relPath, pinnedData); + // Debug log for emoji lookup + console.log('[FOLDER EMOJI DEBUG]', { relPath, allEmojis: pinnedData.folderEmojis, emoji: pinnedData.folderEmojis[relPath] }); + const emoji = pinnedData.folderEmojis[relPath] || '📁'; + folders.push({ type: 'folder', name: entry.name, path: relPath, emoji, children }); + } else if (entry.isFile() && entry.name.endsWith('.md')) { + posts.push(await getPostByPath(fullPath, relPath, pinnedData)); + } + } + + // Sort posts by creation date (newest first) + posts.sort((a, b) => new Date(b.createdAt).getTime() - new Date(a.createdAt).getTime()); + + // Folders first, then posts + return [...folders, ...posts]; +} + +// Update getPostByPath to accept pinnedData +async function getPostByPath(filePath: string, relPath: string, pinnedData: { pinned: string[]; folderEmojis: Record }) { const fileContents = fs.readFileSync(filePath, 'utf8'); const { data, content } = matter(fileContents); const createdAt = getFileCreationDate(filePath); @@ -109,34 +142,11 @@ async function getPostByPath(filePath: string, relPath: string) { }; } -async function readPostsDir(dir: string, relDir = ''): Promise { - const entries = fs.readdirSync(dir, { withFileTypes: true }); - const folders: any[] = []; - const posts: any[] = []; - - for (const entry of entries) { - const fullPath = path.join(dir, entry.name); - const relPath = relDir ? path.join(relDir, entry.name) : entry.name; - - if (entry.isDirectory()) { - const children = await readPostsDir(fullPath, relPath); - const emoji = pinnedData.folderEmojis[relPath] || '📁'; - folders.push({ type: 'folder', name: entry.name, path: relPath, emoji, children }); - } else if (entry.isFile() && entry.name.endsWith('.md')) { - posts.push(await getPostByPath(fullPath, relPath)); - } - } - - // Sort posts by creation date (newest first) - posts.sort((a, b) => new Date(b.createdAt).getTime() - new Date(a.createdAt).getTime()); - - // Folders first, then posts - return [...folders, ...posts]; -} - +// Update GET handler to use fresh pinnedData export async function GET() { try { - const tree = await readPostsDir(postsDirectory); + const pinnedData = getPinnedData(); + const tree = await readPostsDir(postsDirectory, '', pinnedData); return NextResponse.json(tree); } catch (error) { console.error('Error loading posts:', error);