Skip to content

Commit 5ca46e2

Browse files
committed
Merge branch 'main' into pr/lxw15337674/2176
2 parents a8b7f37 + 67613d6 commit 5ca46e2

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

48 files changed

+2716
-432
lines changed

.eslintrc.js

+2-9
Original file line numberDiff line numberDiff line change
@@ -4,22 +4,15 @@ module.exports = {
44
es2021: true,
55
node: true
66
},
7-
extends: [
8-
'plugin:react/recommended',
9-
'plugin:@next/next/recommended',
10-
'standard'
11-
],
7+
extends: ['plugin:react/recommended', 'plugin:@next/next/recommended', 'standard', 'prettier'],
128
parserOptions: {
139
ecmaFeatures: {
1410
jsx: true
1511
},
1612
ecmaVersion: 12,
1713
sourceType: 'module'
1814
},
19-
plugins: [
20-
'react',
21-
'react-hooks'
22-
],
15+
plugins: ['react', 'react-hooks', 'prettier'],
2316
settings: {
2417
react: {
2518
version: 'detect'

.prettierrc.json

+9-5
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
11
{
2-
"singleQuote": true,
3-
"semi": false,
4-
"trailingComma": "none",
5-
"arrowParens": "avoid"
6-
}
2+
"singleQuote": true,
3+
"semi": false,
4+
"trailingComma": "none",
5+
"arrowParens": "avoid",
6+
"printWidth": 120,
7+
"bracketSpacing": true,
8+
"jsxSingleQuote": true,
9+
"jsxBracketSameLine": true
10+
}

components/AlgoliaSearchModal.js

+26-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { useState, useImperativeHandle, useRef, useEffect } from 'react'
1+
import { useState, useImperativeHandle, useRef, useEffect, Fragment } from 'react'
22
import algoliasearch from 'algoliasearch'
33
import replaceSearchResult from '@/components/Mark'
44
import Link from 'next/link'
@@ -7,6 +7,22 @@ import throttle from 'lodash/throttle'
77
import { siteConfig } from '@/lib/config'
88
import { useHotkeys } from 'react-hotkeys-hook';
99

10+
const ShortCutActions = [
11+
{
12+
key: '↑ ↓',
13+
action: '选择'
14+
},
15+
{
16+
key: 'Enter',
17+
action: '跳转'
18+
},
19+
{
20+
key: 'Esc',
21+
action: '关闭'
22+
}
23+
24+
]
25+
1026
/**
1127
* 结合 Algolia 实现的弹出式搜索框
1228
* 打开方式 cRef.current.openSearch()
@@ -234,6 +250,15 @@ export default function AlgoliaSearchModal({ cRef }) {
234250
</ul>
235251
<Pagination totalPage={totalPage} page={page} switchPage={switchPage} />
236252
<div className='flex items-center justify-between mt-2 sm:text-sm text-xs dark:text-gray-300'>
253+
{totalHit === 0 && (<div className='flex items-center'>
254+
{
255+
ShortCutActions.map((action, index) => {
256+
return <Fragment key={index}><div className='border-gray-300 dark:text-gray-300 text-gray-600 px-2 rounded border inline-block'>{action.key}</div>
257+
<span className='ml-2 mr-4 text-gray-600 dark:text-gray-300'>{action.action}</span></Fragment>
258+
})
259+
}
260+
</div>)
261+
}
237262
<div>
238263
{totalHit > 0 && (
239264
<p>

components/WalineComponent.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import React, { createRef } from 'react'
1+
import { createRef, useEffect } from 'react'
22
import { init } from '@waline/client'
33
import { useRouter } from 'next/router'
44
import '@waline/client/dist/waline.css'
@@ -21,7 +21,7 @@ const WalineComponent = (props) => {
2121
}
2222
}
2323

24-
React.useEffect(() => {
24+
useEffect(() => {
2525
if (!waline) {
2626
waline = init({
2727
...props,

lib/db/getSiteData.js

+115-37
Original file line numberDiff line numberDiff line change
@@ -25,10 +25,7 @@ export { getPost } from '../notion/getNotionPost'
2525
* @returns
2626
*
2727
*/
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 }) {
3229
// 从notion获取
3330
const data = await getNotionPageData({ pageId, from })
3431
const db = deepClone(data)
@@ -89,7 +86,9 @@ function cleanBlock(post) {
8986
* @returns
9087
*/
9188
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+
)
9392

9493
const latestPosts = Object.create(allPosts).sort((a, b) => {
9594
const dateA = new Date(a?.lastEditedDate || a?.publishDate)
@@ -140,7 +139,13 @@ function getCustomNav({ allPages }) {
140139
p.to = '/' + p.slug
141140
}
142141
}
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+
})
144149
})
145150
}
146151
return customNav
@@ -152,7 +157,12 @@ function getCustomNav({ allPages }) {
152157
* @returns
153158
*/
154159
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+
)
156166
const menus = []
157167
if (menuPages && menuPages.length > 0) {
158168
menuPages.forEach(e => {
@@ -190,7 +200,9 @@ function getCustomMenu({ collectionData }) {
190200
*/
191201
function getTagOptions(schema) {
192202
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+
)
194206
return tagSchema?.options || []
195207
}
196208

@@ -201,7 +213,9 @@ function getTagOptions(schema) {
201213
*/
202214
function getCategoryOptions(schema) {
203215
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+
)
205219
return categorySchema?.options || []
206220
}
207221

@@ -213,9 +227,15 @@ function getCategoryOptions(schema) {
213227
*/
214228
function getSiteInfo({ collection, block }) {
215229
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
219239

220240
// 用户头像压缩一下
221241
icon = compressImage(icon)
@@ -236,7 +256,13 @@ function getSiteInfo({ collection, block }) {
236256
*/
237257
export function getNavPages({ allPages }) {
238258
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+
)
240266
})
241267

242268
return allNavPages.map(item => ({
@@ -248,7 +274,8 @@ export function getNavPages({ allPages }) {
248274
summary: item.summary || null,
249275
slug: item.slug,
250276
pageIcon: item.pageIcon || '',
251-
lastEditedDate: item.lastEditedDate
277+
lastEditedDate: item.lastEditedDate,
278+
publishDate: item.publishDate
252279
}))
253280
}
254281

@@ -265,19 +292,26 @@ async function getNotice(post) {
265292
}
266293

267294
// 没有数据时返回
268-
const EmptyData = (pageId) => {
295+
const EmptyData = pageId => {
269296
const empty = {
270297
notice: null,
271298
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+
],
281315
allNavPages: [],
282316
collection: [],
283317
collectionQuery: {},
@@ -313,7 +347,8 @@ async function getDataBaseInfoByNotionAPI({ pageId, from }) {
313347
const rawMetadata = block[pageId]?.value
314348
// Check Type Page-Database和Inline-Database
315349
if (
316-
rawMetadata?.type !== 'collection_view_page' && rawMetadata?.type !== 'collection_view'
350+
rawMetadata?.type !== 'collection_view_page' &&
351+
rawMetadata?.type !== 'collection_view'
317352
) {
318353
console.error(`pageId "${pageId}" is not a database`)
319354
return EmptyData(pageId)
@@ -328,9 +363,21 @@ async function getDataBaseInfoByNotionAPI({ pageId, from }) {
328363
const viewIds = rawMetadata?.view_ids
329364
const collectionData = []
330365

331-
const pageIds = getAllPageIds(collectionQuery, collectionId, collectionView, viewIds)
366+
const pageIds = getAllPageIds(
367+
collectionQuery,
368+
collectionId,
369+
collectionView,
370+
viewIds
371+
)
332372
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+
)
334381
} else {
335382
// console.log('有效Page数量', pageIds?.length)
336383
}
@@ -343,15 +390,29 @@ async function getDataBaseInfoByNotionAPI({ pageId, from }) {
343390
// 如果找不到文章对应的block,说明发生了溢出,使用pageID再去请求
344391
const pageBlock = await getSingleBlock(id, from)
345392
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
347401
if (properties) {
348402
collectionData.push(properties)
349403
}
350404
}
351405
continue
352406
}
353407

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
355416
if (properties) {
356417
collectionData.push(properties)
357418
}
@@ -365,13 +426,16 @@ async function getDataBaseInfoByNotionAPI({ pageId, from }) {
365426
if (post?.type === 'Post' && post.status === 'Published') {
366427
postCount++
367428
}
368-
return post && post?.slug &&
369-
(!post?.slug?.startsWith('http')) &&
429+
return (
430+
post &&
431+
post?.slug &&
432+
!post?.slug?.startsWith('http') &&
370433
(post?.status === 'Invisible' || post?.status === 'Published')
434+
)
371435
})
372436

373437
// 站点配置优先读取配置表格,否则读取blog.config.js 文件
374-
const NOTION_CONFIG = await getConfigMapFromConfigPage(collectionData) || {}
438+
const NOTION_CONFIG = (await getConfigMapFromConfigPage(collectionData)) || {}
375439

376440
// Sort by date
377441
if (BLOG.POSTS_SORT_BY === 'date') {
@@ -380,13 +444,27 @@ async function getDataBaseInfoByNotionAPI({ pageId, from }) {
380444
})
381445
}
382446

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+
})
387461
const tagOptions = getAllTags({ allPages, tagOptions: getTagOptions(schema) })
388462
// 旧的菜单
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+
})
390468
// 新的菜单
391469
const customMenu = await getCustomMenu({ collectionData })
392470
const latestPosts = getLatestPosts({ allPages, from, latestPostCount: 6 })

lib/global.js

+3-2
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,8 @@ export function GlobalContextProvider(props) {
1616
const [lang, updateLang] = useState(NOTION_CONFIG?.LANG || LANG) // 默认语言
1717
const [locale, updateLocale] = useState(generateLocaleDict(NOTION_CONFIG?.LANG || LANG)) // 默认语言
1818
const [theme, setTheme] = useState(NOTION_CONFIG?.THEME || THEME) // 默认博客主题
19-
const [isDarkMode, updateDarkMode] = useState(NOTION_CONFIG?.APPEARANCE || APPEARANCE === 'dark') // 默认深色模式
19+
const defaultDarkMode = NOTION_CONFIG?.APPEARANCE || APPEARANCE
20+
const [isDarkMode, updateDarkMode] = useState(defaultDarkMode === 'dark') // 默认深色模式
2021
const [onLoading, setOnLoading] = useState(false) // 抓取文章数据
2122
const router = useRouter()
2223

@@ -57,7 +58,7 @@ export function GlobalContextProvider(props) {
5758
}
5859

5960
useEffect(() => {
60-
initDarkMode(updateDarkMode)
61+
initDarkMode(updateDarkMode,defaultDarkMode)
6162
initLocale(lang, locale, updateLang, updateLocale)
6263
}, [])
6364

0 commit comments

Comments
 (0)