From c8c06d0bba97c488a1da8ac7c21d50031dfcdf3d Mon Sep 17 00:00:00 2001 From: DiegoxK Date: Tue, 6 Feb 2024 17:35:18 -0500 Subject: [PATCH 1/2] Refactor table of contents generation --- .../src/get-page-table-of-contents.ts | 69 ++++++++++++------- yarn.lock | 5 -- 2 files changed, 45 insertions(+), 29 deletions(-) diff --git a/packages/notion-utils/src/get-page-table-of-contents.ts b/packages/notion-utils/src/get-page-table-of-contents.ts index ebc7a472..46ef9ab0 100644 --- a/packages/notion-utils/src/get-page-table-of-contents.ts +++ b/packages/notion-utils/src/get-page-table-of-contents.ts @@ -15,6 +15,50 @@ const indentLevels = { sub_sub_header: 2 } +/** + * Recursive function to traverse blocks and build the table of contents. + */ +const traverseBlocks = ( + blockIds: string[], + recordMap: types.ExtendedRecordMap, + indentLevel: number +): Array => { + const toc: Array = [] + + for (const blockId of blockIds) { + const block = recordMap.block[blockId]?.value + + if (block) { + const { type } = block + + if ( + type === 'header' || + type === 'sub_header' || + type === 'sub_sub_header' + ) { + toc.push({ + id: blockId, + type, + text: getTextContent(block.properties?.title), + indentLevel: indentLevels[type] + }) + } + + // If the block has content, recursively traverse it + if (block.content) { + const nestedHeaders = traverseBlocks( + block.content, + recordMap, + indentLevel + 1 + ) + toc.push(...nestedHeaders) + } + } + } + + return toc +} + /** * Gets the metadata for a table of contents block by parsing the page's * H1, H2, and H3 elements. @@ -23,30 +67,7 @@ export const getPageTableOfContents = ( page: types.PageBlock, recordMap: types.ExtendedRecordMap ): Array => { - const toc = (page.content ?? []) - .map((blockId: string) => { - 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(Boolean) as Array + const toc = traverseBlocks(page.content ?? [], recordMap, 0) const indentLevelStack = [ { diff --git a/yarn.lock b/yarn.lock index ff820bf5..42639e43 100644 --- a/yarn.lock +++ b/yarn.lock @@ -9602,11 +9602,6 @@ normalize-url@^7.0.3: resolved "https://registry.npmjs.org/normalize-url/-/normalize-url-7.0.3.tgz" integrity sha512-RiCOdwdPnzvwcBFJE4iI1ss3dMVRIrEzFpn8ftje6iBfzBInqlnRrNhxcLwBEKjPPXQKzm1Ptlxtaiv9wdcj5w== -notion-types@^6.15.6: - version "6.15.6" - resolved "https://registry.yarnpkg.com/notion-types/-/notion-types-6.15.6.tgz#eabbb28e1c514f421f0ffbf06ecdecd90e8ec8e3" - integrity sha512-JgLWDN4oHg/1sNdHDCeKUfdPl1AYsjOTnYkq+Zn7vITPykxbhw7nIxbAJ7owWUTro1cYTPh+GVmdX0mPiZGujg== - npm-bundled@^1.1.1: version "1.1.2" resolved "https://registry.npmjs.org/npm-bundled/-/npm-bundled-1.1.2.tgz" From 67fc4aab8df514fe0782f0773ee28fd1f15543ed Mon Sep 17 00:00:00 2001 From: DiegoxK Date: Tue, 6 Feb 2024 18:00:09 -0500 Subject: [PATCH 2/2] Removed indentLevel parameter from the traverseBlocks function --- .../notion-utils/src/get-page-table-of-contents.ts | 11 +++-------- 1 file changed, 3 insertions(+), 8 deletions(-) diff --git a/packages/notion-utils/src/get-page-table-of-contents.ts b/packages/notion-utils/src/get-page-table-of-contents.ts index 46ef9ab0..e6d2dab7 100644 --- a/packages/notion-utils/src/get-page-table-of-contents.ts +++ b/packages/notion-utils/src/get-page-table-of-contents.ts @@ -20,8 +20,7 @@ const indentLevels = { */ const traverseBlocks = ( blockIds: string[], - recordMap: types.ExtendedRecordMap, - indentLevel: number + recordMap: types.ExtendedRecordMap ): Array => { const toc: Array = [] @@ -46,11 +45,7 @@ const traverseBlocks = ( // If the block has content, recursively traverse it if (block.content) { - const nestedHeaders = traverseBlocks( - block.content, - recordMap, - indentLevel + 1 - ) + const nestedHeaders = traverseBlocks(block.content, recordMap) toc.push(...nestedHeaders) } } @@ -67,7 +62,7 @@ export const getPageTableOfContents = ( page: types.PageBlock, recordMap: types.ExtendedRecordMap ): Array => { - const toc = traverseBlocks(page.content ?? [], recordMap, 0) + const toc = traverseBlocks(page.content ?? [], recordMap) const indentLevelStack = [ {