diff --git a/lib/notion/getPostBlocks.js b/lib/notion/getPostBlocks.js index 469fa8f7..54dde7ff 100644 --- a/lib/notion/getPostBlocks.js +++ b/lib/notion/getPostBlocks.js @@ -81,23 +81,41 @@ export async function getPageWithRetry(id, from, retryAttempts = 3) { function filterPostBlocks(id, blockMap, slice) { const clonePageBlock = deepClone(blockMap) let count = 0 + const blocksToProcess = Object.keys(clonePageBlock?.block || {}) // 循环遍历文档的每个block for (const i in clonePageBlock?.block) { - const b = clonePageBlock?.block[i] + for (let i = 0; i < blocksToProcess.length; i++) { + const blockId = blocksToProcess[i] + const b = clonePageBlock?.block[blockId] + if (slice && slice > 0 && count > slice) { - delete clonePageBlock?.block[i] + delete clonePageBlock?.block[blockId] continue } - // 当BlockId等于PageId时移除 + if (b?.value?.id === id) { - // 此block含有敏感信息 delete b?.value?.properties continue } - + count++ - // 处理 c++、c#、汇编等语言名字映射 + + if (b?.value?.type === 'sync_block' && b?.value?.children) { + const childBlocks = b.value.children + // Remove the sync block + delete clonePageBlock.block[blockId] + // Insert child blocks in place of the sync block + childBlocks.forEach((childBlock, index) => { + const newBlockId = `${blockId}_child_${index}` + clonePageBlock.block[newBlockId] = childBlock + blocksToProcess.splice(i + index + 1, 0, newBlockId) + }) + // Adjust the loop counter to process the newly inserted blocks + i-- + continue + } + if (b?.value?.type === 'code') { if (b?.value?.properties?.language?.[0][0] === 'C++') { b.value.properties.language[0][0] = 'cpp' @@ -109,8 +127,7 @@ function filterPostBlocks(id, blockMap, slice) { b.value.properties.language[0][0] = 'asm6502' } } - - // 如果是文件,或嵌入式PDF,需要重新加密签名 + if ( (b?.value?.type === 'file' || b?.value?.type === 'pdf' || @@ -123,16 +140,6 @@ function filterPostBlocks(id, blockMap, slice) { const newUrl = `https://notion.so/signed/${encodeURIComponent(oldUrl)}?table=block&id=${b?.value?.id}` b.value.properties.source[0][0] = newUrl } - - if (b?.value?.type === 'synced_block' && b?.value?.properties?.children) { - const childBlocks = b.value.properties.children; - delete clonePageBlock.block[blockId]; - // Insert child blocks at the sync block's position - blockIds.splice(i + 1, 0, ...childBlocks); - // Adjust the loop counter to process the newly inserted blocks - i--; - continue; - } } // 去掉不用的字段