From 32fdaac7dbdd68ca4c77b24dc5cfe4b4382e5bcc Mon Sep 17 00:00:00 2001 From: libr Date: Sun, 30 Nov 2025 14:53:14 +0800 Subject: [PATCH] 2 --- .gitignore | 3 +- src/components/Comment.astro | 27 +++++----- src/components/notion/Bookmark.astro | 1 - src/lib/data-utils.ts | 62 +++++++++++----------- src/pages/404.astro | 2 +- src/pages/500.astro | 29 +++++++++++ src/pages/blog/[...id].astro | 78 ++++++++++++++++------------ src/pages/blog/index.astro | 1 - 8 files changed, 122 insertions(+), 81 deletions(-) create mode 100644 src/pages/500.astro diff --git a/.gitignore b/.gitignore index 7b9c288..9ac60c2 100644 --- a/.gitignore +++ b/.gitignore @@ -10,4 +10,5 @@ pnpm-debug.log* .DS_Store .idea/ teaser.pptx -~$teaser.pptx \ No newline at end of file +~$teaser.pptx +.vercel/ \ No newline at end of file diff --git a/src/components/Comment.astro b/src/components/Comment.astro index c167d4c..8c3fe65 100644 --- a/src/components/Comment.astro +++ b/src/components/Comment.astro @@ -2,25 +2,24 @@ \ No newline at end of file diff --git a/src/components/notion/Bookmark.astro b/src/components/notion/Bookmark.astro index 76b3927..9d67dc6 100644 --- a/src/components/notion/Bookmark.astro +++ b/src/components/notion/Bookmark.astro @@ -10,7 +10,6 @@ export interface Props { } const { block } = Astro.props -console.log(block) const target = block.Bookmark || block.LinkPreview || block.Embed const urlString = target?.Url diff --git a/src/lib/data-utils.ts b/src/lib/data-utils.ts index 0bf347b..0c88fa6 100644 --- a/src/lib/data-utils.ts +++ b/src/lib/data-utils.ts @@ -51,8 +51,9 @@ export type NotionBlock = { export type NotionBlockMap = Record export type RemotePostPayload = { - post: NotionPost + post: CollectionEntry<'blog'> blockMap: NotionBlockMap + rootId: string } export interface LinkEntry { @@ -255,10 +256,20 @@ export async function fetchRemotePostContent( try { const res = await fetch(`https://notion-api.nvme0n1p.dev/v2/posts/${encodeURI(slug)}`) if (!res.ok) throw new Error(`Failed to fetch post content: ${res.status}`) - const data = (await res.json()) as Partial + const data = (await res.json()) as Partial<{ + post: NotionPost + blockMap: NotionBlockMap + }> if (!data.post || !data.blockMap) return null - data.post = normalizePost(data.post) - return data as RemotePostPayload + + const rootId = data.post.id + const post = normalizePost(data.post) + + return { + post, + blockMap: data.blockMap, + rootId, + } } catch (error) { console.error(`fetchRemotePostContent error for slug "${slug}":`, error) return null @@ -897,12 +908,20 @@ export function renderRemoteBlockMap( ): RenderedRemoteContent { const headingBlocks: Block[] = [] const root = blockMap[rootId]?.value - const contentOrder = Array.isArray(root?.content) - ? root?.content - : Object.keys(blockMap) + const fallbackRoot = + root || + Object.values(blockMap) + .map((b) => b.value) + .find( + (value) => + Array.isArray(value?.content) && + (value.type === 'page' || value.type === 'collection_view_page'), + ) + const contentOrder = Array.isArray(fallbackRoot?.content) + ? fallbackRoot.content + : [] const blocks = buildBlocks(contentOrder, blockMap, headingBlocks) - const headings: TOCHeading[] = headingBlocks.map((block) => { const heading = block.Heading1 || block.Heading2 || block.Heading3 return { @@ -933,19 +952,18 @@ export async function getTOCSections(postId: string): Promise { const post = await getPostById(postId) if (!post) return [] - const parentId = isSubpost(postId) ? getParentId(postId) : postId - const parentPost = isSubpost(postId) ? await getPostById(parentId) : post + const parentPost = post if (!parentPost) return [] const sections: TOCSection[] = [] - const { headings: parentHeadings } = await render(parentPost) - if (parentHeadings.length > 0) { + const { headings } = await render(parentPost) + if (headings.length > 0) { sections.push({ type: 'parent', title: 'Overview', - headings: parentHeadings.map((heading) => ({ + headings: headings.map((heading) => ({ slug: heading.slug, text: heading.text, depth: heading.depth, @@ -953,23 +971,5 @@ export async function getTOCSections(postId: string): Promise { }) } - const subposts = await getSubpostsForParent(parentId) - for (const subpost of subposts) { - const { headings: subpostHeadings } = await render(subpost) - if (subpostHeadings.length > 0) { - sections.push({ - type: 'subpost', - title: subpost.data.title, - headings: subpostHeadings.map((heading, index) => ({ - slug: heading.slug, - text: heading.text, - depth: heading.depth, - isSubpostTitle: index === 0, - })), - subpostId: subpost.id, - }) - } - } - return sections } diff --git a/src/pages/404.astro b/src/pages/404.astro index 5591bc7..b3adef8 100644 --- a/src/pages/404.astro +++ b/src/pages/404.astro @@ -16,7 +16,7 @@ import { cn } from '@/lib/utils' >

404: Page not found

-

Oops! The page you're looking for doesn't exist.

+

啊哦,你好像来到了没有知识的荒原~

+ + + +
+
+

500: Something went wrong

+

+ 服务器爆炸了!如果你能联系到博主的话提醒他一声吧! +

+
+ + + Go to home page + +
+ diff --git a/src/pages/blog/[...id].astro b/src/pages/blog/[...id].astro index 449117c..1b14375 100644 --- a/src/pages/blog/[...id].astro +++ b/src/pages/blog/[...id].astro @@ -42,41 +42,55 @@ export async function getStaticPaths() { const currentPostId = Astro.params.id let Content: any = null -let headings -let remoteContent = null +let headings: any[] = [] +let remoteContent: any = null +let post: any = null +let authors: any[] = [] +let isCurrentSubpost = false +let navigation: any = null +let parentPost: any = null +let hasChildPosts = false +let subpostCount = 0 +let tocSections: TOCSection[] = [] +let heroImage: any = null +try { + const remote = await fetchRemotePostContent(currentPostId) + if (!remote) { + // return Astro.response(null, {status: 404}) + return Astro.rewrite('/404') + } -const remote = await fetchRemotePostContent(currentPostId) -if(!remote) { - // return Astro.response(null, {status: 404}) - return Astro.rewrite("/404") + remoteContent = renderRemoteBlockMap(remote.blockMap, remote.rootId) + headings = remoteContent?.headings ?? [] + post = remote.post + authors = await parseAuthors(post?.authors ?? []) + + isCurrentSubpost = isSubpost(currentPostId) + navigation = await getAdjacentPosts(currentPostId) + parentPost = isCurrentSubpost ? await getParentPost(currentPostId) : null + + hasChildPosts = await hasSubposts(currentPostId) + subpostCount = !isCurrentSubpost + ? await getSubpostCount(currentPostId) + : 0 + + tocSections = remoteContent + ? remoteContent.headings.length > 0 + ? [ + { + type: 'parent', + title: 'Overview', + headings: remoteContent.headings, + }, + ] + : [] + : await getTOCSections(currentPostId) + heroImage = post?.data?.banner +} catch (error) { + console.error('blog page render failed:', error) + return Astro.rewrite('/500') } -remoteContent = remote ? renderRemoteBlockMap(remote.blockMap, remote.post.id) : null -headings = remoteContent?.headings ?? [] -const post = remote?.post -const authors = await parseAuthors(post?.authors ?? []) - -const isCurrentSubpost = isSubpost(currentPostId) -const navigation = await getAdjacentPosts(currentPostId) -const parentPost = isCurrentSubpost ? await getParentPost(currentPostId) : null - -const hasChildPosts = await hasSubposts(currentPostId) -const subpostCount = !isCurrentSubpost - ? await getSubpostCount(currentPostId) - : 0 - -const tocSections: TOCSection[] = remoteContent - ? remoteContent.headings.length > 0 - ? [ - { - type: 'parent', - title: 'Overview', - headings: remoteContent.headings, - }, - ] - : [] - : await getTOCSections(currentPostId) -const heroImage = post?.data?.banner export const prerender = false; --- diff --git a/src/pages/blog/index.astro b/src/pages/blog/index.astro index 80a4950..d6577cf 100644 --- a/src/pages/blog/index.astro +++ b/src/pages/blog/index.astro @@ -35,7 +35,6 @@ const page = await fetch(`https://notion-api.nvme0n1p.dev/v2/posts/?page=${nowPa lastPage: totalPages, }; }); -console.log(page); // fetch page if(page.lastPage < page.currentPage) { console.log("redirect to 404")