From e0a8e1b595dd5a636f49edce7c08b2fd12c1e452 Mon Sep 17 00:00:00 2001 From: nsfisis Date: Sat, 21 Jun 2025 15:03:29 +0900 Subject: feat(blog/nuldoc): implement pagination --- vhosts/blog/nuldoc-src/generators/post_list.ts | 48 +++++++++++++++++++++++--- 1 file changed, 44 insertions(+), 4 deletions(-) (limited to 'vhosts/blog/nuldoc-src/generators') diff --git a/vhosts/blog/nuldoc-src/generators/post_list.ts b/vhosts/blog/nuldoc-src/generators/post_list.ts index 67a4b996..b05f7ee6 100644 --- a/vhosts/blog/nuldoc-src/generators/post_list.ts +++ b/vhosts/blog/nuldoc-src/generators/post_list.ts @@ -6,18 +6,58 @@ import { PostPage } from "./post.ts"; export type PostListPage = Page; -export async function generatePostListPage( +export async function generatePostListPages( posts: PostPage[], config: Config, +): Promise { + const postsPerPage = config.blog.postsPerPage; + const totalPages = Math.ceil(posts.length / postsPerPage); + const pages: PostListPage[] = []; + + for (let pageIndex = 0; pageIndex < totalPages; pageIndex++) { + const pagePosts = posts.slice( + pageIndex * postsPerPage, + (pageIndex + 1) * postsPerPage, + ); + + const page = await generatePostListPage( + pagePosts, + config, + pageIndex + 1, + totalPages, + ); + + pages.push(page); + } + + return pages; +} + +async function generatePostListPage( + posts: PostPage[], + config: Config, + currentPage: number, + totalPages: number, ): Promise { const html = await renderToDOM( - PostListPage(posts, config), + PostListPage( + posts, + config, + currentPage, + totalPages, + ), ); + const destFilePath = currentPage === 1 + ? "/posts/index.html" + : `/posts/${currentPage}/index.html`; + + const href = currentPage === 1 ? "/posts/" : `/posts/${currentPage}/`; + return { root: html, renderer: "html", - destFilePath: "/posts/index.html", - href: "/posts/", + destFilePath, + href, }; } -- cgit v1.2.3-70-g09d2