@@ -25,10 +25,7 @@ export { getPost } from '../notion/getNotionPost'
25
25
* @returns
26
26
*
27
27
*/
28
- export async function getGlobalData ( {
29
- pageId = BLOG . NOTION_PAGE_ID ,
30
- from
31
- } ) {
28
+ export async function getGlobalData ( { pageId = BLOG . NOTION_PAGE_ID , from } ) {
32
29
// 从notion获取
33
30
const data = await getNotionPageData ( { pageId, from } )
34
31
const db = deepClone ( data )
@@ -89,7 +86,9 @@ function cleanBlock(post) {
89
86
* @returns
90
87
*/
91
88
function getLatestPosts ( { allPages, from, latestPostCount } ) {
92
- const allPosts = allPages ?. filter ( page => page . type === 'Post' && page . status === 'Published' )
89
+ const allPosts = allPages ?. filter (
90
+ page => page . type === 'Post' && page . status === 'Published'
91
+ )
93
92
94
93
const latestPosts = Object . create ( allPosts ) . sort ( ( a , b ) => {
95
94
const dateA = new Date ( a ?. lastEditedDate || a ?. publishDate )
@@ -140,7 +139,13 @@ function getCustomNav({ allPages }) {
140
139
p . to = '/' + p . slug
141
140
}
142
141
}
143
- customNav . push ( { icon : p . icon || null , name : p . title , to : p . slug , target : '_blank' , show : true } )
142
+ customNav . push ( {
143
+ icon : p . icon || null ,
144
+ name : p . title ,
145
+ to : p . slug ,
146
+ target : '_blank' ,
147
+ show : true
148
+ } )
144
149
} )
145
150
}
146
151
return customNav
@@ -152,7 +157,12 @@ function getCustomNav({ allPages }) {
152
157
* @returns
153
158
*/
154
159
function getCustomMenu ( { collectionData } ) {
155
- const menuPages = collectionData . filter ( post => post . status === 'Published' && ( post ?. type === BLOG . NOTION_PROPERTY_NAME . type_menu || post ?. type === BLOG . NOTION_PROPERTY_NAME . type_sub_menu ) )
160
+ const menuPages = collectionData . filter (
161
+ post =>
162
+ post . status === 'Published' &&
163
+ ( post ?. type === BLOG . NOTION_PROPERTY_NAME . type_menu ||
164
+ post ?. type === BLOG . NOTION_PROPERTY_NAME . type_sub_menu )
165
+ )
156
166
const menus = [ ]
157
167
if ( menuPages && menuPages . length > 0 ) {
158
168
menuPages . forEach ( e => {
@@ -190,7 +200,9 @@ function getCustomMenu({ collectionData }) {
190
200
*/
191
201
function getTagOptions ( schema ) {
192
202
if ( ! schema ) return { }
193
- const tagSchema = Object . values ( schema ) . find ( e => e . name === BLOG . NOTION_PROPERTY_NAME . tags )
203
+ const tagSchema = Object . values ( schema ) . find (
204
+ e => e . name === BLOG . NOTION_PROPERTY_NAME . tags
205
+ )
194
206
return tagSchema ?. options || [ ]
195
207
}
196
208
@@ -201,7 +213,9 @@ function getTagOptions(schema) {
201
213
*/
202
214
function getCategoryOptions ( schema ) {
203
215
if ( ! schema ) return { }
204
- const categorySchema = Object . values ( schema ) . find ( e => e . name === BLOG . NOTION_PROPERTY_NAME . category )
216
+ const categorySchema = Object . values ( schema ) . find (
217
+ e => e . name === BLOG . NOTION_PROPERTY_NAME . category
218
+ )
205
219
return categorySchema ?. options || [ ]
206
220
}
207
221
@@ -213,9 +227,15 @@ function getCategoryOptions(schema) {
213
227
*/
214
228
function getSiteInfo ( { collection, block } ) {
215
229
const title = collection ?. name ?. [ 0 ] [ 0 ] || BLOG . TITLE
216
- const description = collection ?. description ? Object . assign ( collection ) . description [ 0 ] [ 0 ] : BLOG . DESCRIPTION
217
- const pageCover = collection ?. cover ? mapImgUrl ( collection ?. cover , block [ idToUuid ( BLOG . NOTION_PAGE_ID ) ] ?. value ) : BLOG . HOME_BANNER_IMAGE
218
- let icon = collection ?. icon ? mapImgUrl ( collection ?. icon , collection , 'collection' ) : BLOG . AVATAR
230
+ const description = collection ?. description
231
+ ? Object . assign ( collection ) . description [ 0 ] [ 0 ]
232
+ : BLOG . DESCRIPTION
233
+ const pageCover = collection ?. cover
234
+ ? mapImgUrl ( collection ?. cover , block [ idToUuid ( BLOG . NOTION_PAGE_ID ) ] ?. value )
235
+ : BLOG . HOME_BANNER_IMAGE
236
+ let icon = collection ?. icon
237
+ ? mapImgUrl ( collection ?. icon , collection , 'collection' )
238
+ : BLOG . AVATAR
219
239
220
240
// 用户头像压缩一下
221
241
icon = compressImage ( icon )
@@ -236,7 +256,13 @@ function getSiteInfo({ collection, block }) {
236
256
*/
237
257
export function getNavPages ( { allPages } ) {
238
258
const allNavPages = allPages ?. filter ( post => {
239
- return post && post ?. slug && ( ! post ?. slug ?. startsWith ( 'http' ) ) && post ?. type === 'Post' && post ?. status === 'Published'
259
+ return (
260
+ post &&
261
+ post ?. slug &&
262
+ ! post ?. slug ?. startsWith ( 'http' ) &&
263
+ post ?. type === 'Post' &&
264
+ post ?. status === 'Published'
265
+ )
240
266
} )
241
267
242
268
return allNavPages . map ( item => ( {
@@ -248,7 +274,8 @@ export function getNavPages({ allPages }) {
248
274
summary : item . summary || null ,
249
275
slug : item . slug ,
250
276
pageIcon : item . pageIcon || '' ,
251
- lastEditedDate : item . lastEditedDate
277
+ lastEditedDate : item . lastEditedDate ,
278
+ publishDate : item . publishDate
252
279
} ) )
253
280
}
254
281
@@ -265,19 +292,26 @@ async function getNotice(post) {
265
292
}
266
293
267
294
// 没有数据时返回
268
- const EmptyData = ( pageId ) => {
295
+ const EmptyData = pageId => {
269
296
const empty = {
270
297
notice : null ,
271
298
siteInfo : getSiteInfo ( { } ) ,
272
- allPages : [ {
273
- id : 1 ,
274
- title : `无法获取Notion数据,请检查Notion_ID: \n 当前 ${ pageId } ` ,
275
- summary : '访问文档获取帮助→ https://tangly1024.com/article/vercel-deploy-notion-next' ,
276
- status : 'Published' ,
277
- type : 'Post' ,
278
- slug : '13a171332816461db29d50e9f575b00d' ,
279
- date : { start_date : '2023-04-24' , lastEditedDay : '2023-04-24' , tagItems : [ ] }
280
- } ] ,
299
+ allPages : [
300
+ {
301
+ id : 1 ,
302
+ title : `无法获取Notion数据,请检查Notion_ID: \n 当前 ${ pageId } ` ,
303
+ summary :
304
+ '访问文档获取帮助→ https://tangly1024.com/article/vercel-deploy-notion-next' ,
305
+ status : 'Published' ,
306
+ type : 'Post' ,
307
+ slug : '13a171332816461db29d50e9f575b00d' ,
308
+ date : {
309
+ start_date : '2023-04-24' ,
310
+ lastEditedDay : '2023-04-24' ,
311
+ tagItems : [ ]
312
+ }
313
+ }
314
+ ] ,
281
315
allNavPages : [ ] ,
282
316
collection : [ ] ,
283
317
collectionQuery : { } ,
@@ -313,7 +347,8 @@ async function getDataBaseInfoByNotionAPI({ pageId, from }) {
313
347
const rawMetadata = block [ pageId ] ?. value
314
348
// Check Type Page-Database和Inline-Database
315
349
if (
316
- rawMetadata ?. type !== 'collection_view_page' && rawMetadata ?. type !== 'collection_view'
350
+ rawMetadata ?. type !== 'collection_view_page' &&
351
+ rawMetadata ?. type !== 'collection_view'
317
352
) {
318
353
console . error ( `pageId "${ pageId } " is not a database` )
319
354
return EmptyData ( pageId )
@@ -328,9 +363,21 @@ async function getDataBaseInfoByNotionAPI({ pageId, from }) {
328
363
const viewIds = rawMetadata ?. view_ids
329
364
const collectionData = [ ]
330
365
331
- const pageIds = getAllPageIds ( collectionQuery , collectionId , collectionView , viewIds )
366
+ const pageIds = getAllPageIds (
367
+ collectionQuery ,
368
+ collectionId ,
369
+ collectionView ,
370
+ viewIds
371
+ )
332
372
if ( pageIds ?. length === 0 ) {
333
- console . error ( '获取到的文章列表为空,请检查notion模板' , collectionQuery , collection , collectionView , viewIds , pageRecordMap )
373
+ console . error (
374
+ '获取到的文章列表为空,请检查notion模板' ,
375
+ collectionQuery ,
376
+ collection ,
377
+ collectionView ,
378
+ viewIds ,
379
+ pageRecordMap
380
+ )
334
381
} else {
335
382
// console.log('有效Page数量', pageIds?.length)
336
383
}
@@ -343,15 +390,29 @@ async function getDataBaseInfoByNotionAPI({ pageId, from }) {
343
390
// 如果找不到文章对应的block,说明发生了溢出,使用pageID再去请求
344
391
const pageBlock = await getSingleBlock ( id , from )
345
392
if ( pageBlock . block [ id ] . value ) {
346
- const properties = ( await getPageProperties ( id , pageBlock . block [ id ] . value , schema , null , getTagOptions ( schema ) ) ) || null
393
+ const properties =
394
+ ( await getPageProperties (
395
+ id ,
396
+ pageBlock . block [ id ] . value ,
397
+ schema ,
398
+ null ,
399
+ getTagOptions ( schema )
400
+ ) ) || null
347
401
if ( properties ) {
348
402
collectionData . push ( properties )
349
403
}
350
404
}
351
405
continue
352
406
}
353
407
354
- const properties = ( await getPageProperties ( id , value , schema , null , getTagOptions ( schema ) ) ) || null
408
+ const properties =
409
+ ( await getPageProperties (
410
+ id ,
411
+ value ,
412
+ schema ,
413
+ null ,
414
+ getTagOptions ( schema )
415
+ ) ) || null
355
416
if ( properties ) {
356
417
collectionData . push ( properties )
357
418
}
@@ -365,13 +426,16 @@ async function getDataBaseInfoByNotionAPI({ pageId, from }) {
365
426
if ( post ?. type === 'Post' && post . status === 'Published' ) {
366
427
postCount ++
367
428
}
368
- return post && post ?. slug &&
369
- ( ! post ?. slug ?. startsWith ( 'http' ) ) &&
429
+ return (
430
+ post &&
431
+ post ?. slug &&
432
+ ! post ?. slug ?. startsWith ( 'http' ) &&
370
433
( post ?. status === 'Invisible' || post ?. status === 'Published' )
434
+ )
371
435
} )
372
436
373
437
// 站点配置优先读取配置表格,否则读取blog.config.js 文件
374
- const NOTION_CONFIG = await getConfigMapFromConfigPage ( collectionData ) || { }
438
+ const NOTION_CONFIG = ( await getConfigMapFromConfigPage ( collectionData ) ) || { }
375
439
376
440
// Sort by date
377
441
if ( BLOG . POSTS_SORT_BY === 'date' ) {
@@ -380,13 +444,27 @@ async function getDataBaseInfoByNotionAPI({ pageId, from }) {
380
444
} )
381
445
}
382
446
383
- const notice = await getNotice ( collectionData . filter ( post => {
384
- return post && post ?. type && post ?. type === 'Notice' && post . status === 'Published'
385
- } ) ?. [ 0 ] )
386
- const categoryOptions = getAllCategories ( { allPages, categoryOptions : getCategoryOptions ( schema ) } )
447
+ const notice = await getNotice (
448
+ collectionData . filter ( post => {
449
+ return (
450
+ post &&
451
+ post ?. type &&
452
+ post ?. type === 'Notice' &&
453
+ post . status === 'Published'
454
+ )
455
+ } ) ?. [ 0 ]
456
+ )
457
+ const categoryOptions = getAllCategories ( {
458
+ allPages,
459
+ categoryOptions : getCategoryOptions ( schema )
460
+ } )
387
461
const tagOptions = getAllTags ( { allPages, tagOptions : getTagOptions ( schema ) } )
388
462
// 旧的菜单
389
- const customNav = getCustomNav ( { allPages : collectionData . filter ( post => post ?. type === 'Page' && post . status === 'Published' ) } )
463
+ const customNav = getCustomNav ( {
464
+ allPages : collectionData . filter (
465
+ post => post ?. type === 'Page' && post . status === 'Published'
466
+ )
467
+ } )
390
468
// 新的菜单
391
469
const customMenu = await getCustomMenu ( { collectionData } )
392
470
const latestPosts = getLatestPosts ( { allPages, from, latestPostCount : 6 } )
0 commit comments