Some customization

This commit is contained in:
2025-11-28 13:44:26 +08:00
parent f9a7c123cc
commit 985164f4c5
13 changed files with 2264 additions and 235 deletions

View File

@@ -1,24 +1,86 @@
import { getCollection, render, type CollectionEntry } from 'astro:content'
import { readingTime, calculateWordCountFromHtml } from '@/lib/utils'
type NotionPost = {
id: string
slug?: string
Content?: string
excerpt?: string
Tags?: string[]
Published?: boolean
'Published Date'?: string
created_time?: string
last_edited_time?: string
}
export interface LinkEntry {
id: string
picLink?: string
published?: boolean
Description?: string
links: string
name: string
created_time?: string
last_edited_time?: string
}
export async function getAllAuthors(): Promise<CollectionEntry<'authors'>[]> {
return await getCollection('authors')
}
export const POSTS_API_URL = 'https://notion-api.nvme0n1p.dev/v2/posts'
export async function getAllPosts(): Promise<CollectionEntry<'blog'>[]> {
const posts = await getCollection('blog')
return posts
.filter((post) => !post.data.draft && !isSubpost(post.id))
.sort((a, b) => b.data.date.valueOf() - a.data.date.valueOf())
const fallback = async () => {
const posts = await getCollection('blog')
return posts
.filter((post) => !post.data.draft && !isSubpost(post.id))
.sort((a, b) => b.data.date.valueOf() - a.data.date.valueOf())
}
try {
const res = await fetch(POSTS_API_URL)
if (!res.ok) throw new Error(`Failed to fetch posts: ${res.status}`)
const payload = (await res.json()) as { posts?: NotionPost[] }
const posts = (payload.posts ?? []).filter(
(post) => post.Published ?? true,
)
const normalized = posts.map((post) => {
const dateString =
post['Published Date'] ?? post.created_time ?? new Date().toISOString()
const date = new Date(dateString)
const id = post.slug || post.id
return {
id,
collection: 'blog',
data: {
title: post.Content || id || 'Untitled',
description: (post as any).excerpt || '',
date,
tags: Array.isArray(post.Tags) ? post.Tags : [],
draft: !(post.Published ?? true),
authors: [],
},
body: '',
}
})
return normalized
.sort((a, b) => b.data.date.valueOf() - a.data.date.valueOf())
.filter((post) => !isSubpost(post.id)) as unknown as CollectionEntry<'blog'>[]
} catch (error) {
console.error('getAllPosts remote fetch failed, using fallback:', error)
return fallback()
}
}
export async function getAllPostsAndSubposts(): Promise<
CollectionEntry<'blog'>[]
> {
const posts = await getCollection('blog')
return posts
.filter((post) => !post.data.draft)
.sort((a, b) => b.data.date.valueOf() - a.data.date.valueOf())
// 远程源不区分子文章,直接沿用 getAllPosts 结果
return getAllPosts()
}
export async function getAllProjects(): Promise<CollectionEntry<'projects'>[]> {
@@ -179,6 +241,38 @@ export function isSubpost(postId: string): boolean {
return postId.includes('/')
}
export const FRIENDS_API_URL = 'https://notion-api.nvme0n1p.dev/v2/links'
export async function fetchRemotePost(
slug: string,
): Promise<NotionPost | null> {
try {
const res = await fetch(`${POSTS_API_URL}/${slug}`)
if (!res.ok) throw new Error(`Failed to fetch post: ${res.status}`)
const data = (await res.json()) as NotionPost
return data
} catch (error) {
console.error(`fetchRemotePost error for slug "${slug}":`, error)
return null
}
}
export async function getFriendLinks(): Promise<LinkEntry[]> {
const fallback: LinkEntry[] = []
try {
const res = await fetch(FRIENDS_API_URL)
if (!res.ok) throw new Error(`Failed to fetch links: ${res.status}`)
const data = (await res.json()) as LinkEntry[]
if (!Array.isArray(data)) return fallback
return data
} catch (error) {
console.error('getFriendLinks error:', error)
return fallback
}
}
export async function getParentPost(
subpostId: string,
): Promise<CollectionEntry<'blog'> | null> {