From eda7d3aa00b8784de56aaa4dcdc3ae3816bc6ae2 Mon Sep 17 00:00:00 2001 From: Joseph Montanaro Date: Sun, 5 Apr 2026 06:40:18 -0400 Subject: [PATCH] extract post-listing logic --- src/lib/content.ts | 10 ++++++++++ src/pages/[slug].astro | 11 +++++------ src/pages/index.astro | 5 ++--- src/pages/posts.astro | 6 +++--- 4 files changed, 20 insertions(+), 12 deletions(-) create mode 100644 src/lib/content.ts diff --git a/src/lib/content.ts b/src/lib/content.ts new file mode 100644 index 0000000..38a2182 --- /dev/null +++ b/src/lib/content.ts @@ -0,0 +1,10 @@ +import { getCollection } from 'astro:content'; + + +export async function listPosts() { + // all posts in dev, exlucde draft posts in prod + let entries = await getCollection('posts', ({ data }) => import.meta.env.DEV || !data.draft); + // sort by date descending + entries.sort((a, b) => b.data.date.getTime() - a.data.date.getTime()); + return entries; +} diff --git a/src/pages/[slug].astro b/src/pages/[slug].astro index b9132a7..1a41823 100644 --- a/src/pages/[slug].astro +++ b/src/pages/[slug].astro @@ -1,18 +1,17 @@ --- -import { getCollection } from 'astro:content'; +import { listPosts } from '@lib/content.ts'; import BaseLayout from '@layouts/BaseLayout.astro'; import Post from '@components/Post.astro'; export async function getStaticPaths() { - const entries = await getCollection('posts'); - // unlike `/index` and `/posts`, we want to sort ascending by date here - entries.sort((a, b) => a.data.date.getTime() - b.data.date.getTime()); + const entries = await listPosts(); // for each route, the page gets passed the entry itself, plus the previous and next slugs // (if any), so that it can render links to them return entries.map((entry, idx) => { - const prevSlug = entries[idx - 1]?.id || null; - const nextSlug = entries[idx + 1]?.id || null; + // entries are sorted in by date descending, so prev has a higher index and next has lower + const prevSlug = entries[idx + 1]?.id || null; + const nextSlug = entries[idx - 1]?.id || null; return { params: { slug: entry.id }, props: { entry, prevSlug, nextSlug }, diff --git a/src/pages/index.astro b/src/pages/index.astro index 3e8804e..8d940e2 100644 --- a/src/pages/index.astro +++ b/src/pages/index.astro @@ -1,10 +1,9 @@ --- -import { getCollection } from 'astro:content'; +import { listPosts } from '@lib/content.ts'; import BaseLayout from '@layouts/BaseLayout.astro'; import Post from '@components/Post.astro'; -const entries = await getCollection('posts'); -entries.sort((a, b) => b.data.date.getTime() - a.data.date.getTime()); +const entries = await listPosts(); // there will always be at leaste one entry const entry = entries[0]!; const prevSlug = entries[1] ? entries[1]?.id : null; diff --git a/src/pages/posts.astro b/src/pages/posts.astro index 4f75434..e1eba7e 100644 --- a/src/pages/posts.astro +++ b/src/pages/posts.astro @@ -1,12 +1,12 @@ --- import '@styles/prose.css'; -import { getCollection, render } from 'astro:content'; +import { render } from 'astro:content'; +import { listPosts } from '@lib/content.ts'; import BaseLayout from '@layouts/BaseLayout.astro'; // return all posts in dev, only non-draft in prod -let entries = await getCollection('posts', ({ data }) => !data.draft || import.meta.env.DEV ); -entries.sort((a, b) => b.data.date.getTime() - a.data.date.getTime()); +const entries = await listPosts(); const posts = await Promise.all(entries.map(async entry => { const { remarkPluginFrontmatter } = await render(entry); return { ...entry, remarkPluginFrontmatter };