mirror of
https://github.com/ChenQihan666/MyBlog-Next.git
synced 2026-08-14 11:29:33 +08:00
refactor: replace pagination logic with Astro built-in pagination (#376)
This commit is contained in:
committed by
GitHub
parent
70b2486293
commit
08b1676cdd
@@ -1,14 +0,0 @@
|
||||
import { SITE } from "@config";
|
||||
|
||||
const getPageNumbers = (numberOfPosts: number) => {
|
||||
const numberOfPages = numberOfPosts / Number(SITE.postPerPage);
|
||||
|
||||
let pageNumbers: number[] = [];
|
||||
for (let i = 1; i <= Math.ceil(numberOfPages); i++) {
|
||||
pageNumbers = [...pageNumbers, i];
|
||||
}
|
||||
|
||||
return pageNumbers;
|
||||
};
|
||||
|
||||
export default getPageNumbers;
|
||||
@@ -1,35 +0,0 @@
|
||||
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;
|
||||
Reference in New Issue
Block a user