From 72d64d7184db69bb9b01f82bf585d38ac541935f Mon Sep 17 00:00:00 2001 From: anime Date: Wed, 9 Jul 2025 13:51:43 +0800 Subject: [PATCH] feat(getPageContentText): enhance getPageContentText to handle nested block content - refactor getText function to process block properties and content - add support for recursive processing of nested block content - improve null checks and error handling in block processing - update getBlockContentText to handle block value safely --- lib/notion/getPageContentText.js | 27 ++++++++++++++++----------- 1 file changed, 16 insertions(+), 11 deletions(-) diff --git a/lib/notion/getPageContentText.js b/lib/notion/getPageContentText.js index 19130991..e4b79b37 100644 --- a/lib/notion/getPageContentText.js +++ b/lib/notion/getPageContentText.js @@ -3,17 +3,23 @@ import { getTextContent } from 'notion-utils' export function getPageContentText(post, pageBlockMap) { /** * 将对象的指定字段拼接到字符串 - * @param sourceTextArray - * @param targetObj - * @param key + * @param block * @returns string */ - function getText(targetObj) { - if (!targetObj) { + function getText(block) { + const result = [] + const properties = block.properties + if (!properties) { return '' } - const textArray = targetObj['title'] || targetObj['caption'] - return getTextArray(textArray) + const textArray = properties['title'] || properties['caption'] + result.push(getTextArray(textArray)) + if (block['content']?.length > 0) { + for (const blockContent of block.content) { + result.push(getBlockContentText(blockContent)) + } + } + return result.join('') } function getTextArray(textArray) { @@ -38,7 +44,7 @@ export function getPageContentText(post, pageBlockMap) { } function getBlockContentText(id) { - const block = pageBlockMap?.block[id].value + const block = pageBlockMap?.block[id]?.value if (!block) { return '' } @@ -51,7 +57,7 @@ export function getPageContentText(post, pageBlockMap) { return getTableText(block.content) case 'page': if (id !== postId) { - return getText(block.properties) + return getText(block) } return '' case 'breadcrumb': @@ -59,8 +65,7 @@ export function getPageContentText(post, pageBlockMap) { return '' case 'quote': default: - const properties = block?.properties - return getText(properties) + return getText(block) } }