Add blog post pagination

This commit is contained in:
DefectingCat
2022-08-23 15:04:51 +08:00
parent 2f42e3fb90
commit a62f420f4d
8 changed files with 220 additions and 56 deletions

View File

@ -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