Skip to content

Commit 4aeb781

Browse files
authored
Merge pull request tangly1024#2414 from tangly1024/hotfix/NOTION-CONFIG-for-GITBOOK_AUTO_SORT
修复部分NOTION_CONFIG读取问题
2 parents 70e5364 + 517a967 commit 4aeb781

Some content is hidden

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

44 files changed

+903
-557
lines changed

lib/config.js

+38-5
Original file line numberDiff line numberDiff line change
@@ -14,15 +14,38 @@ import { deepClone } from './utils'
1414
* @param {*} extendConfig ; 参考配置对象{key:val},如果notion中找不到优先尝试在这里面查找
1515
* @returns
1616
*/
17-
export const siteConfig = (key, defaultVal = null, extendConfig = null) => {
18-
let global = null
17+
export const siteConfig = (key, defaultVal = null, extendConfig = {}) => {
18+
if (!key) {
19+
return null
20+
}
21+
22+
// 特殊配置处理;某些配置只在服务端生效;而Global的NOTION_CONFIG仅限前端组件使用,因此需要从extendConfig中读取
23+
switch (key) {
24+
case 'NEXT_REVALIDATE_SECOND':
25+
case 'POST_RECOMMEND_COUNT':
26+
case 'IMAGE_COMPRESS_WIDTH':
27+
case 'PSEUDO_STATIC':
28+
case 'POSTS_SORT_BY':
29+
case 'POSTS_PER_PAGE':
30+
case 'POST_PREVIEW_LINES':
31+
case 'POST_URL_PREFIX':
32+
case 'POST_LIST_STYLE':
33+
case 'POST_LIST_PREVIEW':
34+
case 'POST_URL_PREFIX_MAPPING_CATEGORY':
35+
return convertVal(extendConfig[key] || defaultVal || BLOG[key])
36+
default:
37+
}
38+
39+
let global = {}
1940
try {
20-
const isClient = typeof window !== 'undefined'
41+
// const isClient = typeof window !== 'undefined'
2142
// eslint-disable-next-line react-hooks/rules-of-hooks
22-
global = isClient ? useGlobal() : {}
43+
global = useGlobal()
2344
// eslint-disable-next-line react-hooks/rules-of-hooks
2445
// global = useGlobal()
25-
} catch (error) {}
46+
} catch (error) {
47+
console.warn('SiteConfig警告', key, error)
48+
}
2649

2750
// 首先 配置最优先读取NOTION中的表格配置
2851
let val = null
@@ -66,6 +89,16 @@ export const siteConfig = (key, defaultVal = null, extendConfig = null) => {
6689
}
6790

6891
// 从Notion_CONFIG读取的配置通常都是字符串,适当转义
92+
return convertVal(val)
93+
}
94+
95+
/**
96+
* 配置默认都是string类型;
97+
* 识别配置的值是否数字、布尔、[]数组,若是则转成对应类型
98+
* @param {*} val
99+
* @returns
100+
*/
101+
export const convertVal = val => {
69102
if (typeof val === 'string') {
70103
// 解析布尔
71104
if (val === 'true' || val === 'false') {

lib/db/getSiteData.js

+7-6
Original file line numberDiff line numberDiff line change
@@ -236,12 +236,13 @@ function getCategoryOptions(schema) {
236236
* @param from
237237
* @returns {Promise<{title,description,pageCover,icon}>}
238238
*/
239-
function getSiteInfo({ collection, block, NOTION_CONFIG, pageId }) {
240-
const defaultTitle = siteConfig('TITLE', '', NOTION_CONFIG)
241-
const defaultDescription = siteConfig('DESCRIPTION', '', NOTION_CONFIG)
242-
const defaultPageCover = siteConfig('HOME_BANNER_IMAGE', '', NOTION_CONFIG)
243-
const defaultIcon = siteConfig('AVATAR', '', NOTION_CONFIG)
244-
const defaultLink = siteConfig('LINK', '', NOTION_CONFIG)
239+
function getSiteInfo({ collection, block, NOTION_CONFIG }) {
240+
const defaultTitle = NOTION_CONFIG?.TITLE || BLOG.TITLE
241+
const defaultDescription = NOTION_CONFIG?.DESCRIPTION || BLOG.DESCRIPTION
242+
const defaultPageCover =
243+
NOTION_CONFIG?.HOME_BANNER_IMAGE || BLOG.HOME_BANNER_IMAGE
244+
const defaultIcon = NOTION_CONFIG?.AVATAR || BLOG.AVATAR
245+
const defaultLink = NOTION_CONFIG?.LINK || BLOG.LINK
245246
if (!collection && !block) {
246247
return {
247248
title: defaultTitle,

lib/notion/getPageProperties.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -186,7 +186,7 @@ export function adjustPageProperties(properties, NOTION_CONFIG) {
186186
}
187187

188188
// 开启伪静态路径
189-
if (siteConfig('PSEUDO_STATIC', false, NOTION_CONFIG)) {
189+
if (JSON.parse(NOTION_CONFIG?.PSEUDO_STATIC || BLOG.PSEUDO_STATIC)) {
190190
if (
191191
!properties?.href?.endsWith('.html') &&
192192
!properties?.href?.startsWith('http')

lib/robots.txt.js

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
import fs from 'fs'
2-
import { siteConfig } from './config'
32

4-
export async function generateRobotsTxt(NOTION_CONFIG) {
5-
const LINK = siteConfig('LINK', '', NOTION_CONFIG)
3+
export async function generateRobotsTxt(props) {
4+
const { siteInfo } = props
5+
const LINK = siteInfo?.link
66
const content = `
77
# *
88
User-agent: *

lib/rss.js

+21-18
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
1+
import BLOG from '@/blog.config'
12
import NotionPage from '@/components/NotionPage'
23
import { getPostBlocks } from '@/lib/db/getSiteData'
34
import { Feed } from 'feed'
45
import fs from 'fs'
56
import ReactDOMServer from 'react-dom/server'
6-
import { siteConfig } from './config'
77

88
/**
99
* 生成RSS内容
@@ -25,30 +25,33 @@ const createFeedContent = async post => {
2525
}
2626
}
2727

28-
export async function generateRss(NOTION_CONFIG, posts) {
29-
const link = siteConfig('LINK', '', NOTION_CONFIG)
30-
const author = siteConfig('AUTHOR', '', NOTION_CONFIG)
31-
const lang = siteConfig('LANG', '', NOTION_CONFIG)
32-
const subPath = siteConfig('SUB_PATH', '', NOTION_CONFIG)
33-
28+
export async function generateRss(props) {
29+
const { NOTION_CONFIG, siteInfo, latestPosts } = props
30+
const TITLE = siteInfo?.title
31+
const DESCRIPTION = siteInfo?.description
32+
const LINK = siteInfo?.link
33+
const AUTHOR = NOTION_CONFIG?.AUTHOR || BLOG.AUTHOR
34+
const LANG = NOTION_CONFIG?.LANG || BLOG.LANG
35+
const SUB_PATH = NOTION_CONFIG?.SUB_PATH || BLOG.SUB_PATH
36+
const CONTACT_EMAIL = NOTION_CONFIG?.CONTACT_EMAIL || BLOG.CONTACT_EMAIL
3437
const year = new Date().getFullYear()
3538
const feed = new Feed({
36-
title: siteConfig('TITLE', '', NOTION_CONFIG),
37-
description: siteConfig('DESCRIPTION', '', NOTION_CONFIG),
38-
link: `${link}/${subPath}`,
39-
language: lang,
40-
favicon: `${link}/favicon.png`,
41-
copyright: `All rights reserved ${year}, ${author}`,
39+
title: TITLE,
40+
description: DESCRIPTION,
41+
link: `${LINK}/${SUB_PATH}`,
42+
language: LANG,
43+
favicon: `${LINK}/favicon.png`,
44+
copyright: `All rights reserved ${year}, ${AUTHOR}`,
4245
author: {
43-
name: author,
44-
email: siteConfig('CONTACT_EMAIL', '', NOTION_CONFIG),
45-
link: link
46+
name: AUTHOR,
47+
email: CONTACT_EMAIL,
48+
link: LINK
4649
}
4750
})
48-
for (const post of posts) {
51+
for (const post of latestPosts) {
4952
feed.addItem({
5053
title: post.title,
51-
link: `${link}/${post.slug}`,
54+
link: `${LINK}/${post.slug}`,
5255
description: post.summary,
5356
content: await createFeedContent(post),
5457
date: new Date(post?.publishDay)

pages/category/[category]/index.js

+4-1
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,10 @@ export async function getStaticProps({ params: { category }, locale }) {
3737
if (siteConfig('POST_LIST_STYLE') === 'scroll') {
3838
// 滚动列表 给前端返回所有数据
3939
} else if (siteConfig('POST_LIST_STYLE') === 'page') {
40-
props.posts = props.posts?.slice(0, siteConfig('POSTS_PER_PAGE'))
40+
props.posts = props.posts?.slice(
41+
0,
42+
siteConfig('POSTS_PER_PAGE', 12, props?.NOTION_CONFIG)
43+
)
4144
}
4245

4346
delete props.allPages

pages/category/[category]/page/[page].js

+9-4
Original file line numberDiff line numberDiff line change
@@ -30,10 +30,11 @@ export async function getStaticProps({ params: { category, page } }) {
3030
.filter(post => post && post.category && post.category.includes(category))
3131
// 处理文章页数
3232
props.postCount = props.posts.length
33+
const POSTS_PER_PAGE = siteConfig('POSTS_PER_PAGE', 12, props?.NOTION_CONFIG)
3334
// 处理分页
3435
props.posts = props.posts.slice(
35-
siteConfig('POSTS_PER_PAGE') * (page - 1),
36-
siteConfig('POSTS_PER_PAGE') * page
36+
POSTS_PER_PAGE * (page - 1),
37+
POSTS_PER_PAGE * page
3738
)
3839

3940
delete props.allPages
@@ -53,7 +54,9 @@ export async function getStaticProps({ params: { category, page } }) {
5354

5455
export async function getStaticPaths() {
5556
const from = 'category-paths'
56-
const { categoryOptions, allPages } = await getGlobalData({ from })
57+
const { categoryOptions, allPages, NOTION_CONFIG } = await getGlobalData({
58+
from
59+
})
5760
const paths = []
5861

5962
categoryOptions?.forEach(category => {
@@ -65,7 +68,9 @@ export async function getStaticPaths() {
6568
)
6669
// 处理文章页数
6770
const postCount = categoryPosts.length
68-
const totalPages = Math.ceil(postCount / siteConfig('POSTS_PER_PAGE'))
71+
const totalPages = Math.ceil(
72+
postCount / siteConfig('POSTS_PER_PAGE', 12, NOTION_CONFIG)
73+
)
6974
if (totalPages > 1) {
7075
for (let i = 1; i <= totalPages; i++) {
7176
paths.push({ params: { category: category.name, page: '' + i } })

pages/index.js

+13-10
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,11 @@ export async function getStaticProps(req) {
2828
const { locale } = req
2929
const from = 'index'
3030
const props = await getGlobalData({ from, locale })
31-
31+
const POST_PREVIEW_LINES = siteConfig(
32+
'POST_PREVIEW_LINES',
33+
12,
34+
props?.NOTION_CONFIG
35+
)
3236
props.posts = props.allPages?.filter(
3337
page => page.type === 'Post' && page.status === 'Published'
3438
)
@@ -37,29 +41,28 @@ export async function getStaticProps(req) {
3741
if (siteConfig('POST_LIST_STYLE') === 'scroll') {
3842
// 滚动列表默认给前端返回所有数据
3943
} else if (siteConfig('POST_LIST_STYLE') === 'page') {
40-
props.posts = props.posts?.slice(0, siteConfig('POSTS_PER_PAGE'))
44+
props.posts = props.posts?.slice(
45+
0,
46+
siteConfig('POSTS_PER_PAGE', 12, props?.NOTION_CONFIG)
47+
)
4148
}
4249

4350
// 预览文章内容
44-
if (siteConfig('POST_LIST_PREVIEW')) {
51+
if (siteConfig('POST_LIST_PREVIEW', false, props?.NOTION_CONFIG)) {
4552
for (const i in props.posts) {
4653
const post = props.posts[i]
4754
if (post.password && post.password !== '') {
4855
continue
4956
}
50-
post.blockMap = await getPostBlocks(
51-
post.id,
52-
'slug',
53-
siteConfig('POST_PREVIEW_LINES')
54-
)
57+
post.blockMap = await getPostBlocks(post.id, 'slug', POST_PREVIEW_LINES)
5558
}
5659
}
5760

5861
// 生成robotTxt
59-
generateRobotsTxt(props?.NOTION_CONFIG)
62+
generateRobotsTxt(props)
6063
// 生成Feed订阅
6164
if (JSON.parse(BLOG.ENABLE_RSS)) {
62-
generateRss(props?.NOTION_CONFIG, props?.latestPosts || [])
65+
generateRss(props)
6366
}
6467

6568
// 生成全文索引 - 仅在 yarn build 时执行 && process.env.npm_lifecycle_event === 'build'

pages/page/[page].js

+15-10
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,10 @@ const Page = props => {
2121

2222
export async function getStaticPaths({ locale }) {
2323
const from = 'page-paths'
24-
const { postCount } = await getGlobalData({ from, locale })
25-
const totalPages = Math.ceil(postCount / siteConfig('POSTS_PER_PAGE'))
24+
const { postCount, NOTION_CONFIG } = await getGlobalData({ from, locale })
25+
const totalPages = Math.ceil(
26+
postCount / siteConfig('POSTS_PER_PAGE', 12, NOTION_CONFIG)
27+
)
2628
return {
2729
// remove first page, we 're not gonna handle that.
2830
paths: Array.from({ length: totalPages - 1 }, (_, i) => ({
@@ -36,28 +38,31 @@ export async function getStaticProps({ params: { page } }) {
3638
const from = `page-${page}`
3739
const props = await getGlobalData({ from })
3840
const { allPages } = props
41+
const POST_PREVIEW_LINES = siteConfig(
42+
'POST_PREVIEW_LINES',
43+
12,
44+
props?.NOTION_CONFIG
45+
)
46+
3947
const allPosts = allPages?.filter(
4048
page => page.type === 'Post' && page.status === 'Published'
4149
)
50+
const POSTS_PER_PAGE = siteConfig('POSTS_PER_PAGE', 12, props?.NOTION_CONFIG)
4251
// 处理分页
4352
props.posts = allPosts.slice(
44-
siteConfig('POSTS_PER_PAGE') * (page - 1),
45-
siteConfig('POSTS_PER_PAGE') * page
53+
POSTS_PER_PAGE * (page - 1),
54+
POSTS_PER_PAGE * page
4655
)
4756
props.page = page
4857

4958
// 处理预览
50-
if (siteConfig('POST_LIST_PREVIEW')) {
59+
if (siteConfig('POST_LIST_PREVIEW', false, props?.NOTION_CONFIG)) {
5160
for (const i in props.posts) {
5261
const post = props.posts[i]
5362
if (post.password && post.password !== '') {
5463
continue
5564
}
56-
post.blockMap = await getPostBlocks(
57-
post.id,
58-
'slug',
59-
siteConfig('POST_PREVIEW_LINES')
60-
)
65+
post.blockMap = await getPostBlocks(post.id, 'slug', POST_PREVIEW_LINES)
6166
}
6267
}
6368

pages/search/[keyword]/index.js

+11-4
Original file line numberDiff line numberDiff line change
@@ -31,11 +31,18 @@ export async function getStaticProps({ params: { keyword }, locale }) {
3131
)
3232
props.posts = await filterByMemCache(allPosts, keyword)
3333
props.postCount = props.posts.length
34+
const POST_LIST_STYLE = siteConfig(
35+
'POST_LIST_STYLE',
36+
'Page',
37+
props?.NOTION_CONFIG
38+
)
39+
const POSTS_PER_PAGE = siteConfig('POSTS_PER_PAGE', 12, props?.NOTION_CONFIG)
40+
3441
// 处理分页
35-
if (siteConfig('POST_LIST_STYLE') === 'scroll') {
36-
// 滚动列表 给前端返回所有数据
37-
} else if (siteConfig('POST_LIST_STYLE') === 'page') {
38-
props.posts = props.posts?.slice(0, siteConfig('POSTS_PER_PAGE'))
42+
if (POST_LIST_STYLE === 'scroll') {
43+
// 滚动列表默认给前端返回所有数据
44+
} else if (POST_LIST_STYLE) {
45+
props.posts = props.posts?.slice(0, POSTS_PER_PAGE)
3946
}
4047
props.keyword = keyword
4148
return {

pages/search/[keyword]/page/[page].js

+3-2
Original file line numberDiff line numberDiff line change
@@ -34,10 +34,11 @@ export async function getStaticProps({ params: { keyword, page }, locale }) {
3434
)
3535
props.posts = await filterByMemCache(allPosts, keyword)
3636
props.postCount = props.posts.length
37+
const POSTS_PER_PAGE = siteConfig('POSTS_PER_PAGE', 12, props?.NOTION_CONFIG)
3738
// 处理分页
3839
props.posts = props.posts.slice(
39-
siteConfig('POSTS_PER_PAGE') * (page - 1),
40-
siteConfig('POSTS_PER_PAGE') * page
40+
POSTS_PER_PAGE * (page - 1),
41+
POSTS_PER_PAGE * page
4142
)
4243
props.keyword = keyword
4344
props.page = page

pages/tag/[tag]/index.js

+4-1
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,10 @@ export async function getStaticProps({ params: { tag }, locale }) {
3535
if (siteConfig('POST_LIST_STYLE') === 'scroll') {
3636
// 滚动列表 给前端返回所有数据
3737
} else if (siteConfig('POST_LIST_STYLE') === 'page') {
38-
props.posts = props.posts?.slice(0, siteConfig('POSTS_PER_PAGE'))
38+
props.posts = props.posts?.slice(
39+
0,
40+
siteConfig('POSTS_PER_PAGE', 12, props?.NOTION_CONFIG)
41+
)
3942
}
4043

4144
props.tag = tag

pages/tag/[tag]/page/[page].js

+7-4
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,11 @@ export async function getStaticProps({ params: { tag, page }, locale }) {
2222
.filter(post => post && post?.tags && post?.tags.includes(tag))
2323
// 处理文章数
2424
props.postCount = props.posts.length
25+
const POSTS_PER_PAGE = siteConfig('POSTS_PER_PAGE', 12, props?.NOTION_CONFIG)
2526
// 处理分页
2627
props.posts = props.posts.slice(
27-
siteConfig('POSTS_PER_PAGE') * (page - 1),
28-
siteConfig('POSTS_PER_PAGE') * page
28+
POSTS_PER_PAGE * (page - 1),
29+
POSTS_PER_PAGE * page
2930
)
3031

3132
props.tag = tag
@@ -43,7 +44,7 @@ export async function getStaticProps({ params: { tag, page }, locale }) {
4344

4445
export async function getStaticPaths() {
4546
const from = 'tag-page-static-path'
46-
const { tagOptions, allPages } = await getGlobalData({ from })
47+
const { tagOptions, allPages, NOTION_CONFIG } = await getGlobalData({ from })
4748
const paths = []
4849
tagOptions?.forEach(tag => {
4950
// 过滤状态类型
@@ -52,7 +53,9 @@ export async function getStaticPaths() {
5253
.filter(post => post && post?.tags && post?.tags.includes(tag.name))
5354
// 处理文章页数
5455
const postCount = tagPosts.length
55-
const totalPages = Math.ceil(postCount / siteConfig('POSTS_PER_PAGE'))
56+
const totalPages = Math.ceil(
57+
postCount / siteConfig('POSTS_PER_PAGE', 12, NOTION_CONFIG)
58+
)
5659
if (totalPages > 1) {
5760
for (let i = 1; i <= totalPages; i++) {
5861
paths.push({ params: { tag: tag.name, page: '' + i } })

0 commit comments

Comments
 (0)