feat: update homepage to show posts grouped by category

This commit is contained in:
Lewis Wynne 2026-01-23 19:22:21 +00:00
parent 6ef5b3a413
commit 6497f485e1

View file

@ -19,11 +19,26 @@ interface TxtFile {
} }
const posts = await getCollection('posts'); const posts = await getCollection('posts');
const sorted = posts.sort((a, b) => {
if (a.data.pinned && !b.data.pinned) return -1; // Group by category (default: "posts")
if (!a.data.pinned && b.data.pinned) return 1; const grouped = posts.reduce((acc, post) => {
return b.data.date.getTime() - a.data.date.getTime(); const category = post.data.category ?? 'posts';
}); if (!acc[category]) acc[category] = [];
acc[category].push(post);
return acc;
}, {} as Record<string, typeof posts>);
// Sort categories alphabetically
const sortedCategories = Object.keys(grouped).sort();
// Sort posts within each category: pinned first, then by date descending
for (const category of sortedCategories) {
grouped[category].sort((a, b) => {
if (a.data.pinned && !b.data.pinned) return -1;
if (!a.data.pinned && b.data.pinned) return 1;
return b.data.date.getTime() - a.data.date.getTime();
});
}
const bookmarks = (yaml.load(bookmarksRaw) as Bookmark[]) const bookmarks = (yaml.load(bookmarksRaw) as Bookmark[])
.sort((a, b) => new Date(b.date).getTime() - new Date(a.date).getTime()); .sort((a, b) => new Date(b.date).getTime() - new Date(a.date).getTime());
@ -70,13 +85,18 @@ function extractDomain(url: string): string {
--- ---
<Layout title="lewis m.w." isHome> <Layout title="lewis m.w." isHome>
{sortedCategories.map(category => {
const categoryPosts = grouped[category];
return (
<details open> <details open>
<summary>blog</summary> <summary>{category}</summary>
<pre set:html={[ <pre set:html={[
...sorted.slice(0, 10).map(post => `<span class="muted">${formatDate(post.data.date)}</span> <a href="/blog/${post.id}">${post.data.title}</a>${post.data.pinned ? ' [pinned]' : ''}`), ...categoryPosts.slice(0, 10).map(post => `<span class="muted">${formatDate(post.data.date)}</span> <a href="/md/${post.id}">${post.data.title}</a>${post.data.pinned ? ' [pinned]' : ''}`),
...(sorted.length > 10 ? [`<a href="/blog/">+${sorted.length - 10} more</a>`] : []) ...(categoryPosts.length > 10 ? [`<a href="/md/">+${categoryPosts.length - 10} more</a>`] : [])
].join('\n')} /> ].join('\n')} />
</details> </details>
);
})}
<details open> <details open>
<summary>txt</summary> <summary>txt</summary>