Using default Astro's paginate for pages

This commit is contained in:
DarkCat09 2023-07-23 18:46:51 +04:00
parent fef04a8829
commit aab08105c9
2 changed files with 13 additions and 20 deletions

View file

@ -31,6 +31,6 @@ export default defineConfig({
], ],
}, },
redirects: { redirects: {
"/blog": "/blog/page0", "/blog": "/blog/page1",
} }
}); });

View file

@ -4,29 +4,22 @@ import ArticleCard from "../../components/ArticleCard.astro";
import { getCollection } from "astro:content"; import { getCollection } from "astro:content";
export async function getStaticPaths() { export async function getStaticPaths({ paginate }) {
const POSTS_ON_PAGE = 15; const POSTS_ON_PAGE = 15;
const posts = (await getCollection('blog')).sort( const posts = (await getCollection('blog'))
// sort by slug (which includes date), desc .sort( // sort by slug (which includes date), desc
(a, b) => (a.slug < b.slug) ? 1 : -1 (a, b) => (a.slug < b.slug) ? 1 : -1
); )
const count = Math.ceil(posts.length / POSTS_ON_PAGE); .map( // convert to objects
const paths = []; p => { return { post: p } }
);
for (let n = 0; n < count; n++) { // use Astro's function
paths.push({ return paginate(posts, { pageSize: POSTS_ON_PAGE });
params: { n: n },
props: {
posts: posts.slice(n, n + POSTS_ON_PAGE + 1),
},
});
}
return paths;
} }
const posts: Array<any> = Astro.props.posts; const { page } = Astro.props;
const description = const description =
"DarkCat09's blog. " + "DarkCat09's blog. " +
"Reading a QR code without a phone, " + "Reading a QR code without a phone, " +
@ -40,7 +33,7 @@ const description =
<meta name="robots" content="noindex, follow" /> <meta name="robots" content="noindex, follow" />
</Fragment> </Fragment>
<div id="articles"> <div id="articles">
{posts.map(post => <ArticleCard post={post} />)} {page.data.map(({ post }) => <ArticleCard post={post} />)}
</div> </div>
</BlogLayout> </BlogLayout>