rewrite.toc.

This commit is contained in:
tangly1024
2022-12-15 10:37:37 +08:00
parent 727fa0e2e6
commit 068639647d
2 changed files with 40 additions and 26 deletions

View File

@@ -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
}

View File

@@ -427,7 +427,7 @@
margin-top: 2px;
} */
.notion-h2 {
font-size: 1.5em;
font-size: 1.4em;
margin-top: 1.1em;
}
.notion-h3 {