Skip to content

Commit 470cbe7

Browse files
committed
fix: headings in synced blocks not shown in toc
1 parent 6493adc commit 470cbe7

File tree

1 file changed

+15
-2
lines changed

1 file changed

+15
-2
lines changed

packages/notion-utils/src/get-page-table-of-contents.ts

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,12 @@ export const getPageTableOfContents = (
2323
page: types.PageBlock,
2424
recordMap: types.ExtendedRecordMap
2525
): Array<TableOfContentsEntry> => {
26-
const toc = (page.content ?? [])
27-
.map((blockId: string) => {
26+
type MapResult = TableOfContentsEntry | null | MapResult[]
27+
28+
// Maps `content` property to TOC entries.
29+
// Pages and transclusion containers (synced blocks) both have the property.
30+
function mapContentToEntries(content?: string[]): MapResult[] {
31+
return (content ?? []).map((blockId: string) => {
2832
const block = recordMap.block[blockId]?.value
2933

3034
if (block) {
@@ -42,10 +46,19 @@ export const getPageTableOfContents = (
4246
indentLevel: indentLevels[type]
4347
}
4448
}
49+
50+
if (type === 'transclusion_container') {
51+
return mapContentToEntries(block.content)
52+
}
4553
}
4654

4755
return null
4856
})
57+
}
58+
59+
const toc = mapContentToEntries(page.content)
60+
// Synced blocks cannot be nested. So theoretically a 1-level flattening is enough.
61+
.flat()
4962
.filter(Boolean) as Array<TableOfContentsEntry>
5063

5164
const indentLevelStack = [

0 commit comments

Comments
 (0)