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
This commit is contained in:
anime
2025-07-09 13:51:43 +08:00
parent 44ff4bcb23
commit 72d64d7184

View File

@@ -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)
}
}