summaryrefslogtreecommitdiffhomepage
path: root/vhosts/blog/nuldoc-src/generators/post_list.ts
diff options
context:
space:
mode:
authornsfisis <nsfisis@gmail.com>2025-06-21 15:03:29 +0900
committernsfisis <nsfisis@gmail.com>2025-06-21 15:48:56 +0900
commite0a8e1b595dd5a636f49edce7c08b2fd12c1e452 (patch)
tree2bb9a635b6144273772c692b939750b71f7a7332 /vhosts/blog/nuldoc-src/generators/post_list.ts
parentbe5d20ba8b6988c6107a6066774f3d7b994c48f5 (diff)
downloadnsfisis.dev-e0a8e1b595dd5a636f49edce7c08b2fd12c1e452.tar.gz
nsfisis.dev-e0a8e1b595dd5a636f49edce7c08b2fd12c1e452.tar.zst
nsfisis.dev-e0a8e1b595dd5a636f49edce7c08b2fd12c1e452.zip
feat(blog/nuldoc): implement pagination
Diffstat (limited to 'vhosts/blog/nuldoc-src/generators/post_list.ts')
-rw-r--r--vhosts/blog/nuldoc-src/generators/post_list.ts48
1 files changed, 44 insertions, 4 deletions
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<PostListPage[]> {
+ 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<PostListPage> {
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,
};
}