From 29eff36794fc4e6d995611fad3aa9c1b3a3c67eb Mon Sep 17 00:00:00 2001 From: Sat Naing Date: Mon, 16 Sep 2024 20:43:54 +0700 Subject: [PATCH] feat: add prev/next links at the bottom of blog post (#372) * feat: add prev/next links at the bottom of blog post * fix: update and rerun scripts in post details page after swap Resolves #358 --- src/layouts/PostDetails.astro | 115 +++++++++++++++++++++++++---- src/pages/posts/[slug]/index.astro | 6 +- 2 files changed, 106 insertions(+), 15 deletions(-) diff --git a/src/layouts/PostDetails.astro b/src/layouts/PostDetails.astro index 3c1a53d42..8ab45a74b 100644 --- a/src/layouts/PostDetails.astro +++ b/src/layouts/PostDetails.astro @@ -11,9 +11,10 @@ import { SITE } from "@config"; export interface Props { post: CollectionEntry<"blog">; + posts: CollectionEntry<"blog">[]; } -const { post } = Astro.props; +const { post, posts } = Astro.props; const { title, @@ -44,6 +45,19 @@ const layoutProps = { ogImage: ogUrl, scrollSmooth: true, }; + +/* ========== Prev/Next Posts ========== */ + +const allPosts = posts.map(({ data: { title }, slug }) => ({ + slug, + title, +})); + +const currentPostIndex = allPosts.findIndex(a => a.slug === post.slug); + +const prevPost = currentPostIndex !== 0 ? allPosts[currentPostIndex - 1] : null; +const nextPost = + currentPostIndex !== allPosts.length ? allPosts[currentPostIndex + 1] : null; --- @@ -94,6 +108,72 @@ const layoutProps = { + +
+ + +