From b0fd1b6d9eec3592d217d28666171aee0784ae40 Mon Sep 17 00:00:00 2001 From: Caleb Braaten Date: Fri, 9 Jan 2026 16:41:32 -0800 Subject: [PATCH] FIX: Posts now render in reverse chronological order --- src/db/posts.ts | 21 ++++++++++++--------- 1 file changed, 12 insertions(+), 9 deletions(-) diff --git a/src/db/posts.ts b/src/db/posts.ts index 6247a54..bc80a64 100644 --- a/src/db/posts.ts +++ b/src/db/posts.ts @@ -16,10 +16,11 @@ export function addToDatabase(filePath: string, data: { [key: string]: any }, co const { title, date, readingTime, tags, excerpt } = data; // Convert all values to strings or null explicitly (except tags) + // Ensure date is stored in ISO format for consistent sorting const values = [ filePath ? String(filePath) : null, title ? String(title) : null, - date ? String(date) : null, + date ? (date instanceof Date ? date.toISOString().split('T')[0] : String(date)) : null, readingTime ? String(readingTime) : null, excerpt ? String(excerpt) : null, content ? String(content) : null @@ -156,7 +157,9 @@ export function calculateReadTime(content: string): number { // Helper function to format date for display export function formatDate(dateString: string): string { - const date = new Date(dateString); + // Parse ISO date string (YYYY-MM-DD) + const date = new Date(dateString + 'T00:00:00'); + return date.toLocaleDateString('en-US', { year: 'numeric', month: 'long', @@ -170,7 +173,7 @@ export function getAdjacentPosts(currentPostPath: string) { SELECT path, title, date FROM posts WHERE path NOT LIKE '%.md' - ORDER BY date ASC + ORDER BY date DESC `); const allPosts = allPostsQuery.all() as any[]; @@ -183,10 +186,10 @@ export function getAdjacentPosts(currentPostPath: string) { return { previousPost: null, nextPost: null }; } - // Get previous post (newer date) + // Get previous post (newer post, which comes before current in reverse chronological order) let previousPost = null; - if (currentIndex < allPosts.length - 1) { - const prevPost = allPosts[currentIndex + 1]; + if (currentIndex > 0) { + const prevPost = allPosts[currentIndex - 1]; // Clean up the path to match the URL structure const cleanPath = prevPost.path.replace(/^.*\/content\//, '/').replace(/\.md$/, ''); previousPost = { @@ -195,10 +198,10 @@ export function getAdjacentPosts(currentPostPath: string) { }; } - // Get next post (older date) + // Get next post (older post, which comes after current in reverse chronological order) let nextPost = null; - if (currentIndex > 0) { - const post = allPosts[currentIndex - 1]; + if (currentIndex < allPosts.length - 1) { + const post = allPosts[currentIndex + 1]; // Clean up the path to match the URL structure const cleanPath = post.path.replace(/^.*\/content\//, '/').replace(/\.md$/, ''); nextPost = {