Skip to content

Commit f57fcc5

Browse files
committed
feat: add "next post in series" button
1 parent 98568d7 commit f57fcc5

1 file changed

Lines changed: 22 additions & 2 deletions

File tree

src/pages/posts/[slug].astro

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import Layout from '~/layouts/Layout.astro'
44
import { SeriesGroup, TagsGroup, getSortedPosts } from '~/utils'
55
import PostPreview from '~/components/PostPreview.astro'
66
import DividerText from '~/components/DividerText.astro'
7-
import { getCollection, render } from 'astro:content'
7+
import { getCollection, render, type CollectionEntry } from 'astro:content'
88
import PostAddendum from '~/components/PostAddendum.astro'
99
import TableOfContents from '~/components/TableOfContents.astro'
1010
import { Image } from 'astro:assets'
@@ -14,6 +14,7 @@ import Tags from '~/components/Tags.astro'
1414
import PostInfo from '~/components/PostInfo.astro'
1515
import ScrollUpButton from '~/components/ScrollUpButton.astro'
1616
import type { Collation } from '~/types'
17+
import ChevronsRight from '~/icons/chevrons-right.svg'
1718
1819
export const getStaticPaths = (async () => {
1920
const posts = await getSortedPosts()
@@ -46,9 +47,18 @@ const sortedPosts = await getSortedPosts()
4647
4748
// Get series posts if this post is part of a series
4849
let series: Collation<'posts'> | undefined
50+
let nextPostInSeries: CollectionEntry<'posts'> | undefined
4951
if (postData.series) {
5052
const seriesGroup = await SeriesGroup.build(sortedPosts)
5153
series = seriesGroup.match(postData.series)
54+
if (!series) {
55+
// This should not happen if series data is correct
56+
throw new Error(`Series "${postData.series}" not found`)
57+
}
58+
const index = series.entries.findIndex((entry) => entry.id === post.id)
59+
if (index >= 0 && index <= series.entries.length - 2) {
60+
nextPostInSeries = series.entries[index + 1]
61+
}
5262
}
5363
const showSeries = series && series.entries.length > 1
5464
@@ -100,7 +110,17 @@ if (postData.tags && postData.tags.length > 0) {
100110
</div>
101111
</div>
102112
</article>
103-
<div class="article-end-marker" aria-hidden="true"></div>
113+
{
114+
nextPostInSeries && (
115+
<a
116+
href={`/posts/${nextPostInSeries.id}`}
117+
class="button justify-center -mt-8 mb-8 !whitespace-normal text-center flex gap-2 sm:gap-3 items-center"
118+
>
119+
<span>Next: {nextPostInSeries.data.title}</span>
120+
<ChevronsRight class="size-5 hidden sm:block" />
121+
</a>
122+
)
123+
}
104124
{
105125
AddendumContent && (
106126
<PostAddendum avatarImage={addendumAvatarImage}>

0 commit comments

Comments
 (0)