From b4ba7d8f23a41294421c307e4633755ac421c907 Mon Sep 17 00:00:00 2001 From: anime Date: Tue, 8 Jul 2025 00:28:58 +0800 Subject: [PATCH] refactor(getPageContentText): Relocate getPageContentText to a dedicated file and eliminate the redundant isIterable function. --- lib/notion/getPageContentText.js | 118 ++++++++++++++++++++++++++++ lib/plugins/algolia.js | 2 +- lib/utils/post.js | 2 +- pages/search/[keyword]/index.js | 127 +------------------------------ 4 files changed, 121 insertions(+), 128 deletions(-) create mode 100644 lib/notion/getPageContentText.js diff --git a/lib/notion/getPageContentText.js b/lib/notion/getPageContentText.js new file mode 100644 index 00000000..e636fab4 --- /dev/null +++ b/lib/notion/getPageContentText.js @@ -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('') +} diff --git a/lib/plugins/algolia.js b/lib/plugins/algolia.js index e6c76422..fceb7acc 100644 --- a/lib/plugins/algolia.js +++ b/lib/plugins/algolia.js @@ -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' /** * 生成全文索引 diff --git a/lib/utils/post.js b/lib/utils/post.js index a3ee0f91..e4a22d2e 100644 --- a/lib/utils/post.js +++ b/lib/utils/post.js @@ -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' /** * 获取文章的关联推荐文章列表,目前根据标签关联性筛选 diff --git a/pages/search/[keyword]/index.js b/pages/search/[keyword]/index.js index f0e63668..cb486895 100644 --- a/pages/search/[keyword]/index.js +++ b/pages/search/[keyword]/index.js @@ -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