diff options
| author | Dawid Rycerz <dawid@rycerz.xyz> | 2026-01-30 20:45:07 +0100 |
|---|---|---|
| committer | Dawid Rycerz <dawid@rycerz.xyz> | 2026-01-30 20:45:07 +0100 |
| commit | 650249e1a8fe7d6645bb712026930dd7e8906ef8 (patch) | |
| tree | 4a4f5c46c72f7b8b0f6bef21ce6938e8a8978084 /src/data | |
| parent | 2345a208663efff76837d1228bf14b8847f3177f (diff) | |
feat(blog): add next/previous post navigation scoped by category
Navigate between posts within the same category (regular, microblog,
archived). Newer post links left, older post links right. Includes
i18n support for English and Polish.
Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
Diffstat (limited to 'src/data')
| -rw-r--r-- | src/data/post.ts | 39 |
1 files changed, 39 insertions, 0 deletions
diff --git a/src/data/post.ts b/src/data/post.ts index 85cc0d0..08a6678 100644 --- a/src/data/post.ts +++ b/src/data/post.ts @@ -1,4 +1,5 @@ import { type CollectionEntry, getCollection } from "astro:content"; +import { collectionDateSort } from "@/utils/date"; /** filter out draft posts based on the environment and optionally archived posts */ export async function getAllPosts(includeArchived = false): Promise<CollectionEntry<"post">[]> { @@ -62,3 +63,41 @@ export function getUniqueTagsWithCount(posts: CollectionEntry<"post">[]): [strin ), ].sort((a, b) => b[1] - a[1]); } + +export type PostCategory = "regular" | "microblog" | "archived"; + +/** Determine the category of a post based on its tags. "archived" takes precedence over "microblog". */ +export function getPostCategory(post: CollectionEntry<"post">): PostCategory { + const tags = post.data.tags; + if (tags.includes("archived")) return "archived"; + if (tags.includes("microblog")) return "microblog"; + return "regular"; +} + +export interface PostNavigation { + prevPost: CollectionEntry<"post"> | null; + nextPost: CollectionEntry<"post"> | null; +} + +/** Get previous (newer) and next (older) posts within the same category. + * Posts are sorted newest-first. prevPost = newer, nextPost = older. + */ +export function getPostNavigation( + allPosts: CollectionEntry<"post">[], + currentPost: CollectionEntry<"post">, +): PostNavigation { + const category = getPostCategory(currentPost); + const sameCategoryPosts = allPosts + .filter((p) => getPostCategory(p) === category) + .sort(collectionDateSort); + + const currentIndex = sameCategoryPosts.findIndex((p) => p.id === currentPost.id); + + return { + prevPost: currentIndex > 0 ? (sameCategoryPosts[currentIndex - 1] ?? null) : null, + nextPost: + currentIndex < sameCategoryPosts.length - 1 + ? (sameCategoryPosts[currentIndex + 1] ?? null) + : null, + }; +} |
