File tree Expand file tree Collapse file tree 2 files changed +25
-5
lines changed
Expand file tree Collapse file tree 2 files changed +25
-5
lines changed Original file line number Diff line number Diff line change @@ -3,6 +3,7 @@ import { idToUuid } from 'notion-utils'
33import { defaultMapImageUrl } from 'react-notion-x'
44import formatDate from '../utils/formatDate'
55import { 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 ( ) ,
Original file line number Diff line number Diff 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 - 9 a - f A - F ] { 8 } \b - [ 0 - 9 a - f A - F ] { 4 } \b - [ 0 - 9 a - f A - F ] { 4 } \b - [ 0 - 9 a - f A - F ] { 4 } \b - [ 0 - 9 a - f A - F ] { 12 } $ /
114+ return regex . test ( str )
115+ }
116+
117+
107118// 检查一个字符串是否notionid : 32位,仅由数字英文构成
108119export function checkStrIsNotionId ( str ) {
109120 if ( ! str ) {
You can’t perform that action at this time.
0 commit comments