feat: new

This commit is contained in:
2025-11-28 20:22:33 +08:00
parent 7cffbaaacf
commit 9a93e8060c
6 changed files with 50 additions and 26 deletions

View File

@@ -65,7 +65,6 @@ const subpostCount = !isSubpost(entry.id) ? await getSubpostCount(entry.id) : 0
fallback={author.name[0]} fallback={author.name[0]}
className="size-5 rounded-full" className="size-5 rounded-full"
/> />
<span>{author.name}</span>
</div> </div>
))} ))}
<Separator orientation="vertical" className="h-4!" /> <Separator orientation="vertical" className="h-4!" />

View File

@@ -10,7 +10,7 @@ export interface Props {
} }
const { block } = Astro.props const { block } = Astro.props
console.log(block)
const target = block.Bookmark || block.LinkPreview || block.Embed const target = block.Bookmark || block.LinkPreview || block.Embed
const urlString = target?.Url const urlString = target?.Url
@@ -37,7 +37,7 @@ try {
<div class="bookmark"> <div class="bookmark">
<a href={url.toString()} target="_blank" rel="noopener noreferrer"> <a href={url.toString()} target="_blank" rel="noopener noreferrer">
<div> <div>
<div>{target?.Url}</div> <Caption richTexts={block.Bookmark?.Caption ?? []} />
<div class="muted">{url.hostname}</div> <div class="muted">{url.hostname}</div>
<div> <div>
<div> <div>
@@ -51,7 +51,7 @@ try {
</div> </div>
</div> </div>
</a> </a>
<Caption richTexts={block.Bookmark?.Caption ?? []} />
</div> </div>
)} )}
</> </>

View File

@@ -162,7 +162,7 @@ const PaginationComponent: React.FC<PaginationProps> = ({
const getPageUrl = (page: number) => { const getPageUrl = (page: number) => {
if (page === 1) return baseUrl if (page === 1) return baseUrl
return `${baseUrl}${page}` return `${baseUrl}?p=${page}`
} }
return ( return (

View File

@@ -94,16 +94,16 @@ export function normalizePost(post: NotionPost): CollectionEntry<'blog'> {
} }
} }
export async function getAllPosts(): Promise<CollectionEntry<'blog'>[]> { export async function getAllPosts(page?: Number, size?: Number): Promise<CollectionEntry<'blog'>[]> {
try { try {
const res = await fetch("https://notion-api.nvme0n1p.dev/v2/posts") const res = await fetch(`https://notion-api.nvme0n1p.dev/v2/posts?page=${page ?? 1}&length=${size ?? 1000}`)
if (!res.ok) throw new Error(`Failed to fetch posts: ${res.status}`) if (!res.ok) throw new Error(`Failed to fetch posts: ${res.status}`)
const payload = (await res.json()) as { posts?: NotionPost[] } const payload = (await res.json()) as { posts?: NotionPost[], length?: number }
const posts = (payload.posts ?? []).filter( const posts = (payload.posts ?? []).filter(
(post) => post.Published ?? true, (post) => post.Published ?? true,
) )
const length = payload.length;
const normalized = posts.map(normalizePost) const normalized = posts.map(normalizePost)
return normalized return normalized
@@ -545,7 +545,6 @@ function buildBlocks(
i++ i++
continue continue
} }
if (current.type === 'bulleted_list' || current.type === 'numbered_list') { if (current.type === 'bulleted_list' || current.type === 'numbered_list') {
const listItems: Block[] = [] const listItems: Block[] = []
const targetType = current.type const targetType = current.type
@@ -748,7 +747,7 @@ function convertBlock(
} }
case 'bookmark': { case 'bookmark': {
const url = const url =
block.properties?.link_url?.[0]?.[0] || block.properties?.link?.[0]?.[0] ||
block.properties?.source?.[0]?.[0] || block.properties?.source?.[0]?.[0] ||
block.properties?.title?.[0]?.[0] || block.properties?.title?.[0]?.[0] ||
'' ''
@@ -758,7 +757,7 @@ function convertBlock(
Type: 'bookmark', Type: 'bookmark',
HasChildren: false, HasChildren: false,
Bookmark: { Bookmark: {
Caption: parseRichTexts(block.properties?.caption), Caption: parseRichTexts(block.properties?.title),
Url: url, Url: url,
}, },
} }

View File

@@ -46,10 +46,14 @@ let remoteContent = null
const remote = await fetchRemotePostContent(currentPostId) 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 remoteContent = remote ? renderRemoteBlockMap(remote.blockMap, remote.post.id) : null
headings = remoteContent?.headings ?? [] headings = remoteContent?.headings ?? []
const post = remote?.post const post = remote?.post
const authors = await parseAuthors(post.authors ?? []) const authors = await parseAuthors(post?.authors ?? [])
const isCurrentSubpost = isSubpost(currentPostId) const isCurrentSubpost = isSubpost(currentPostId)
const navigation = await getAdjacentPosts(currentPostId) const navigation = await getAdjacentPosts(currentPostId)
@@ -71,7 +75,7 @@ const tocSections: TOCSection[] = remoteContent
] ]
: [] : []
: await getTOCSections(currentPostId) : await getTOCSections(currentPostId)
const heroImage = post.data.banner const heroImage = post?.data?.banner
export const prerender = false; export const prerender = false;
--- ---
@@ -232,7 +236,7 @@ export const prerender = false;
headings={remoteContent.headingBlocks} headings={remoteContent.headingBlocks}
/> />
) : ( ) : (
<p>Content unavailable.</p> <p>你来到了没有知识的荒原!</p>
) )
) : ( ) : (
Content && <Content /> Content && <Content />

View File

@@ -5,20 +5,42 @@ import PageHead from '@/components/PageHead.astro'
import PaginationComponent from '@/components/ui/pagination' import PaginationComponent from '@/components/ui/pagination'
import { SITE } from '@/consts' import { SITE } from '@/consts'
import Layout from '@/layouts/Layout.astro' 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' import type { PaginateFunction } from 'astro'
export async function getStaticPaths({ // export async function getStaticPaths({
paginate, // paginate,
}: { // }: {
paginate: PaginateFunction // paginate: PaginateFunction
}) { // }) {
const allPosts = await getAllPosts() // const allPosts = await getAllPosts()
return paginate(allPosts, { pageSize: SITE.postsPerPage }) // 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 postsByYear = groupPostsByYear(page.data)
const years = Object.keys(postsByYear).sort((a, b) => parseInt(b) - parseInt(a)) const years = Object.keys(postsByYear).sort((a, b) => parseInt(b) - parseInt(a))
export const prerender = false; export const prerender = false;