mirror of
https://github.com/d0zingcat/NotionNext.git
synced 2026-05-13 15:09:13 +00:00
refactor(getPageContentText): Relocate getPageContentText to a dedicated file and eliminate the redundant isIterable function.
This commit is contained in:
118
lib/notion/getPageContentText.js
Normal file
118
lib/notion/getPageContentText.js
Normal file
@@ -0,0 +1,118 @@
|
||||
import { checkStrIsUuid, isIterable } from '@/lib/utils'
|
||||
|
||||
export function getPageContentText(post, pageBlockMap) {
|
||||
/**
|
||||
* 将对象的指定字段拼接到字符串
|
||||
* @param sourceTextArray
|
||||
* @param targetObj
|
||||
* @param key
|
||||
* @returns string
|
||||
*/
|
||||
function getText(targetObj) {
|
||||
if (!targetObj) {
|
||||
return ''
|
||||
}
|
||||
const textArray = targetObj['title'] || targetObj['caption']
|
||||
return getTextArray(textArray)
|
||||
}
|
||||
|
||||
function getTextArray(textArray) {
|
||||
const text = textArray ? getTextContent(textArray) : ''
|
||||
if (text && text !== 'Untitled') {
|
||||
return text
|
||||
}
|
||||
return ''
|
||||
}
|
||||
|
||||
const removeTypeFlag = ['a', 'p', '‣']
|
||||
|
||||
/**
|
||||
* 递归获取层层嵌套的数组
|
||||
* @param {*} textArray
|
||||
* @returns string
|
||||
*/
|
||||
function getTextContent(textArray) {
|
||||
if (typeof textArray === 'object' && isIterable(textArray)) {
|
||||
let result = ''
|
||||
for (const textObj of textArray) {
|
||||
if (textArray.length > 1 && removeTypeFlag.includes(textArray[0])) {
|
||||
return result
|
||||
}
|
||||
result = result + getTextContent(textObj)
|
||||
}
|
||||
return result
|
||||
} else if (typeof textArray === 'string') {
|
||||
if (checkStrIsUuid(textArray) && pageBlockMap.block[textArray]) {
|
||||
return getBlockContentText(textArray)
|
||||
} else if (textArray === pageBlockMap.block[postId].value.space_id) {
|
||||
return ''
|
||||
}
|
||||
return textArray
|
||||
}
|
||||
}
|
||||
|
||||
function getTransclusionReference(block) {
|
||||
const result = []
|
||||
const blockPointer = block.format.transclusion_reference_pointer
|
||||
const blockPointerId = blockPointer.id
|
||||
if (blockPointer) {
|
||||
const blockContentList = pageBlockMap.block[blockPointerId].value.content
|
||||
for (const blockContent of blockContentList) {
|
||||
result.push(getBlockContentText(blockContent))
|
||||
}
|
||||
}
|
||||
return result.join('')
|
||||
}
|
||||
|
||||
function getBlockContentText(id) {
|
||||
const block = pageBlockMap?.block[id].value
|
||||
const blockType = block.type
|
||||
switch (blockType) {
|
||||
case 'transclusion_reference':
|
||||
return getTransclusionReference(block)
|
||||
case 'table':
|
||||
return getTableText(block.content)
|
||||
case 'page':
|
||||
if (id !== postId) {
|
||||
return getText(block.properties)
|
||||
}
|
||||
return ''
|
||||
case 'breadcrumb':
|
||||
case 'divider':
|
||||
return ''
|
||||
case 'quote':
|
||||
default:
|
||||
const properties = block?.properties
|
||||
return getText(properties)
|
||||
}
|
||||
}
|
||||
|
||||
function getTableText(tableRowIds) {
|
||||
const result = []
|
||||
for (const blockRowId of tableRowIds) {
|
||||
if (pageBlockMap.block[blockRowId]) {
|
||||
const blockRow = pageBlockMap.block[blockRowId].value
|
||||
const blockRowProperties = blockRow.properties
|
||||
for (const blockRowPropertyValue of Object.values(blockRowProperties)) {
|
||||
result.push(getTextArray(blockRowPropertyValue))
|
||||
}
|
||||
}
|
||||
}
|
||||
return result.join('')
|
||||
}
|
||||
|
||||
const postId = post.id
|
||||
let contentTextList = []
|
||||
// 防止搜到加密文章的内容
|
||||
if (pageBlockMap && pageBlockMap.block && !post.password) {
|
||||
const contentIds = Object.keys(pageBlockMap.block)
|
||||
for (const id of contentIds) {
|
||||
const blockContentText = getBlockContentText(id)
|
||||
if (blockContentText) {
|
||||
contentTextList.push(blockContentText)
|
||||
}
|
||||
}
|
||||
}
|
||||
console.log(contentTextList.join(''))
|
||||
return contentTextList.join('')
|
||||
}
|
||||
@@ -1,6 +1,6 @@
|
||||
import BLOG from '@/blog.config'
|
||||
import { getPageContentText } from '@/pages/search/[keyword]'
|
||||
import algoliasearch from 'algoliasearch'
|
||||
import { getPageContentText } from '@/lib/notion/getPageContentText'
|
||||
|
||||
/**
|
||||
* 生成全文索引
|
||||
|
||||
@@ -6,11 +6,11 @@ import { getPostBlocks } from '@/lib/db/getSiteData'
|
||||
import { getPageTableOfContents } from '@/lib/notion/getPageTableOfContents'
|
||||
import { siteConfig } from '@/lib/config'
|
||||
import { getDataFromCache, setDataToCache } from '@/lib/cache/cache_manager'
|
||||
import { getPageContentText } from '@/pages/search/[keyword]'
|
||||
import { getAiSummary } from '@/lib/plugins/aiSummary'
|
||||
import BLOG from '@/blog.config'
|
||||
import { uploadDataToAlgolia } from '@/lib/plugins/algolia'
|
||||
import { countWords } from '@/lib/plugins/wordCount'
|
||||
import { getPageContentText } from '@/lib/notion/getPageContentText'
|
||||
|
||||
/**
|
||||
* 获取文章的关联推荐文章列表,目前根据标签关联性筛选
|
||||
|
||||
@@ -3,7 +3,7 @@ import { getDataFromCache } from '@/lib/cache/cache_manager'
|
||||
import { siteConfig } from '@/lib/config'
|
||||
import { getGlobalData } from '@/lib/db/getSiteData'
|
||||
import { DynamicLayout } from '@/themes/theme'
|
||||
import { checkStrIsUuid } from '@/lib/utils'
|
||||
import { getPageContentText } from '@/lib/notion/getPageContentText'
|
||||
|
||||
const Index = props => {
|
||||
const theme = siteConfig('THEME', BLOG.THEME, props.NOTION_CONFIG)
|
||||
@@ -59,14 +59,6 @@ export function getStaticPaths() {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 对象是否可以遍历
|
||||
* @param {*} obj
|
||||
* @returns
|
||||
*/
|
||||
const isIterable = obj =>
|
||||
obj != null && typeof obj[Symbol.iterator] === 'function'
|
||||
|
||||
/**
|
||||
* 在内存缓存中进行全文索引
|
||||
* @param {*} allPosts
|
||||
@@ -116,121 +108,4 @@ async function filterByMemCache(allPosts, keyword) {
|
||||
return filterPosts
|
||||
}
|
||||
|
||||
export function getPageContentText(post, pageBlockMap) {
|
||||
/**
|
||||
* 将对象的指定字段拼接到字符串
|
||||
* @param sourceTextArray
|
||||
* @param targetObj
|
||||
* @param key
|
||||
* @returns string
|
||||
*/
|
||||
function getText(targetObj) {
|
||||
if (!targetObj) {
|
||||
return ''
|
||||
}
|
||||
const textArray = targetObj['title'] || targetObj['caption']
|
||||
return getTextArray(textArray)
|
||||
}
|
||||
|
||||
function getTextArray(textArray) {
|
||||
const text = textArray ? getTextContent(textArray) : ''
|
||||
if (text && text !== 'Untitled') {
|
||||
return text
|
||||
}
|
||||
return ''
|
||||
}
|
||||
|
||||
const removeTypeFlag = ['a', 'p', '‣']
|
||||
|
||||
/**
|
||||
* 递归获取层层嵌套的数组
|
||||
* @param {*} textArray
|
||||
* @returns string
|
||||
*/
|
||||
function getTextContent(textArray) {
|
||||
if (typeof textArray === 'object' && isIterable(textArray)) {
|
||||
let result = ''
|
||||
for (const textObj of textArray) {
|
||||
if (textArray.length > 1 && removeTypeFlag.includes(textArray[0])) {
|
||||
return result
|
||||
}
|
||||
result = result + getTextContent(textObj)
|
||||
}
|
||||
return result
|
||||
} else if (typeof textArray === 'string') {
|
||||
if (checkStrIsUuid(textArray) && pageBlockMap.block[textArray]) {
|
||||
return getBlockContentText(textArray)
|
||||
} else if (textArray === pageBlockMap.block[postId].value.space_id) {
|
||||
return ''
|
||||
}
|
||||
return textArray
|
||||
}
|
||||
}
|
||||
|
||||
function getTransclusionReference(block) {
|
||||
const result = []
|
||||
const blockPointer = block.format.transclusion_reference_pointer
|
||||
const blockPointerId = blockPointer.id
|
||||
if (blockPointer) {
|
||||
const blockContentList = pageBlockMap.block[blockPointerId].value.content
|
||||
for (const blockContent of blockContentList) {
|
||||
result.push(getBlockContentText(blockContent))
|
||||
}
|
||||
}
|
||||
return result.join('')
|
||||
}
|
||||
|
||||
function getBlockContentText(id) {
|
||||
const block = pageBlockMap?.block[id].value
|
||||
const blockType = block.type
|
||||
switch (blockType) {
|
||||
case 'transclusion_reference':
|
||||
return getTransclusionReference(block)
|
||||
case 'table':
|
||||
return getTableText(block.content)
|
||||
case 'page':
|
||||
if (id !== postId) {
|
||||
return getText(block.properties)
|
||||
}
|
||||
return ''
|
||||
case 'breadcrumb':
|
||||
case 'divider':
|
||||
return ''
|
||||
case 'quote':
|
||||
default:
|
||||
const properties = block?.properties
|
||||
return getText(properties)
|
||||
}
|
||||
}
|
||||
|
||||
function getTableText(tableRowIds) {
|
||||
const result = []
|
||||
for (const blockRowId of tableRowIds) {
|
||||
if (pageBlockMap.block[blockRowId]) {
|
||||
const blockRow = pageBlockMap.block[blockRowId].value
|
||||
const blockRowProperties = blockRow.properties
|
||||
for (const blockRowPropertyValue of Object.values(blockRowProperties)) {
|
||||
result.push(getTextArray(blockRowPropertyValue))
|
||||
}
|
||||
}
|
||||
}
|
||||
return result.join('')
|
||||
}
|
||||
|
||||
const postId = post.id
|
||||
let contentTextList = []
|
||||
// 防止搜到加密文章的内容
|
||||
if (pageBlockMap && pageBlockMap.block && !post.password) {
|
||||
const contentIds = Object.keys(pageBlockMap.block)
|
||||
for (const id of contentIds) {
|
||||
const blockContentText = getBlockContentText(id)
|
||||
if (blockContentText) {
|
||||
contentTextList.push(blockContentText)
|
||||
}
|
||||
}
|
||||
}
|
||||
console.log(contentTextList.join(''))
|
||||
return contentTextList.join('')
|
||||
}
|
||||
|
||||
export default Index
|
||||
|
||||
Reference in New Issue
Block a user