wip
This commit is contained in:
17
lib/blog.ts
17
lib/blog.ts
@@ -56,3 +56,20 @@ export async function getAllPosts(locale: string): Promise<PostMdx[]> {
|
||||
|
||||
return posts;
|
||||
}
|
||||
|
||||
export async function getAdjacentPosts(slug: string, locale: string): Promise<{ prev: PostMdx | null; next: PostMdx | null }> {
|
||||
const posts = await getAllPosts(locale);
|
||||
const currentIndex = posts.findIndex(post => post.slug === slug);
|
||||
|
||||
if (currentIndex === -1) {
|
||||
return { prev: null, next: null };
|
||||
}
|
||||
|
||||
// Posts are sorted by date descending (newest first)
|
||||
// So "next" post (newer) is at index - 1
|
||||
// And "previous" post (older) is at index + 1
|
||||
const next = currentIndex > 0 ? posts[currentIndex - 1] : null;
|
||||
const prev = currentIndex < posts.length - 1 ? posts[currentIndex + 1] : null;
|
||||
|
||||
return { prev, next };
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user