mirror of
https://github.com/ChenQihan666/MyBlog-Next.git
synced 2026-08-14 11:29:33 +08:00
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:
@@ -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;
|
||||
@@ -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;
|
||||
|
||||
Reference in New Issue
Block a user