Add PostPagination component for blog post navigation
Some checks failed
Test / test (push) Failing after 38s
Test / test (pull_request) Failing after 38s

This commit is contained in:
Valentin Popov 2024-09-12 14:06:44 +00:00
parent 4ba339984d
commit 0b57b888ca
Signed by: Valentin Popov
GPG Key ID: AE3CE523DAAA8401
2 changed files with 61 additions and 3 deletions

View File

@ -0,0 +1,47 @@
---
const { prevPost, nextPost } = Astro.props;
---
<style lang="scss">
.pagination {
overflow: hidden;
padding: 5rem 0;
width: 100%;
}
@media (width <=684px) {
.pagination {
padding: 2rem 0;
}
}
.prev,
.next {
max-width: 40%;
}
.prev {
float: left;
}
.next {
float: right;
}
</style>
<div class="pagination">
{
prevPost && (
<span class="prev">
<a href={`/blog/${prevPost.slug}`}>&lt; {prevPost.data.title}</a>
</span>
)
}
{
nextPost && (
<span class="next">
<a href={`/blog/${nextPost.slug}`}>{nextPost.data.title} &gt;</a>
</span>
)
}
</div>

View File

@ -2,17 +2,24 @@
import { type CollectionEntry, getCollection } from "astro:content"; import { type CollectionEntry, getCollection } from "astro:content";
import Comments from "../../components/Comments.astro"; import Comments from "../../components/Comments.astro";
import Layout from "../../layouts/PageLayout.astro"; import Layout from "../../layouts/PageLayout.astro";
import Pagination from "../../components/PostPagination.astro";
export async function getStaticPaths() { export async function getStaticPaths() {
const posts = await getCollection("blog"); const posts = await getCollection("blog");
return posts.map((post) => ({ const total = posts.length;
return posts.map((post, index) => ({
params: { slug: post.slug }, 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">; type Props = CollectionEntry<"blog">;
const post = Astro.props; const { post, prevPost, nextPost } = Astro.props;
const { Content, remarkPluginFrontmatter } = await post.render(); const { Content, remarkPluginFrontmatter } = await post.render();
--- ---
@ -40,6 +47,10 @@ const { Content, remarkPluginFrontmatter } = await post.render();
<Content /> <Content />
</section> </section>
<section>
<Pagination prevPost={prevPost} nextPost={nextPost} />
</section>
<section> <section>
<Comments /> <Comments />
</section> </section>