mirror of
https://github.com/ChenQihan666/MyBlog-Next.git
synced 2026-08-14 11:29:33 +08:00
Update [slug] page to render both post detail and post page
This commit is contained in:
@@ -1,65 +1,78 @@
|
|||||||
---
|
---
|
||||||
import Layout from "@layouts/Layout.astro";
|
import Posts from "@layouts/Posts.astro";
|
||||||
import Header from "@components/Header.astro";
|
import PostDetails from "@layouts/PostDetails.astro";
|
||||||
import Footer from "@components/Footer.astro";
|
import getSortedPosts from "@utils/getSortedPosts";
|
||||||
import Tag from "@components/Tag.astro";
|
import getPageNumbers from "@utils/getPageNumbers";
|
||||||
import Datetime from "@components/Datetime";
|
|
||||||
import type { MarkdownInstance } from "astro";
|
import type { MarkdownInstance } from "astro";
|
||||||
import type { Frontmatter } from "@utils/types";
|
import type { Frontmatter } from "@utils/types";
|
||||||
import Breadcrumbs from "@components/Breadcrumbs.astro";
|
|
||||||
|
|
||||||
export interface Props {
|
export interface Props {
|
||||||
post: MarkdownInstance<Frontmatter>;
|
post: MarkdownInstance<Frontmatter>;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
type PostResult = {
|
||||||
|
params: {
|
||||||
|
slug: string | number;
|
||||||
|
};
|
||||||
|
props?: {
|
||||||
|
post: MarkdownInstance<Record<string, any>>;
|
||||||
|
};
|
||||||
|
}[];
|
||||||
|
|
||||||
|
type PagePaths = {
|
||||||
|
params: {
|
||||||
|
slug: string;
|
||||||
|
};
|
||||||
|
}[];
|
||||||
|
|
||||||
export async function getStaticPaths() {
|
export async function getStaticPaths() {
|
||||||
const posts = await Astro.glob("../../contents/*.md");
|
const posts = await Astro.glob("../../contents/*.md");
|
||||||
|
|
||||||
return posts.map((post) => {
|
let postResult: PostResult = posts.map((post) => {
|
||||||
return {
|
return {
|
||||||
params: {
|
params: {
|
||||||
slug: post.frontmatter.slug,
|
slug: post.frontmatter.slug,
|
||||||
},
|
},
|
||||||
props: {
|
props: {
|
||||||
post,
|
post,
|
||||||
frontmatter: post.frontmatter,
|
|
||||||
},
|
},
|
||||||
};
|
};
|
||||||
});
|
});
|
||||||
|
|
||||||
|
const pagePaths: PagePaths = getPageNumbers(posts.length).map((pageNum) => ({
|
||||||
|
params: { slug: String(pageNum) },
|
||||||
|
}));
|
||||||
|
|
||||||
|
return [...postResult, ...pagePaths];
|
||||||
}
|
}
|
||||||
|
|
||||||
const { Content, frontmatter } = Astro.props.post;
|
const { slug } = Astro.params;
|
||||||
|
const { post } = Astro.props;
|
||||||
|
|
||||||
|
const posts = await Astro.glob<Frontmatter>("../../contents/*.md");
|
||||||
|
|
||||||
|
const sortedPosts = getSortedPosts(posts);
|
||||||
|
|
||||||
|
const totalPages = getPageNumbers(posts.length);
|
||||||
|
|
||||||
|
const currentPage =
|
||||||
|
slug && !isNaN(Number(slug)) && totalPages.includes(Number(slug))
|
||||||
|
? Number(slug)
|
||||||
|
: 0;
|
||||||
|
const lastPost = currentPage * import.meta.env.PUBLIC_POST_PER_PAGE;
|
||||||
|
const startPost = lastPost - import.meta.env.PUBLIC_POST_PER_PAGE;
|
||||||
|
|
||||||
|
const paginatedPosts = sortedPosts.slice(startPost, lastPost);
|
||||||
---
|
---
|
||||||
|
|
||||||
<Layout title={frontmatter.title}>
|
{
|
||||||
<Header />
|
post ? (
|
||||||
<Breadcrumbs />
|
<PostDetails post={post} />
|
||||||
<main id="main-content">
|
) : (
|
||||||
<h1 class="post-title">{frontmatter.title}</h1>
|
<Posts
|
||||||
<Datetime datetime={frontmatter.datetime} size="lg" className="my-2" />
|
posts={paginatedPosts}
|
||||||
<article
|
pageNum={currentPage}
|
||||||
id="article"
|
totalPages={totalPages.length}
|
||||||
role="article"
|
/>
|
||||||
class="mx-auto max-w-3xl mt-8 prose prose-slate"
|
)
|
||||||
>
|
}
|
||||||
<Content />
|
|
||||||
</article>
|
|
||||||
|
|
||||||
<ul class="tags-container">
|
|
||||||
{frontmatter.tags.map((tag) => <Tag name={tag} />)}
|
|
||||||
</ul>
|
|
||||||
</main>
|
|
||||||
<Footer />
|
|
||||||
</Layout>
|
|
||||||
|
|
||||||
<style>
|
|
||||||
main {
|
|
||||||
@apply max-w-3xl mx-auto px-4 pb-12 w-full;
|
|
||||||
}
|
|
||||||
.post-title {
|
|
||||||
@apply font-semibold text-2xl text-skin-accent;
|
|
||||||
}
|
|
||||||
.tags-container {
|
|
||||||
@apply my-8;
|
|
||||||
}
|
|
||||||
</style>
|
|
||||||
|
|||||||
Reference in New Issue
Block a user