mirror of
https://github.com/d0zingcat/NotionNext.git
synced 2026-05-13 23:16:47 +00:00
rewrite.toc.
This commit is contained in:
@@ -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
|
||||
}
|
||||
|
||||
@@ -427,7 +427,7 @@
|
||||
margin-top: 2px;
|
||||
} */
|
||||
.notion-h2 {
|
||||
font-size: 1.5em;
|
||||
font-size: 1.4em;
|
||||
margin-top: 1.1em;
|
||||
}
|
||||
.notion-h3 {
|
||||
|
||||
Reference in New Issue
Block a user