This commit is contained in:
2025-11-30 14:53:14 +08:00
parent 1e6c0c0ab9
commit 32fdaac7db
8 changed files with 122 additions and 81 deletions

View File

@@ -51,8 +51,9 @@ export type NotionBlock = {
export type NotionBlockMap = Record<string, NotionBlock>
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<RemotePostPayload>
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<TOCSection[]> {
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<TOCSection[]> {
})
}
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
}