Skip to content

Commit 3688bab

Browse files
authored
Merge pull request #3168 from qixing-jk/fix-getPost-500
Fix get post 500 #3167
2 parents 1e6cdf6 + 1e21f9d commit 3688bab

File tree

2 files changed

+25
-5
lines changed

2 files changed

+25
-5
lines changed

lib/notion/getNotionPost.js

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import { idToUuid } from 'notion-utils'
33
import { defaultMapImageUrl } from 'react-notion-x'
44
import formatDate from '../utils/formatDate'
55
import { getPage } from './getPostBlocks'
6+
import { checkStrIsNotionId, checkStrIsUuid } from '@/lib/utils'
67

78
/**
89
* 根据页面ID获取内容
@@ -14,14 +15,22 @@ export async function getPost(pageId) {
1415
if (!blockMap) {
1516
return null
1617
}
17-
18-
const postInfo = blockMap?.block?.[idToUuid(pageId)].value
18+
if (checkStrIsNotionId(pageId)) {
19+
pageId = idToUuid(pageId)
20+
}
21+
if (!checkStrIsUuid(pageId)) {
22+
return null
23+
}
24+
const postInfo = blockMap?.block?.[pageId]?.value
25+
if (!postInfo) {
26+
return null
27+
}
1928
return {
2029
id: pageId,
21-
type: postInfo,
30+
type: postInfo.type,
2231
category: '',
2332
tags: [],
24-
title: postInfo?.properties?.title?.[0],
33+
title: postInfo?.properties?.title?.[0] || null,
2534
status: 'Published',
2635
createdTime: formatDate(
2736
new Date(postInfo.created_time).toString(),
@@ -32,7 +41,7 @@ export async function getPost(pageId) {
3241
BLOG.LANG
3342
),
3443
fullWidth: postInfo?.fullWidth || false,
35-
page_cover: getPageCover(postInfo) || BLOG.HOME_BANNER_IMAGE,
44+
page_cover: getPageCover(postInfo) || BLOG.HOME_BANNER_IMAGE || null,
3645
date: {
3746
start_date: formatDate(
3847
new Date(postInfo?.last_edited_time).toString(),

lib/utils/index.js

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,17 @@ export function checkStartWithHttp(str) {
104104
}
105105
}
106106

107+
// 检查一个字符串是否UUID https://ihateregex.io/expr/uuid/
108+
export function checkStrIsUuid(str) {
109+
if (!str) {
110+
return false
111+
}
112+
// 使用正则表达式进行匹配
113+
const regex = /^[0-9a-fA-F]{8}\b-[0-9a-fA-F]{4}\b-[0-9a-fA-F]{4}\b-[0-9a-fA-F]{4}\b-[0-9a-fA-F]{12}$/
114+
return regex.test(str)
115+
}
116+
117+
107118
// 检查一个字符串是否notionid : 32位,仅由数字英文构成
108119
export function checkStrIsNotionId(str) {
109120
if (!str) {

0 commit comments

Comments
 (0)