From 3310a60fe38fa0e8332fbb853e6f16c9e1a8f915 Mon Sep 17 00:00:00 2001 From: anime Date: Mon, 20 Jan 2025 12:57:16 +0800 Subject: [PATCH 1/2] =?UTF-8?q?fix(=E4=BF=AE=E5=A4=8DgetPost=20=E6=97=B6?= =?UTF-8?q?=E5=BC=82=E5=B8=B8=E5=80=BC=E5=AF=BC=E8=87=B4=E7=9A=84500?= =?UTF-8?q?=E5=92=8C=E6=97=A0=E6=B3=95=E6=AD=A3=E7=A1=AE=E6=B8=B2=E6=9F=93?= =?UTF-8?q?=E9=97=AE=E9=A2=98):?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit (cherry picked from commit cc3d0f81f93832c8671e8a48cd49c966ab8b279b) --- lib/notion/getNotionPost.js | 19 ++++++++++++++----- 1 file changed, 14 insertions(+), 5 deletions(-) diff --git a/lib/notion/getNotionPost.js b/lib/notion/getNotionPost.js index f5aabb2a15a..97ceea9a48d 100644 --- a/lib/notion/getNotionPost.js +++ b/lib/notion/getNotionPost.js @@ -3,6 +3,7 @@ import { idToUuid } from 'notion-utils' import { defaultMapImageUrl } from 'react-notion-x' import formatDate from '../utils/formatDate' import { getPage } from './getPostBlocks' +import { checkStrIsNotionId, checkStrIsUuid } from '@/lib/utils' /** * 根据页面ID获取内容 @@ -14,14 +15,22 @@ export async function getPost(pageId) { if (!blockMap) { return null } - - const postInfo = blockMap?.block?.[idToUuid(pageId)].value + if (checkStrIsNotionId(pageId)) { + pageId = idToUuid(pageId) + } + if (!checkStrIsUuid(pageId)) { + return null + } + const postInfo = blockMap?.block?.[pageId]?.value + if (!postInfo) { + return null + } return { id: pageId, - type: postInfo, + type: postInfo.type, category: '', tags: [], - title: postInfo?.properties?.title?.[0], + title: postInfo?.properties?.title?.[0] || null, status: 'Published', createdTime: formatDate( new Date(postInfo.created_time).toString(), @@ -32,7 +41,7 @@ export async function getPost(pageId) { BLOG.LANG ), fullWidth: postInfo?.fullWidth || false, - page_cover: getPageCover(postInfo) || BLOG.HOME_BANNER_IMAGE, + page_cover: getPageCover(postInfo) || BLOG.HOME_BANNER_IMAGE || null, date: { start_date: formatDate( new Date(postInfo?.last_edited_time).toString(), From 1e21f9dae76070c578ffb64f4bc5fb7f0833688f Mon Sep 17 00:00:00 2001 From: anime Date: Mon, 20 Jan 2025 13:04:16 +0800 Subject: [PATCH 2/2] =?UTF-8?q?feat(=E6=B7=BB=E5=8A=A0=E7=BC=BA=E5=A4=B1?= =?UTF-8?q?=E7=9A=84=20checkStrIsUuid):?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- lib/utils/index.js | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/lib/utils/index.js b/lib/utils/index.js index 787dc90ddf7..e12f2f85fc3 100644 --- a/lib/utils/index.js +++ b/lib/utils/index.js @@ -104,6 +104,17 @@ export function checkStartWithHttp(str) { } } +// 检查一个字符串是否UUID https://ihateregex.io/expr/uuid/ +export function checkStrIsUuid(str) { + if (!str) { + return false + } + // 使用正则表达式进行匹配 + 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}$/ + return regex.test(str) +} + + // 检查一个字符串是否notionid : 32位,仅由数字英文构成 export function checkStrIsNotionId(str) { if (!str) {