mirror of
https://github.com/d0zingcat/NotionNext.git
synced 2026-06-08 15:10:37 +00:00
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:
@@ -3,17 +3,23 @@ import { getTextContent } from 'notion-utils'
|
|||||||
export function getPageContentText(post, pageBlockMap) {
|
export function getPageContentText(post, pageBlockMap) {
|
||||||
/**
|
/**
|
||||||
* 将对象的指定字段拼接到字符串
|
* 将对象的指定字段拼接到字符串
|
||||||
* @param sourceTextArray
|
* @param block
|
||||||
* @param targetObj
|
|
||||||
* @param key
|
|
||||||
* @returns string
|
* @returns string
|
||||||
*/
|
*/
|
||||||
function getText(targetObj) {
|
function getText(block) {
|
||||||
if (!targetObj) {
|
const result = []
|
||||||
|
const properties = block.properties
|
||||||
|
if (!properties) {
|
||||||
return ''
|
return ''
|
||||||
}
|
}
|
||||||
const textArray = targetObj['title'] || targetObj['caption']
|
const textArray = properties['title'] || properties['caption']
|
||||||
return getTextArray(textArray)
|
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) {
|
function getTextArray(textArray) {
|
||||||
@@ -38,7 +44,7 @@ export function getPageContentText(post, pageBlockMap) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
function getBlockContentText(id) {
|
function getBlockContentText(id) {
|
||||||
const block = pageBlockMap?.block[id].value
|
const block = pageBlockMap?.block[id]?.value
|
||||||
if (!block) {
|
if (!block) {
|
||||||
return ''
|
return ''
|
||||||
}
|
}
|
||||||
@@ -51,7 +57,7 @@ export function getPageContentText(post, pageBlockMap) {
|
|||||||
return getTableText(block.content)
|
return getTableText(block.content)
|
||||||
case 'page':
|
case 'page':
|
||||||
if (id !== postId) {
|
if (id !== postId) {
|
||||||
return getText(block.properties)
|
return getText(block)
|
||||||
}
|
}
|
||||||
return ''
|
return ''
|
||||||
case 'breadcrumb':
|
case 'breadcrumb':
|
||||||
@@ -59,8 +65,7 @@ export function getPageContentText(post, pageBlockMap) {
|
|||||||
return ''
|
return ''
|
||||||
case 'quote':
|
case 'quote':
|
||||||
default:
|
default:
|
||||||
const properties = block?.properties
|
return getText(block)
|
||||||
return getText(properties)
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user