mirror of
https://github.com/DefectingCat/DefectingCat.github.io
synced 2025-07-15 16:51:37 +00:00
Add blog post pagination
This commit is contained in:
19
lib/posts.ts
19
lib/posts.ts
@ -33,6 +33,25 @@ export const postLists = async (): Promise<Post[]> => {
|
||||
return (await Promise.all(files.map(readFileMeta))).sort(sortByDate);
|
||||
};
|
||||
|
||||
/**
|
||||
* Get posts list page.
|
||||
*/
|
||||
export const PostPerPage = 5;
|
||||
export type PostPath = { params: { page: string } };
|
||||
const postPathCallback = (prev: PostPath[], _: unknown, i: number) =>
|
||||
i + 1 > 2
|
||||
? prev.concat({
|
||||
params: { page: (i + 1).toString() },
|
||||
})
|
||||
: prev;
|
||||
export const getPostListPath = async () => {
|
||||
const length = (await fs.readdir(path.join(dataPath))).length;
|
||||
const pages = Math.ceil(length / PostPerPage);
|
||||
return Array.from({ length: pages }).reduce<PostPath[]>(postPathCallback, [
|
||||
{ params: { page: '2' } },
|
||||
]);
|
||||
};
|
||||
|
||||
/**
|
||||
* Replace file name.
|
||||
* @param filename
|
||||
|
Reference in New Issue
Block a user