mirror of
https://github.com/lbr77/blog-astro.git
synced 2026-04-10 09:39:11 +00:00
feat: new
This commit is contained in:
@@ -46,10 +46,14 @@ let remoteContent = null
|
||||
|
||||
|
||||
const remote = await fetchRemotePostContent(currentPostId)
|
||||
if(!remote) {
|
||||
// return Astro.response(null, {status: 404})
|
||||
return Astro.rewrite("/404")
|
||||
}
|
||||
remoteContent = remote ? renderRemoteBlockMap(remote.blockMap, remote.post.id) : null
|
||||
headings = remoteContent?.headings ?? []
|
||||
const post = remote?.post
|
||||
const authors = await parseAuthors(post.authors ?? [])
|
||||
const authors = await parseAuthors(post?.authors ?? [])
|
||||
|
||||
const isCurrentSubpost = isSubpost(currentPostId)
|
||||
const navigation = await getAdjacentPosts(currentPostId)
|
||||
@@ -71,7 +75,7 @@ const tocSections: TOCSection[] = remoteContent
|
||||
]
|
||||
: []
|
||||
: await getTOCSections(currentPostId)
|
||||
const heroImage = post.data.banner
|
||||
const heroImage = post?.data?.banner
|
||||
export const prerender = false;
|
||||
---
|
||||
|
||||
@@ -232,7 +236,7 @@ export const prerender = false;
|
||||
headings={remoteContent.headingBlocks}
|
||||
/>
|
||||
) : (
|
||||
<p>Content unavailable.</p>
|
||||
<p>你来到了没有知识的荒原!</p>
|
||||
)
|
||||
) : (
|
||||
Content && <Content />
|
||||
|
||||
@@ -5,20 +5,42 @@ import PageHead from '@/components/PageHead.astro'
|
||||
import PaginationComponent from '@/components/ui/pagination'
|
||||
import { SITE } from '@/consts'
|
||||
import Layout from '@/layouts/Layout.astro'
|
||||
import { getAllPosts, groupPostsByYear } from '@/lib/data-utils'
|
||||
import { getAllPosts, groupPostsByYear, normalizePost } from '@/lib/data-utils'
|
||||
import type { PaginateFunction } from 'astro'
|
||||
|
||||
export async function getStaticPaths({
|
||||
paginate,
|
||||
}: {
|
||||
paginate: PaginateFunction
|
||||
}) {
|
||||
const allPosts = await getAllPosts()
|
||||
return paginate(allPosts, { pageSize: SITE.postsPerPage })
|
||||
// export async function getStaticPaths({
|
||||
// paginate,
|
||||
// }: {
|
||||
// paginate: PaginateFunction
|
||||
// }) {
|
||||
// const allPosts = await getAllPosts()
|
||||
// return paginate(allPosts, { pageSize: SITE.postsPerPage })
|
||||
// }
|
||||
const nowPage = parseInt(new URL(Astro.request.url).searchParams.get("p") || "1");
|
||||
|
||||
// use url param
|
||||
// const { page } = Astro.para
|
||||
|
||||
const page = await fetch(`https://notion-api.nvme0n1p.dev/v2/posts/?page=${nowPage}&length=${SITE.postsPerPage}`)
|
||||
.then((res) => {
|
||||
if (!res.ok) throw new Error(`Failed to fetch posts: ${res.status}`);
|
||||
return res.json();
|
||||
})
|
||||
.then((allPosts) => {
|
||||
const totalPages = allPosts.length;
|
||||
const currentPage = nowPage;
|
||||
return {
|
||||
data: allPosts.posts.map((post) => normalizePost(post)),
|
||||
currentPage,
|
||||
lastPage: totalPages,
|
||||
};
|
||||
});
|
||||
console.log(page);
|
||||
// fetch page
|
||||
if(page.lastPage < page.currentPage) {
|
||||
console.log("redirect to 404")
|
||||
return Astro.rewrite("/404")
|
||||
}
|
||||
|
||||
const { page } = Astro.props
|
||||
|
||||
const postsByYear = groupPostsByYear(page.data)
|
||||
const years = Object.keys(postsByYear).sort((a, b) => parseInt(b) - parseInt(a))
|
||||
export const prerender = false;
|
||||
Reference in New Issue
Block a user