From de1885fe8fe279c95c2c2b101fad916958dadd4a Mon Sep 17 00:00:00 2001 From: Valentin Popov Date: Thu, 12 Sep 2024 22:10:31 +0000 Subject: [PATCH] Added Pagination component --- astro.config.mjs | 6 +++ src/components/Pagination.astro | 46 +++++++++++++++++++ .../{PostElement.astro => PostSummary.astro} | 0 src/pages/[...page].astro | 30 ++++++++++++ src/pages/index.astro | 14 ------ 5 files changed, 82 insertions(+), 14 deletions(-) create mode 100644 src/components/Pagination.astro rename src/components/{PostElement.astro => PostSummary.astro} (100%) create mode 100644 src/pages/[...page].astro delete mode 100644 src/pages/index.astro diff --git a/astro.config.mjs b/astro.config.mjs index 8f03e22..1b1e5bf 100644 --- a/astro.config.mjs +++ b/astro.config.mjs @@ -8,4 +8,10 @@ export default defineConfig({ markdown: { remarkPlugins: [remarkReadingTime], }, + redirects: { + "/blog": { + destination: "/", + status: 301, + }, + }, }); diff --git a/src/components/Pagination.astro b/src/components/Pagination.astro new file mode 100644 index 0000000..e9fbc69 --- /dev/null +++ b/src/components/Pagination.astro @@ -0,0 +1,46 @@ +--- +type Props = { + readonly prevUrl?: string; + readonly nextUrl?: string; +}; + +const { prevUrl, nextUrl } = Astro.props; +--- + + + + diff --git a/src/components/PostElement.astro b/src/components/PostSummary.astro similarity index 100% rename from src/components/PostElement.astro rename to src/components/PostSummary.astro diff --git a/src/pages/[...page].astro b/src/pages/[...page].astro new file mode 100644 index 0000000..23a5d51 --- /dev/null +++ b/src/pages/[...page].astro @@ -0,0 +1,30 @@ +--- +import type { GetStaticPaths, InferGetStaticPropsType } from "astro"; +import { getCollection } from "astro:content"; +import Layout from "../layouts/BaseLayout.astro"; +import Pagination from "../components/Pagination.astro"; +import PostSummary from "../components/PostSummary.astro"; + +export const getStaticPaths = (async ({ paginate }) => { + const posts = await getCollection("blog"); + posts.sort((a, b) => b.data.pubDate.getTime() - a.data.pubDate.getTime()); + + return paginate(posts, { + pageSize: 5, + }); +}) satisfies GetStaticPaths; + +type Props = InferGetStaticPropsType; + +const { page } = Astro.props; +--- + + +
+ {page.data.map((post) => )} +
+ +
+ +
+
diff --git a/src/pages/index.astro b/src/pages/index.astro deleted file mode 100644 index 6575ca6..0000000 --- a/src/pages/index.astro +++ /dev/null @@ -1,14 +0,0 @@ ---- -import { getCollection } from "astro:content"; -import Element from "../components/PostElement.astro"; -import Layout from "../layouts/BaseLayout.astro"; - -const posts = await getCollection("blog"); -posts.sort((a, b) => b.data.pubDate.getTime() - a.data.pubDate.getTime()); ---- - - -
- {posts.map((post) => )} -
-