Skip to content

Commit 67da2b8

Browse files
committed
2 parents 6f1f994 + 59025e8 commit 67da2b8

12 files changed

+138
-84
lines changed

components/ExternalPlugins.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import TianLiGPT from './TianliGPT'
66
import WebWhiz from './Webwhiz'
77

88
import { CUSTOM_EXTERNAL_CSS, CUSTOM_EXTERNAL_JS } from '@/blog.config'
9-
import { mapPageUrl } from '@/lib/notion/mapPageUrl'
9+
import { convertInnerUrl } from '@/lib/notion/convertInnerUrl'
1010
import { isBrowser, loadExternalResource } from '@/lib/utils'
1111
import { useRouter } from 'next/router'
1212
import { useEffect } from 'react'
@@ -106,7 +106,7 @@ const ExternalPlugin = props => {
106106
}
107107

108108
// 映射url
109-
mapPageUrl(props?.allNavPages)
109+
convertInnerUrl(props?.allNavPages)
110110
}, [router])
111111

112112
useEffect(() => {

lib/db/getSiteData.js

+88-33
Original file line numberDiff line numberDiff line change
@@ -7,16 +7,16 @@ import { getConfigMapFromConfigPage } from '@/lib/notion/getNotionConfig'
77
import getPageProperties, {
88
adjustPageProperties
99
} from '@/lib/notion/getPageProperties'
10-
import { fetchInBatches, getPostBlocks } from '@/lib/notion/getPostBlocks'
10+
import { fetchInBatches, getPage } from '@/lib/notion/getPostBlocks'
1111
import { compressImage, mapImgUrl } from '@/lib/notion/mapImage'
1212
import { deepClone } from '@/lib/utils'
1313
import { idToUuid } from 'notion-utils'
1414
import { siteConfig } from '../config'
15-
import { extractLangId, extractLangPrefix } from '../utils/pageId'
15+
import { extractLangId, extractLangPrefix, getShortId } from '../utils/pageId'
1616

1717
export { getAllTags } from '../notion/getAllTags'
1818
export { getPost } from '../notion/getNotionPost'
19-
export { getPostBlocks } from '../notion/getPostBlocks'
19+
export { getPage as getPostBlocks } from '../notion/getPostBlocks'
2020

2121
/**
2222
* 获取博客数据; 基于Notion实现
@@ -77,7 +77,16 @@ export async function getNotionPageData({ pageId, from }) {
7777
}
7878

7979
// 返回给前端的数据做处理
80-
const db = deepClone(data)
80+
return compressData(deepClone(data))
81+
}
82+
83+
/**
84+
* 减少返回给前端的数据
85+
* 并脱敏
86+
* @param {*} db
87+
*/
88+
function compressData(db) {
89+
// 清理多余数据
8190
delete db.block
8291
delete db.schema
8392
delete db.rawMetadata
@@ -91,42 +100,88 @@ export async function getNotionPageData({ pageId, from }) {
91100
// 清理多余的块
92101
if (db?.notice) {
93102
db.notice = cleanBlock(db?.notice)
103+
delete db.notice?.id
94104
}
95-
if (db?.post) {
96-
db.post = cleanBlock(db?.post)
97-
}
105+
106+
db.tagOptions = cleanIds(db?.tagOptions)
107+
db.categoryOptions = cleanIds(db?.categoryOptions)
108+
db.customMenu = cleanIds(db?.customMenu)
109+
110+
db.latestPosts = shortenIds(db?.latestPosts)
111+
db.allNavPages = shortenIds(db?.allNavPages)
112+
// db.allPages = cleanBlocks(db?.allPages)
113+
98114
return db
99115
}
100116

101117
/**
102-
* 清理block数据
118+
* 清理一组数据的id
119+
* @param {*} items
120+
* @returns
103121
*/
104-
function cleanBlock(post) {
105-
const pageBlock = post?.blockMap?.block
106-
for (const i in pageBlock) {
107-
pageBlock[i] = cleanBlock(pageBlock[i])
108-
delete pageBlock[i]?.role
109-
delete pageBlock[i]?.value?.version
110-
delete pageBlock[i]?.value?.created_by_table
111-
delete pageBlock[i]?.value?.created_by_id
112-
delete pageBlock[i]?.value?.last_edited_by_table
113-
delete pageBlock[i]?.value?.last_edited_by_id
114-
delete pageBlock[i]?.value?.space_id
115-
delete pageBlock[i]?.value?.version
116-
delete pageBlock[i]?.value?.format?.copied_from_pointer
117-
delete pageBlock[i]?.value?.format?.block_locked_by
118-
delete pageBlock[i]?.value?.parent_table
119-
delete pageBlock[i]?.value?.copied_from_pointer
120-
delete pageBlock[i]?.value?.copied_from
121-
delete pageBlock[i]?.value?.created_by_table
122-
delete pageBlock[i]?.value?.created_by_id
123-
delete pageBlock[i]?.value?.last_edited_by_table
124-
delete pageBlock[i]?.value?.last_edited_by_id
125-
delete pageBlock[i]?.value?.permissions
126-
delete pageBlock[i]?.value?.alive
122+
function shortenIds(items) {
123+
if (items && Array.isArray(items)) {
124+
return deepClone(
125+
items.map(item => {
126+
item.short_id = getShortId(item.id)
127+
delete item.id
128+
return item
129+
})
130+
)
127131
}
132+
return items
133+
}
128134

135+
/**
136+
* 清理一组数据的id
137+
* @param {*} items
138+
* @returns
139+
*/
140+
function cleanIds(items) {
141+
if (items && Array.isArray(items)) {
142+
return deepClone(
143+
items.map(item => {
144+
delete item.id
145+
return item
146+
})
147+
)
148+
}
149+
return items
150+
}
151+
152+
/**
153+
* 清理block数据
154+
*/
155+
function cleanBlock(item) {
156+
const post = deepClone(item)
157+
const pageBlock = post?.blockMap?.block
158+
// delete post?.id
129159
// delete post?.blockMap?.collection
160+
161+
if (pageBlock) {
162+
for (const i in pageBlock) {
163+
pageBlock[i] = cleanBlock(pageBlock[i])
164+
delete pageBlock[i]?.role
165+
delete pageBlock[i]?.value?.version
166+
delete pageBlock[i]?.value?.created_by_table
167+
delete pageBlock[i]?.value?.created_by_id
168+
delete pageBlock[i]?.value?.last_edited_by_table
169+
delete pageBlock[i]?.value?.last_edited_by_id
170+
delete pageBlock[i]?.value?.space_id
171+
delete pageBlock[i]?.value?.version
172+
delete pageBlock[i]?.value?.format?.copied_from_pointer
173+
delete pageBlock[i]?.value?.format?.block_locked_by
174+
delete pageBlock[i]?.value?.parent_table
175+
delete pageBlock[i]?.value?.copied_from_pointer
176+
delete pageBlock[i]?.value?.copied_from
177+
delete pageBlock[i]?.value?.created_by_table
178+
delete pageBlock[i]?.value?.created_by_id
179+
delete pageBlock[i]?.value?.last_edited_by_table
180+
delete pageBlock[i]?.value?.last_edited_by_id
181+
delete pageBlock[i]?.value?.permissions
182+
delete pageBlock[i]?.value?.alive
183+
}
184+
}
130185
return post
131186
}
132187

@@ -320,7 +375,7 @@ async function getNotice(post) {
320375
return null
321376
}
322377

323-
post.blockMap = await getPostBlocks(post.id, 'data-notice')
378+
post.blockMap = await getPage(post.id, 'data-notice')
324379
return post
325380
}
326381

@@ -372,7 +427,7 @@ const EmptyData = pageId => {
372427
*/
373428
async function getDataBaseInfoByNotionAPI({ pageId, from }) {
374429
console.log('[Fetching Data]', pageId, from)
375-
const pageRecordMap = await getPostBlocks(pageId, from)
430+
const pageRecordMap = await getPage(pageId, from)
376431
if (!pageRecordMap) {
377432
console.error('can`t get Notion Data ; Which id is: ', pageId)
378433
return {}

lib/notion/mapPageUrl.js lib/notion/convertInnerUrl.js

+7-5
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
1-
import { uuidToId } from 'notion-utils'
1+
import { idToUuid } from 'notion-utils'
22
import { checkStrIsNotionId, getLastPartOfUrl, isBrowser } from '../utils'
33

44
/**
55
* 处理页面内连接跳转:
6-
* 1. 若是本站域名,则在当前窗口打开、不开新窗口
7-
* 2. 若是Notion笔记中的内链,尝试转换成博客中现有的文章地址
6+
* 1.若是本站域名,则在当前窗口打开、不开新窗口
7+
* 2.url是notion-id,转成站内文章链接
88
*/
9-
export const mapPageUrl = allPages => {
9+
export const convertInnerUrl = allPages => {
1010
if (isBrowser) {
1111
const currentURL = window.location.origin + window.location.pathname
1212
const allAnchorTags = document.getElementsByTagName('a') // 或者使用 document.querySelectorAll('a') 获取 NodeList
@@ -16,7 +16,9 @@ export const mapPageUrl = allPages => {
1616
// 如果url是一个Notion_id,尝试匹配成博客的文章内链
1717
const slug = getLastPartOfUrl(anchorTag.href)
1818
if (checkStrIsNotionId(slug)) {
19-
const slugPage = allPages?.find(page => uuidToId(page.id) === slug)
19+
const slugPage = allPages?.find(page => {
20+
return idToUuid(slug).indexOf(page.short_id) === 0
21+
})
2022
if (slugPage) {
2123
anchorTag.href = slugPage?.href
2224
}

lib/notion/getNotionConfig.js

+3-3
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
import { getDateValue, getTextContent } from 'notion-utils'
1010
import { deepClone } from '../utils'
1111
import getAllPageIds from './getAllPageIds'
12-
import { getPostBlocks } from './getPostBlocks'
12+
import { getPage } from './getPostBlocks'
1313

1414
/**
1515
* 从Notion中读取Config配置表
@@ -41,12 +41,12 @@ export async function getConfigMapFromConfigPage(allPages) {
4141
}
4242
const configPageId = configPage.id
4343
// console.log('[Notion配置]请求配置数据 ', configPage.id)
44-
let pageRecordMap = await getPostBlocks(configPageId, 'config-table')
44+
let pageRecordMap = await getPage(configPageId, 'config-table')
4545
// console.log('配置中心Page', configPageId, pageRecordMap)
4646
let content = pageRecordMap.block[configPageId].value.content
4747
for (const table of ['Config-Table', 'CONFIG-TABLE']) {
4848
if (content) break
49-
pageRecordMap = await getPostBlocks(configPageId, table)
49+
pageRecordMap = await getPage(configPageId, table)
5050
content = pageRecordMap.block[configPageId].value.content
5151
}
5252

lib/notion/getNotionPost.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,15 @@ import BLOG from '@/blog.config'
22
import { idToUuid } from 'notion-utils'
33
import { defaultMapImageUrl } from 'react-notion-x'
44
import formatDate from '../utils/formatDate'
5-
import { getPostBlocks } from './getPostBlocks'
5+
import { getPage } from './getPostBlocks'
66

77
/**
88
* 根据页面ID获取内容
99
* @param {*} pageId
1010
* @returns
1111
*/
1212
export async function getPost(pageId) {
13-
const blockMap = await getPostBlocks(pageId, 'slug')
13+
const blockMap = await getPage(pageId, 'slug')
1414
if (!blockMap) {
1515
return null
1616
}

lib/notion/getPostBlocks.js

+7-24
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import { deepClone, delay } from '../utils'
1010
* @param {*} slice
1111
* @returns
1212
*/
13-
export async function getPostBlocks(id, from, slice) {
13+
export async function getPage(id, from, slice) {
1414
const cacheKey = 'page_block_' + id
1515
let pageBlock = await getDataFromCache(cacheKey)
1616
if (pageBlock) {
@@ -27,28 +27,6 @@ export async function getPostBlocks(id, from, slice) {
2727
return pageBlock
2828
}
2929

30-
/**
31-
* 针对在getDataBaseInfoByNotionAPI=>getPostBlocks中获取不到的溢出的block-id,用此方法另外抓取
32-
* @param {*} id
33-
* @param {*} from
34-
* @returns
35-
*/
36-
export async function getSingleBlock(id, from) {
37-
const cacheKey = 'single_block_' + id
38-
let pageBlock = await getDataFromCache(cacheKey)
39-
if (pageBlock) {
40-
// console.log('[API<<--缓存]', `from:${from}`, cacheKey)
41-
return pageBlock
42-
}
43-
44-
pageBlock = await getPageWithRetry(id, 'single_' + from)
45-
46-
if (pageBlock) {
47-
await setDataToCache(cacheKey, pageBlock)
48-
}
49-
return pageBlock
50-
}
51-
5230
/**
5331
* 调用接口,失败会重试
5432
* @param {*} id
@@ -162,6 +140,11 @@ function filterPostBlocks(id, blockMap, slice) {
162140
* @returns
163141
*/
164142
export const fetchInBatches = async (ids, batchSize = 100) => {
143+
// 如果 ids 不是数组,则将其转换为数组
144+
if (!Array.isArray(ids)) {
145+
ids = [ids]
146+
}
147+
165148
const authToken = BLOG.NOTION_ACCESS_TOKEN || null
166149
const api = new NotionAPI({
167150
authToken,
@@ -171,7 +154,7 @@ export const fetchInBatches = async (ids, batchSize = 100) => {
171154
let fetchedBlocks = {}
172155
for (let i = 0; i < ids.length; i += batchSize) {
173156
const batch = ids.slice(i, i + batchSize)
174-
console.log('[API-->>请求] Fetching missing blocks', ids.length)
157+
console.log('[API-->>请求] Fetching missing blocks', batch, ids.length)
175158
const start = new Date().getTime()
176159
const pageChunk = await api.getBlocks(batch)
177160
const end = new Date().getTime()

lib/utils/pageId.js

+15-1
Original file line numberDiff line numberDiff line change
@@ -30,4 +30,18 @@ function extractLangId(str) {
3030
}
3131
}
3232

33-
module.exports = { extractLangPrefix, extractLangId }
33+
/**
34+
* 列表中用过来区分page只需要端的id足够
35+
*/
36+
37+
function getShortId(uuid) {
38+
if (!uuid || uuid.indexOf('-') < 0) {
39+
return uuid
40+
}
41+
// 找到第一个 '-' 的位置
42+
const index = uuid.indexOf('-')
43+
// 截取从开始到第一个 '-' 之前的部分
44+
return uuid.substring(0, index)
45+
}
46+
47+
module.exports = { extractLangPrefix, extractLangId, getShortId }

package.json

+2-1
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,8 @@
1818
"start": "next start",
1919
"post-build": "next-sitemap --config next-sitemap.config.js",
2020
"export": "next build && next-sitemap --config next-sitemap.config.js && next export",
21-
"bundle-report": "cross-env ANALYZE=true yarn build"
21+
"bundle-report": "cross-env ANALYZE=true next build",
22+
"build-all-in-dev": "cross-env VERCEL_ENV=production next build"
2223
},
2324
"dependencies": {
2425
"@headlessui/react": "^1.7.15",

pages/[prefix]/[slug]/[...suffix].js

+1-1
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ export async function getStaticProps({
9292
}
9393

9494
// 文章内容加载
95-
if (!props?.posts?.blockMap) {
95+
if (!props?.post?.blockMap) {
9696
props.post.blockMap = await getPostBlocks(props.post.id, from)
9797
}
9898
// 生成全文索引 && JSON.parse(BLOG.ALGOLIA_RECREATE_DATA)

pages/[prefix]/[slug]/index.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ export async function getStaticProps({ params: { prefix, slug }, locale }) {
8181
}
8282

8383
// 文章内容加载
84-
if (!props?.posts?.blockMap) {
84+
if (!props?.post?.blockMap) {
8585
props.post.blockMap = await getPostBlocks(props.post.id, from)
8686
}
8787
// 生成全文索引 && JSON.parse(BLOG.ALGOLIA_RECREATE_DATA)

pages/[prefix]/index.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -145,7 +145,7 @@ export async function getStaticProps({ params: { prefix }, locale }) {
145145
}
146146

147147
// 文章内容加载
148-
if (!props?.posts?.blockMap) {
148+
if (!props?.post?.blockMap) {
149149
props.post.blockMap = await getPostBlocks(props.post.id, from)
150150
}
151151

0 commit comments

Comments
 (0)