mirror of
https://github.com/d0zingcat/NotionNext.git
synced 2026-05-14 07:26:52 +00:00
feat(getPageContentText): add flexible property value retrieval and support for more block types
- Add getPropertyValue helper function for flexible property retrieval - Enhance getText function to accept custom keys for property lookup - Add support for additional block types: image, bookmark, callout, header - Improve documentation with JSDoc comments for better code understanding
This commit is contained in:
@@ -1,18 +1,34 @@
|
||||
import { getTextContent } from 'notion-utils'
|
||||
|
||||
/**
|
||||
* 获取属性值,优先从 overrides 中读取,否则按顺序从 properties 中读取,最后返回默认值
|
||||
* @param {Object} properties 原始属性对象
|
||||
* @param {Array} keys 优先级字段名列表
|
||||
* @param {Object} overrides 自定义覆盖对象(可选)
|
||||
* @param {string} defaultValue 默认值(可选)
|
||||
*/
|
||||
function getPropertyValue(properties, keys, overrides = {}, defaultValue = '') {
|
||||
for (const key of keys) {
|
||||
if (overrides[key]) return overrides[key]
|
||||
if (properties[key]) return properties[key]
|
||||
}
|
||||
return defaultValue
|
||||
}
|
||||
|
||||
export function getPageContentText(post, pageBlockMap) {
|
||||
/**
|
||||
* 将对象的指定字段拼接到字符串
|
||||
* @param block
|
||||
* @param customKeys 优先级字段名列表
|
||||
* @returns string
|
||||
*/
|
||||
function getText(block) {
|
||||
function getText(block, customKeys = ['title', 'caption']) {
|
||||
const result = []
|
||||
const properties = block.properties
|
||||
if (!properties) {
|
||||
return ''
|
||||
}
|
||||
const textArray = properties['title'] || properties['caption']
|
||||
const textArray = getPropertyValue(properties, customKeys)
|
||||
result.push(getTextArray(textArray))
|
||||
if (block.type !== 'page' && block['content']?.length > 0) {
|
||||
for (const blockContent of block.content) {
|
||||
@@ -61,9 +77,20 @@ export function getPageContentText(post, pageBlockMap) {
|
||||
}
|
||||
return ''
|
||||
case 'breadcrumb':
|
||||
case 'external_object_instance':
|
||||
case 'divider':
|
||||
return ''
|
||||
case 'image':
|
||||
return getText(block, ['alt_text', 'title'])
|
||||
// 除title以外,还有额外的link和description可供索引,但认为不需要
|
||||
case 'bookmark':
|
||||
case 'quote':
|
||||
case 'callout':
|
||||
case 'header':
|
||||
case 'sub_header':
|
||||
case 'code':
|
||||
case 'equation':
|
||||
case 'text':
|
||||
default:
|
||||
return getText(block)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user