mirror of
https://github.com/d0zingcat/NotionNext.git
synced 2026-05-29 15:10:06 +00:00
@@ -212,17 +212,16 @@ async function getPageRecordMapByNotionAPI({ pageId, from }) {
|
||||
collectionData.push(properties)
|
||||
}
|
||||
}
|
||||
// 读取映射 配置
|
||||
let postCount = 0
|
||||
|
||||
// 获取page作为自定义菜单
|
||||
const customNav = getCustomNav({ allPages: collectionData.filter(post => post.type === 'Page' && post.status === 'Published') })
|
||||
|
||||
// 文章计数
|
||||
let postCount = 0
|
||||
const allPages = collectionData.filter(post => {
|
||||
if (post.type === 'Post' && post.status === 'Published') {
|
||||
postCount++
|
||||
}
|
||||
|
||||
return post &&
|
||||
post.type &&
|
||||
(post.type === 'Post' || post.type === 'Page') &&
|
||||
|
||||
@@ -3,6 +3,7 @@ import { NotionAPI } from 'notion-client'
|
||||
import BLOG from '@/blog.config'
|
||||
import formatDate from '../formatDate'
|
||||
import { defaultMapImageUrl } from 'react-notion-x'
|
||||
import md5 from 'js-md5'
|
||||
|
||||
export default async function getPageProperties(id, block, schema, authToken, tagOptions, siteInfo) {
|
||||
const rawProperties = Object.entries(block?.[id]?.value?.properties || [])
|
||||
@@ -89,6 +90,7 @@ export default async function getPageProperties(id, block, schema, authToken, ta
|
||||
properties.pageIcon = getImageUrl(block[id].value?.format?.page_icon, block[id].value) ?? ''
|
||||
properties.page_cover = getImageUrl(block[id].value?.format?.page_cover, block[id].value) ?? siteInfo?.pageCover
|
||||
properties.content = value.content ?? []
|
||||
properties.password = properties.password ? md5(properties.slug + properties.password) : ''
|
||||
properties.tagItems = properties?.tags?.map(tag => {
|
||||
return { name: tag, color: tagOptions?.find(t => t.value === tag)?.color || 'gray' }
|
||||
}) || []
|
||||
|
||||
90
lib/notion/getPageTableOfContents.js
Normal file
90
lib/notion/getPageTableOfContents.js
Normal file
@@ -0,0 +1,90 @@
|
||||
import { getTextContent } from 'notion-utils'
|
||||
|
||||
const indentLevels = {
|
||||
header: 0,
|
||||
sub_header: 1,
|
||||
sub_sub_header: 2
|
||||
}
|
||||
|
||||
/**
|
||||
* @see https://github.com/NotionX/react-notion-x/blob/master/packages/notion-utils/src/get-page-table-of-contents.ts
|
||||
* Gets the metadata for a table of contents block by parsing the page's
|
||||
* H1, H2, and H3 elements.
|
||||
*/
|
||||
export const getPageTableOfContents = (page, recordMap) => {
|
||||
const contents = (page.content ?? [])
|
||||
const toc = getBlockHeader(contents, recordMap)
|
||||
const indentLevelStack = [
|
||||
{
|
||||
actual: -1,
|
||||
effective: -1
|
||||
}
|
||||
]
|
||||
|
||||
// Adjust indent levels to always change smoothly.
|
||||
// This is a little tricky, but the key is that when increasing indent levels,
|
||||
// they should never jump more than one at a time.
|
||||
for (const tocItem of toc) {
|
||||
const { indentLevel } = tocItem
|
||||
const actual = indentLevel
|
||||
|
||||
do {
|
||||
const prevIndent = indentLevelStack[indentLevelStack.length - 1]
|
||||
const { actual: prevActual, effective: prevEffective } = prevIndent
|
||||
|
||||
if (actual > prevActual) {
|
||||
tocItem.indentLevel = prevEffective + 1
|
||||
indentLevelStack.push({
|
||||
actual,
|
||||
effective: tocItem.indentLevel
|
||||
})
|
||||
} else if (actual === prevActual) {
|
||||
tocItem.indentLevel = prevEffective
|
||||
break
|
||||
} else {
|
||||
indentLevelStack.pop()
|
||||
}
|
||||
|
||||
// eslint-disable-next-line no-constant-condition
|
||||
} while (true)
|
||||
}
|
||||
|
||||
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
|
||||
}
|
||||
Reference in New Issue
Block a user