feat: add pagination in tag posts (#201)

* feat: add pagination in tag posts

Implement pagination in tag posts. Refactor and extract pagination logic and pagination component.

Closes: #152

* fix: update breadcrumbs for updated tag pagination
This commit is contained in:
Sat Naing
2023-12-29 10:26:02 +06:30
committed by GitHub
parent b05b8fb842
commit 581826a5af
11 changed files with 262 additions and 140 deletions
+35
View File
@@ -0,0 +1,35 @@
import { SITE } from "@config";
import getPageNumbers from "./getPageNumbers";
interface GetPaginationProps<T> {
posts: T;
page: string | number;
isIndex?: boolean;
}
const getPagination = <T>({
posts,
page,
isIndex = false,
}: GetPaginationProps<T[]>) => {
const totalPagesArray = getPageNumbers(posts.length);
const totalPages = totalPagesArray.length;
const currentPage = isIndex
? 1
: page && !isNaN(Number(page)) && totalPagesArray.includes(Number(page))
? Number(page)
: 0;
const lastPost = isIndex ? SITE.postPerPage : currentPage * SITE.postPerPage;
const startPost = isIndex ? 0 : lastPost - SITE.postPerPage;
const paginatedPosts = posts.slice(startPost, lastPost);
return {
totalPages,
currentPage,
paginatedPosts,
};
};
export default getPagination;
+5 -2
View File
@@ -1,7 +1,10 @@
import { slugifyAll } from "./slugify";
import type { CollectionEntry } from "astro:content";
import getSortedPosts from "./getSortedPosts";
import { slugifyAll } from "./slugify";
const getPostsByTag = (posts: CollectionEntry<"blog">[], tag: string) =>
posts.filter(post => slugifyAll(post.data.tags).includes(tag));
getSortedPosts(
posts.filter(post => slugifyAll(post.data.tags).includes(tag))
);
export default getPostsByTag;