diff --git a/lib/notion/getPageTableOfContents.js b/lib/notion/getPageTableOfContents.js index a15ebe2d..a23be7cb 100644 --- a/lib/notion/getPageTableOfContents.js +++ b/lib/notion/getPageTableOfContents.js @@ -12,31 +12,8 @@ const indentLevels = { * H1, H2, and H3 elements. */ export const getPageTableOfContents = (page, recordMap) => { - const toc = (page.content ?? []).map((blockId) => { - const block = recordMap.block[blockId]?.value - - if (block) { - const { type } = block - - if ( - type === 'header' || - type === 'sub_header' || - type === 'sub_sub_header' - ) { - return { - id: blockId, - type, - text: getTextContent(block.properties?.title), - indentLevel: indentLevels[type] - } - } - } - - return null - }).filter(e => e) - - console.log('目录', toc) - + const contents = (page.content ?? []) + const toc = getBlockHeader(contents, recordMap) const indentLevelStack = [ { actual: -1, @@ -74,3 +51,40 @@ export const getPageTableOfContents = (page, recordMap) => { return toc } + +/** + * 重写获取目录方法 + */ +function getBlockHeader(contents, recordMap, toc) { + if (!toc) { + toc = [] + } + if (!contents) { + return toc + } + + for (const blockId of contents) { + const block = recordMap.block[blockId]?.value + if (!block) { + continue + } + const { type } = block + if (type.indexOf('header') >= 0) { + const existed = toc.find(e => e.id === blockId) + if (!existed) { + toc.push({ + id: blockId, + type, + text: getTextContent(block.properties?.title), + indentLevel: indentLevels[type] + }) + } + } + + if (block.content?.length > 0) { + getBlockHeader(block.content, recordMap, toc) + } + } + + return toc +} diff --git a/styles/notion.css b/styles/notion.css index d010bb0c..d55d8456 100644 --- a/styles/notion.css +++ b/styles/notion.css @@ -427,7 +427,7 @@ margin-top: 2px; } */ .notion-h2 { - font-size: 1.5em; + font-size: 1.4em; margin-top: 1.1em; } .notion-h3 {