From 6b831e710034edd9bdbe62396e0480226fdd0da9 Mon Sep 17 00:00:00 2001 From: fiatcode Date: Thu, 22 Jan 2026 18:40:39 +0700 Subject: [PATCH] feat: implement pagination for blog posts and enhance NavLink component with class support --- README.md | 14 ++++----- src/components/NavLink.astro | 5 ++-- src/components/Pagination.astro | 26 +++++++++++++++++ src/pages/posts/[...page].astro | 51 +++++++++++++++++++++++++++++++++ src/pages/posts/index.astro | 30 ------------------- 5 files changed, 87 insertions(+), 39 deletions(-) create mode 100644 src/components/Pagination.astro create mode 100644 src/pages/posts/[...page].astro delete mode 100644 src/pages/posts/index.astro diff --git a/README.md b/README.md index c373370..89ffa19 100644 --- a/README.md +++ b/README.md @@ -11,13 +11,13 @@ npm run dev ## Commands -| Command | Action | -| :---------------- | :------------------------------------------ | -| `npm install` | Install dependencies | -| `npm run dev` | Start dev server at `localhost:4321` | -| `npm run build` | Build production site to `./dist/` | -| `npm run preview` | Preview production build locally | -| `npm run format` | Format code with Prettier | +| Command | Action | +| :---------------- | :----------------------------------- | +| `npm install` | Install dependencies | +| `npm run dev` | Start dev server at `localhost:4321` | +| `npm run build` | Build production site to `./dist/` | +| `npm run preview` | Preview production build locally | +| `npm run format` | Format code with Prettier | ## Tech Stack diff --git a/src/components/NavLink.astro b/src/components/NavLink.astro index d5d41da..fd2ebe5 100644 --- a/src/components/NavLink.astro +++ b/src/components/NavLink.astro @@ -2,14 +2,15 @@ interface Props { href: string; ariaLabel?: string; + class?: string; } -const { href, ariaLabel } = Astro.props; +const { href, ariaLabel, class: className } = Astro.props; --- diff --git a/src/components/Pagination.astro b/src/components/Pagination.astro new file mode 100644 index 0000000..29412b4 --- /dev/null +++ b/src/components/Pagination.astro @@ -0,0 +1,26 @@ +--- +import NavLink from "./NavLink.astro"; + +interface Props { + currentPage: number; + lastPage: number; + getPageUrl: (page: number) => string; +} + +const { currentPage, lastPage, getPageUrl } = Astro.props; +--- + + diff --git a/src/pages/posts/[...page].astro b/src/pages/posts/[...page].astro new file mode 100644 index 0000000..2d15724 --- /dev/null +++ b/src/pages/posts/[...page].astro @@ -0,0 +1,51 @@ +--- +import type { PaginateFunction, Page } from "astro"; +import { getCollection } from "astro:content"; +import Layout from "@/layouts/Layout.astro"; +import BlogPostCard from "@/components/BlogPostCard.astro"; +import PageTitle from "@/components/PageTitle.astro"; +import type { CollectionEntry } from "astro:content"; +import Pagination from "@/components/Pagination.astro"; + +export async function getStaticPaths({ + paginate, +}: { + paginate: PaginateFunction; +}) { + const posts = await getCollection("blog"); + posts.sort((a, b) => b.data.date.getTime() - a.data.date.getTime()); + return paginate(posts, { pageSize: 5 }); +} + +interface Props { + page: Page>; +} + +const { page } = Astro.props; +--- + + + +
    + { + page.data.map((post) => ( +
  • + +
  • + )) + } +
+ (page === 1 ? `/posts/` : `/posts/${page}/`)} + /> +
diff --git a/src/pages/posts/index.astro b/src/pages/posts/index.astro deleted file mode 100644 index bb44ca1..0000000 --- a/src/pages/posts/index.astro +++ /dev/null @@ -1,30 +0,0 @@ ---- -import { getCollection } from "astro:content"; -import Layout from "@/layouts/Layout.astro"; -import BlogPostCard from "@/components/BlogPostCard.astro"; -import PageTitle from "@/components/PageTitle.astro"; - -const posts = await getCollection("blog"); -posts.sort((a, b) => b.data.date.getTime() - a.data.date.getTime()); ---- - - - -
    - { - posts.map((post) => ( -
  • - -
  • - )) - } -
-