From 0b57b888caf8817ff4992c59ed40ed29bee34fd4 Mon Sep 17 00:00:00 2001 From: Valentin Popov Date: Thu, 12 Sep 2024 14:06:44 +0000 Subject: Add PostPagination component for blog post navigation --- src/components/PostPagination.astro | 47 +++++++++++++++++++++++++++++++++++++ src/pages/blog/[...slug].astro | 17 +++++++++++--- 2 files changed, 61 insertions(+), 3 deletions(-) create mode 100644 src/components/PostPagination.astro (limited to 'src') diff --git a/src/components/PostPagination.astro b/src/components/PostPagination.astro new file mode 100644 index 0000000..6ae6bef --- /dev/null +++ b/src/components/PostPagination.astro @@ -0,0 +1,47 @@ +--- +const { prevPost, nextPost } = Astro.props; +--- + + + + diff --git a/src/pages/blog/[...slug].astro b/src/pages/blog/[...slug].astro index 4ecb1e1..9262a66 100644 --- a/src/pages/blog/[...slug].astro +++ b/src/pages/blog/[...slug].astro @@ -2,17 +2,24 @@ import { type CollectionEntry, getCollection } from "astro:content"; import Comments from "../../components/Comments.astro"; import Layout from "../../layouts/PageLayout.astro"; +import Pagination from "../../components/PostPagination.astro"; export async function getStaticPaths() { const posts = await getCollection("blog"); - return posts.map((post) => ({ + const total = posts.length; + + return posts.map((post, index) => ({ params: { slug: post.slug }, - props: post, + props: { + post, + prevPost: index + 1 === total ? null : posts[index + 1], + nextPost: index === 0 ? null : posts[index - 1], + }, })); } type Props = CollectionEntry<"blog">; -const post = Astro.props; +const { post, prevPost, nextPost } = Astro.props; const { Content, remarkPluginFrontmatter } = await post.render(); --- @@ -40,6 +47,10 @@ const { Content, remarkPluginFrontmatter } = await post.render(); +
+ +
+
-- cgit v1.2.3