From b041c3d7293bbc19a180152b8370dbaace161681 Mon Sep 17 00:00:00 2001 From: Jaehee Lee Date: Sun, 28 Aug 2022 16:24:53 +0900 Subject: [PATCH 01/13] implemented pagination --- blog.tsx | 37 ++++++++++++++++++++++++++ components.tsx | 71 +++++++++++++++++++++++++++++++++++++++++++++++++- 2 files changed, 107 insertions(+), 1 deletion(-) diff --git a/blog.tsx b/blog.tsx index 2a52dd5..ccb57fb 100644 --- a/blog.tsx +++ b/blog.tsx @@ -343,6 +343,30 @@ export async function handler( }); } } + + if(pathname === "/" && searchParams.get('page')?.match(/^[0-9]+$/)){ + const index = Number(searchParams.get('page')); + return html({ + ...sharedHtmlOptions, + title: blogState.title ?? "My Blog", + meta: { + "description": blogState.description, + "og:title": blogState.title, + "og:description": blogState.description, + "og:image": ogImage ?? blogState.cover, + "twitter:title": blogState.title, + "twitter:description": blogState.description, + "twitter:image": ogImage ?? blogState.cover, + "twitter:card": ogImage ? twitterCard : undefined, + }, + styles: [ + ...(blogState.style ? [blogState.style] : []), + ], + body: ( + + ) + }) + } if (pathname === "/") { return html({ @@ -364,7 +388,9 @@ export async function handler( body: ( ), }); @@ -523,6 +549,17 @@ export function redirects(redirectMap: Record): BlogMiddleware { }; } +function getPostsPage( + posts: Map, + index: number +) { + const i = Math.round(index); + if(i<0) return new Map(); + return new Map( + Array.from(posts.entries()).slice(i*10, i*10+10), + ) +} + function filterPosts( posts: Map, searchParams: URLSearchParams, diff --git a/components.tsx b/components.tsx index e722c18..74a4680 100644 --- a/components.tsx +++ b/components.tsx @@ -17,12 +17,46 @@ const socialAppIcons = new Map([ ["linkedin.com", IconLinkedin], ]); +interface PaginationProps { + index: number; + type: "forward" | "backward" | "both"; +} +function Pagination({ index, type }: PaginationProps) { + const page = ( +
+ {(type === "backward" || type === "both") + ? ( + + + + ) + : ""} + {(type === "forward" || type === "both") + ? ( + + + + ) + : ""} +
+ ); + return page; +} + interface IndexProps { state: BlogState; posts: Map; + index: number; + postsLength: number; } -export function Index({ state, posts }: IndexProps) { +export function Index({ state, posts, index, postsLength }: IndexProps) { const postIndex = []; for (const [_key, post] of posts.entries()) { postIndex.push(post); @@ -30,6 +64,14 @@ export function Index({ state, posts }: IndexProps) { postIndex.sort( (a, b) => (b.publishDate?.getTime() ?? 0) - (a.publishDate?.getTime() ?? 0), ); + let page; + if ((index + 1) * 10 >= postsLength && index !== 0) { + page = ; + } else if (index === 0) { + page = ; + } else { + page = ; + } return (
@@ -114,6 +156,7 @@ export function Index({ state, posts }: IndexProps) { /> ))}
+ {page} {state.footer ||