mirror of
https://github.com/lbr77/blog-astro.git
synced 2026-04-09 00:19:12 +00:00
More customzation
This commit is contained in:
40
src/lib/notion/client.ts
Normal file
40
src/lib/notion/client.ts
Normal file
@@ -0,0 +1,40 @@
|
||||
import { POSTS_API_URL } from '../data-utils'
|
||||
import type { Post } from '../interfaces'
|
||||
|
||||
export async function getPostByPageId(pageId: string): Promise<Post | null> {
|
||||
if (!pageId) return null
|
||||
|
||||
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?: any[] }
|
||||
const match = (payload.posts ?? []).find((post) => post.id === pageId)
|
||||
if (!match) return null
|
||||
|
||||
const dateString =
|
||||
match['Published Date'] || match.created_time || new Date().toISOString()
|
||||
|
||||
return {
|
||||
PageId: match.id,
|
||||
Title: match.Content || match.slug || 'Untitled',
|
||||
Icon: null,
|
||||
Cover: null,
|
||||
Slug: match.slug || match.id,
|
||||
Date: dateString,
|
||||
Tags: Array.isArray(match.Tags)
|
||||
? match.Tags.map((name: string) => ({
|
||||
id: name,
|
||||
name,
|
||||
color: 'default',
|
||||
}))
|
||||
: [],
|
||||
Excerpt: match.excerpt || '',
|
||||
FeaturedImage: null,
|
||||
Rank: Number(match.rank || 0),
|
||||
}
|
||||
} catch (error) {
|
||||
console.error('getPostByPageId error:', error)
|
||||
return null
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user