mirror of
https://github.com/lbr77/blog-astro.git
synced 2026-04-08 16:11:56 +00:00
feat: new
This commit is contained in:
@@ -65,7 +65,6 @@ const subpostCount = !isSubpost(entry.id) ? await getSubpostCount(entry.id) : 0
|
||||
fallback={author.name[0]}
|
||||
className="size-5 rounded-full"
|
||||
/>
|
||||
<span>{author.name}</span>
|
||||
</div>
|
||||
))}
|
||||
<Separator orientation="vertical" className="h-4!" />
|
||||
|
||||
@@ -10,7 +10,7 @@ export interface Props {
|
||||
}
|
||||
|
||||
const { block } = Astro.props
|
||||
|
||||
console.log(block)
|
||||
const target = block.Bookmark || block.LinkPreview || block.Embed
|
||||
const urlString = target?.Url
|
||||
|
||||
@@ -37,7 +37,7 @@ try {
|
||||
<div class="bookmark">
|
||||
<a href={url.toString()} target="_blank" rel="noopener noreferrer">
|
||||
<div>
|
||||
<div>{target?.Url}</div>
|
||||
<Caption richTexts={block.Bookmark?.Caption ?? []} />
|
||||
<div class="muted">{url.hostname}</div>
|
||||
<div>
|
||||
<div>
|
||||
@@ -51,7 +51,7 @@ try {
|
||||
</div>
|
||||
</div>
|
||||
</a>
|
||||
<Caption richTexts={block.Bookmark?.Caption ?? []} />
|
||||
|
||||
</div>
|
||||
)}
|
||||
</>
|
||||
|
||||
@@ -162,7 +162,7 @@ const PaginationComponent: React.FC<PaginationProps> = ({
|
||||
|
||||
const getPageUrl = (page: number) => {
|
||||
if (page === 1) return baseUrl
|
||||
return `${baseUrl}${page}`
|
||||
return `${baseUrl}?p=${page}`
|
||||
}
|
||||
|
||||
return (
|
||||
|
||||
@@ -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 {
|
||||
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}`)
|
||||
const payload = (await res.json()) as { posts?: NotionPost[] }
|
||||
const payload = (await res.json()) as { posts?: NotionPost[], length?: number }
|
||||
const posts = (payload.posts ?? []).filter(
|
||||
(post) => post.Published ?? true,
|
||||
)
|
||||
|
||||
const length = payload.length;
|
||||
const normalized = posts.map(normalizePost)
|
||||
|
||||
return normalized
|
||||
@@ -545,7 +545,6 @@ function buildBlocks(
|
||||
i++
|
||||
continue
|
||||
}
|
||||
|
||||
if (current.type === 'bulleted_list' || current.type === 'numbered_list') {
|
||||
const listItems: Block[] = []
|
||||
const targetType = current.type
|
||||
@@ -748,7 +747,7 @@ function convertBlock(
|
||||
}
|
||||
case 'bookmark': {
|
||||
const url =
|
||||
block.properties?.link_url?.[0]?.[0] ||
|
||||
block.properties?.link?.[0]?.[0] ||
|
||||
block.properties?.source?.[0]?.[0] ||
|
||||
block.properties?.title?.[0]?.[0] ||
|
||||
''
|
||||
@@ -758,7 +757,7 @@ function convertBlock(
|
||||
Type: 'bookmark',
|
||||
HasChildren: false,
|
||||
Bookmark: {
|
||||
Caption: parseRichTexts(block.properties?.caption),
|
||||
Caption: parseRichTexts(block.properties?.title),
|
||||
Url: url,
|
||||
},
|
||||
}
|
||||
|
||||
@@ -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