FIX: Posts now render in reverse chronological order

This commit is contained in:
Caleb Braaten 2026-01-09 16:41:32 -08:00
parent d7fb16e24e
commit b0fd1b6d9e

View File

@ -16,10 +16,11 @@ export function addToDatabase(filePath: string, data: { [key: string]: any }, co
const { title, date, readingTime, tags, excerpt } = data; const { title, date, readingTime, tags, excerpt } = data;
// Convert all values to strings or null explicitly (except tags) // Convert all values to strings or null explicitly (except tags)
// Ensure date is stored in ISO format for consistent sorting
const values = [ const values = [
filePath ? String(filePath) : null, filePath ? String(filePath) : null,
title ? String(title) : 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, readingTime ? String(readingTime) : null,
excerpt ? String(excerpt) : null, excerpt ? String(excerpt) : null,
content ? String(content) : null content ? String(content) : null
@ -156,7 +157,9 @@ export function calculateReadTime(content: string): number {
// Helper function to format date for display // Helper function to format date for display
export function formatDate(dateString: string): string { 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', { return date.toLocaleDateString('en-US', {
year: 'numeric', year: 'numeric',
month: 'long', month: 'long',
@ -170,7 +173,7 @@ export function getAdjacentPosts(currentPostPath: string) {
SELECT path, title, date SELECT path, title, date
FROM posts FROM posts
WHERE path NOT LIKE '%.md' WHERE path NOT LIKE '%.md'
ORDER BY date ASC ORDER BY date DESC
`); `);
const allPosts = allPostsQuery.all() as any[]; const allPosts = allPostsQuery.all() as any[];
@ -183,10 +186,10 @@ export function getAdjacentPosts(currentPostPath: string) {
return { previousPost: null, nextPost: null }; 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; let previousPost = null;
if (currentIndex < allPosts.length - 1) { if (currentIndex > 0) {
const prevPost = allPosts[currentIndex + 1]; const prevPost = allPosts[currentIndex - 1];
// Clean up the path to match the URL structure // Clean up the path to match the URL structure
const cleanPath = prevPost.path.replace(/^.*\/content\//, '/').replace(/\.md$/, ''); const cleanPath = prevPost.path.replace(/^.*\/content\//, '/').replace(/\.md$/, '');
previousPost = { 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; let nextPost = null;
if (currentIndex > 0) { if (currentIndex < allPosts.length - 1) {
const post = allPosts[currentIndex - 1]; const post = allPosts[currentIndex + 1];
// Clean up the path to match the URL structure // Clean up the path to match the URL structure
const cleanPath = post.path.replace(/^.*\/content\//, '/').replace(/\.md$/, ''); const cleanPath = post.path.replace(/^.*\/content\//, '/').replace(/\.md$/, '');
nextPost = { nextPost = {