/
index.astro
73 lines (60 loc) 路 1.7 KB
/
index.astro
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
---
import { type CollectionEntry, getCollection } from "astro:content";
import Posts from "@layouts/Posts.astro";
import PostDetails from "@layouts/PostDetails.astro";
import getSortedPosts from "@utils/getSortedPosts";
import getPageNumbers from "@utils/getPageNumbers";
import getPagination from "@utils/getPagination";
export interface Props {
post: CollectionEntry<"blog">;
}
export async function getStaticPaths() {
const posts = await getCollection(
"blog",
({ data }) =>
!data.draft &&
(data.permalink || data.permalinks?.length) &&
!data.permalink?.includes("posts/")
);
let postResult = posts
.map(post => {
let result = [];
if (post.data?.permalink) {
result.push({
params: {
slug: post.data.permalink!.replace("/post/", ""),
},
props: { post },
});
}
if (post.data?.permalinks.length) {
result = [
...result,
...post.data?.permalinks
.filter(link => link.startsWith("/post/"))
.map(link => ({
params: {
slug: link?.replace("/post/", ""),
},
props: { post },
})),
];
}
return result;
})
.flat();
const pagePaths = getPageNumbers(posts.length).map(pageNum => ({
params: { slug: String(pageNum) },
}));
return [...postResult, ...pagePaths];
}
const { slug } = Astro.params;
const { post } = Astro.props;
const posts = await getCollection("blog");
const sortedPosts = getSortedPosts(posts);
const pagination = getPagination({
posts: sortedPosts,
page: slug,
});
---
{post ? <PostDetails post={post} /> : <Posts {...pagination} />}