Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add next & prev pages functionality #340

Merged
merged 1 commit into from
Jul 13, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 36 additions & 1 deletion pages/blog/[slug].tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { MDXRemote, MDXRemoteSerializeResult } from 'next-mdx-remote';
import { GetStaticPathsResult, GetStaticPropsContext, GetStaticPropsResult } from 'next';
import { ParsedUrlQuery } from 'querystring';

import { getPostBySlug, getPostSlugs } from '@/lib/posts/api';
import { getPostBySlug, getPosts, getPostSlugs } from '@/lib/posts/api';
import { compileMDX } from '@/lib/scripts/compiler';
import { Post } from '@/lib/types';

Expand All @@ -14,9 +14,12 @@ import { ViewCounter } from '@/components/view-counter/view-counter';
import {
FacebookShare, LinkedInShare, TelegramShare, TwitterShare,
} from '@/components/share-button';
import { sortByBirthtime } from '@/lib/posts/utils';

type ArticlePageProps = Pick<Post, 'data'> & {
content: MDXRemoteSerializeResult;
prevPostSlug: string,
nextPostSlug: string,
};

function ArticlePage(props: ArticlePageProps) {
Expand All @@ -33,6 +36,8 @@ function ArticlePage(props: ArticlePageProps) {
categories = [],
keywords,
},
nextPostSlug,
prevPostSlug,
} = props;

const formattedDate = new Date(createDate).toLocaleDateString('en-us', {
Expand Down Expand Up @@ -158,6 +163,30 @@ function ArticlePage(props: ArticlePageProps) {
<MDXRemote {...content} components={MDXComponents} />
</div>

<ul className={`${!prevPostSlug ? 'justify-end' : 'justify-between'} mt-16 flex`}>
{prevPostSlug && (
<li>
<Link
className="text-black dark:text-gray-200 inline-block p-3 rounded-lg bg-gray-200 dark:bg-gray-800 transition-all"
href={`/blog/${prevPostSlug}`}
>
👈 Previous article
</Link>
</li>
)}

{nextPostSlug && (
<li>
<Link
className="text-black dark:text-gray-200 inline-block p-3 rounded-lg bg-gray-200 dark:bg-gray-800"
href={`/blog/${nextPostSlug}`}
>
Next article 👉
</Link>
</li>
)}
</ul>

<div className="flex lg:hidden text-gray-600 dark:text-gray-400 items-center mt-16">
<p>Share it:</p>
<ul className="flex gap-2">
Expand Down Expand Up @@ -192,11 +221,17 @@ export async function getStaticProps({
> {
const { data, content } = await getPostBySlug(params?.slug);
const html = await compileMDX(content);
const posts = await getPosts();
const currentPostIndex = posts.sort(sortByBirthtime).findIndex((post) => post.data.slug === data.slug);
const prevPost = posts[currentPostIndex + 1] || null;
const nextPost = posts[currentPostIndex - 1] || null;

return {
props: {
data,
content: html,
prevPostSlug: prevPost?.data.slug || '',
nextPostSlug: nextPost?.data.slug || '',
},
};
}
Expand Down
Loading