-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: use Astro's built-in pagination
- Loading branch information
Showing
11 changed files
with
80 additions
and
180 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,40 +1,34 @@ | ||
--- | ||
import type { CollectionEntry } from "astro:content"; | ||
import Layout from "@layouts/Layout.astro"; | ||
import Main from "@layouts/Main.astro"; | ||
import Header from "@components/Header.astro"; | ||
import Footer from "@components/Footer.astro"; | ||
import Pagination from "@components/Pagination.astro"; | ||
import Card from "@components/Card"; | ||
import { SITE } from "@config"; | ||
import type { Page } from "astro"; | ||
import type { CollectionEntry } from "astro:content"; | ||
export interface Props { | ||
currentPage: number; | ||
totalPages: number; | ||
paginatedPosts: CollectionEntry<"blog">[]; | ||
page: Page<CollectionEntry<"blog">>; | ||
} | ||
const { currentPage, totalPages, paginatedPosts } = Astro.props; | ||
const { page } = Astro.props; | ||
--- | ||
|
||
<Layout title={`Posts | ${SITE.title}`}> | ||
<Header activeNav="posts" /> | ||
<Main pageTitle="Articles" pageDesc="Tous les articles."> | ||
<Main pageTitle="Posts" pageDesc="All the articles I've posted."> | ||
<ul> | ||
{ | ||
paginatedPosts.map(({ data, slug }) => ( | ||
page.data.map(({ data, slug }) => ( | ||
<Card href={`/posts/${slug}/`} frontmatter={data} /> | ||
)) | ||
} | ||
</ul> | ||
</Main> | ||
|
||
<Pagination | ||
{currentPage} | ||
{totalPages} | ||
prevUrl={`/posts${currentPage - 1 !== 1 ? "/" + (currentPage - 1) : ""}/`} | ||
nextUrl={`/posts/${currentPage + 1}/`} | ||
/> | ||
<Pagination {page} /> | ||
|
||
<Footer noMarginTop={totalPages > 1} /> | ||
</Layout> | ||
<Footer noMarginTop={page.lastPage > 1} /> | ||
</Layout> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,49 +1,41 @@ | ||
--- | ||
import { type CollectionEntry } from "astro:content"; | ||
import Layout from "@layouts/Layout.astro"; | ||
import Main from "@layouts/Main.astro"; | ||
import Header from "@components/Header.astro"; | ||
import Footer from "@components/Footer.astro"; | ||
import Card from "@components/Card"; | ||
import Pagination from "@components/Pagination.astro"; | ||
import { SITE } from "@config"; | ||
import type { Page } from "astro"; | ||
import type { CollectionEntry } from "astro:content"; | ||
export interface Props { | ||
currentPage: number; | ||
totalPages: number; | ||
paginatedPosts: CollectionEntry<"blog">[]; | ||
page: Page<CollectionEntry<"blog">>; | ||
tag: string; | ||
tagName: string; | ||
} | ||
const { currentPage, totalPages, paginatedPosts, tag, tagName } = Astro.props; | ||
const { page, tag, tagName } = Astro.props; | ||
--- | ||
|
||
<Layout title={`Tag: ${tagName} | ${SITE.title}`}> | ||
<Header activeNav="tags" /> | ||
<Main | ||
pageTitle={[`Tag:`, `${tagName}`]} | ||
titleTransition={tag} | ||
pageDesc={`All the articles with the tag "${tagName}".`} | ||
pageDesc={`Tous les articles avec le tag "${tagName}".`} | ||
> | ||
<h1 slot="title" transition:name={tag}>{`Tag:${tag}`}</h1> | ||
<ul> | ||
{ | ||
paginatedPosts.map(({ data, slug }) => ( | ||
page.data.map(({ data, slug }) => ( | ||
<Card href={`/posts/${slug}/`} frontmatter={data} /> | ||
)) | ||
} | ||
</ul> | ||
</Main> | ||
|
||
<Pagination | ||
{currentPage} | ||
{totalPages} | ||
prevUrl={`/tags/${tag}${ | ||
currentPage - 1 !== 1 ? "/" + (currentPage - 1) : "" | ||
}/`} | ||
nextUrl={`/tags/${tag}/${currentPage + 1}/`} | ||
/> | ||
<Pagination {page} /> | ||
|
||
<Footer noMarginTop={totalPages > 1} /> | ||
</Layout> | ||
<Footer noMarginTop={page.lastPage > 1} /> | ||
</Layout> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
--- | ||
import { SITE } from "@config"; | ||
import Posts from "@layouts/Posts.astro"; | ||
import type { GetStaticPaths } from "astro"; | ||
import { getCollection } from "astro:content"; | ||
import getSortedPosts from "@utils/getSortedPosts"; | ||
export const getStaticPaths = (async ({ paginate }) => { | ||
const posts = await getCollection("blog", ({ data }) => !data.draft); | ||
return paginate(getSortedPosts(posts), { pageSize: SITE.postPerPage }); | ||
}) satisfies GetStaticPaths; | ||
const { page } = Astro.props; | ||
--- | ||
|
||
<Posts {page} /> |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,44 +1,29 @@ | ||
--- | ||
import { type CollectionEntry, getCollection } from "astro:content"; | ||
import { getCollection } from "astro:content"; | ||
import TagPosts from "@layouts/TagPosts.astro"; | ||
import getUniqueTags from "@utils/getUniqueTags"; | ||
import getPostsByTag from "@utils/getPostsByTag"; | ||
import getPageNumbers from "@utils/getPageNumbers"; | ||
import getPagination from "@utils/getPagination"; | ||
import type { GetStaticPathsOptions } from "astro"; | ||
import { SITE } from "@config"; | ||
export interface Props { | ||
post: CollectionEntry<"blog">; | ||
tag: string; | ||
tagName: string; | ||
} | ||
export async function getStaticPaths() { | ||
export async function getStaticPaths({ paginate }: GetStaticPathsOptions) { | ||
const posts = await getCollection("blog"); | ||
const tags = getUniqueTags(posts); | ||
return tags.flatMap(({ tag, tagName }) => { | ||
const tagPosts = getPostsByTag(posts, tag); | ||
const totalPages = getPageNumbers(tagPosts.length); | ||
return totalPages.map((page) => ({ | ||
params: { tag, page }, | ||
props: { tag, tagName }, | ||
})); | ||
return paginate(tagPosts, { | ||
params: { tag }, | ||
props: { tagName }, | ||
pageSize: SITE.postPerPage, | ||
}); | ||
}); | ||
} | ||
const { page } = Astro.params; | ||
const { tag, tagName } = Astro.props; | ||
const posts = await getCollection("blog", ({ data }) => !data.draft); | ||
const postsByTag = getPostsByTag(posts, tag); | ||
const pagination = getPagination({ | ||
posts: postsByTag, | ||
page, | ||
}); | ||
const params = Astro.params; | ||
const { tag } = params; | ||
const { page, tagName } = Astro.props; | ||
--- | ||
|
||
<TagPosts {...pagination} {tag} {tagName} /> | ||
<TagPosts {page} {tag} {tagName} /> |
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
aced187
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Successfully deployed to the following URLs:
no1cetea-keyboard-5no2 – ./
no1cekeebs.live
no1cetea-keyboard-5no2-git-master-no1ceteaprojects.vercel.app
www.no1cekeebs.live
no1cetea-keyboard-5no2-no1ceteaprojects.vercel.app