summaryrefslogtreecommitdiffhomepage
path: root/services/blog/nuldoc-src/generators/post_list.ts
blob: b05f7ee6d4fb46a7320c2ca7d26d1598d6c22447 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
import { renderToDOM } from "../jsx/render.ts";
import PostListPage from "../pages/PostListPage.tsx";
import { Config } from "../config.ts";
import { Page } from "../page.ts";
import { PostPage } from "./post.ts";

export type PostListPage = Page;

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,
      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,
    href,
  };
}