Skip to content

Commit 54a3b37

Browse files
committed
调整blog.config,新增ext,部分数值配置支持环境变量
1 parent 4d3b30b commit 54a3b37

File tree

16 files changed

+234
-202
lines changed

16 files changed

+234
-202
lines changed

.prettierrc.json

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

blog.config.js

+89-91
Large diffs are not rendered by default.

lib/config.js

+9-5
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ import { deepClone } from './utils'
1717
export const siteConfig = (key, defaultVal = null, extendConfig = null) => {
1818
let global = null
1919
try {
20-
const isClient = typeof window !== 'undefined';
20+
const isClient = typeof window !== 'undefined'
2121
// eslint-disable-next-line react-hooks/rules-of-hooks
2222
global = isClient ? useGlobal() : {}
2323
// eslint-disable-next-line react-hooks/rules-of-hooks
@@ -67,15 +67,19 @@ export const siteConfig = (key, defaultVal = null, extendConfig = null) => {
6767
} else {
6868
if (typeof val === 'string') {
6969
if (val === 'true' || val === 'false') {
70-
return JSON.parse(val);
70+
return JSON.parse(val)
7171
}
72-
return val;
72+
if (/^\d+$/.test(val)) {
73+
// 如果是数字,使用parseFloat或者parseInt将字符串转换为数字
74+
return parseInt(val)
75+
}
76+
return val
7377
} else {
7478
try {
75-
return JSON.parse(val);
79+
return JSON.parse(val)
7680
} catch (error) {
7781
// 如果值是一个字符串但不是有效的 JSON 格式,直接返回字符串
78-
return val;
82+
return val
7983
}
8084
}
8185
}

lib/db/getSiteData.js

+7-6
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,18 @@
11
import BLOG from '@/blog.config'
22
import { getDataFromCache, setDataToCache } from '@/lib/cache/cache_manager'
3-
import { getPostBlocks, getSingleBlock } from '@/lib/notion/getPostBlocks'
4-
import { idToUuid } from 'notion-utils'
5-
import { deepClone } from '@/lib/utils'
63
import { getAllCategories } from '@/lib/notion/getAllCategories'
74
import getAllPageIds from '@/lib/notion/getAllPageIds'
85
import { getAllTags } from '@/lib/notion/getAllTags'
6+
import { getConfigMapFromConfigPage } from '@/lib/notion/getNotionConfig'
97
import getPageProperties from '@/lib/notion/getPageProperties'
8+
import { getPostBlocks, getSingleBlock } from '@/lib/notion/getPostBlocks'
109
import { compressImage, mapImgUrl } from '@/lib/notion/mapImage'
11-
import { getConfigMapFromConfigPage } from '@/lib/notion/getNotionConfig'
10+
import { deepClone } from '@/lib/utils'
11+
import { idToUuid } from 'notion-utils'
1212

1313
export { getAllTags } from '../notion/getAllTags'
14-
export { getPostBlocks } from '../notion/getPostBlocks'
1514
export { getPost } from '../notion/getNotionPost'
15+
export { getPostBlocks } from '../notion/getPostBlocks'
1616

1717
/**
1818
* 获取博客数据; 基于Notion实现
@@ -275,7 +275,8 @@ export function getNavPages({ allPages }) {
275275
slug: item.slug,
276276
pageIcon: item.pageIcon || '',
277277
lastEditedDate: item.lastEditedDate,
278-
publishDate: item.publishDate
278+
publishDate: item.publishDate,
279+
ext: item.ext || {}
279280
}))
280281
}
281282

lib/utils/index.js

+17-1
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,28 @@
11
// 封装异步加载资源的方法
2-
import { memo } from 'react'
2+
import { memo } from 'react';
33

44
/**
55
* 判断是否客户端
66
* @returns {boolean}
77
*/
88
export const isBrowser = typeof window !== 'undefined'
99

10+
/**
11+
* 打乱数组
12+
* @param {*} array
13+
* @returns
14+
*/
15+
export const shuffleArray = (array) => {
16+
if (!array) {
17+
return []
18+
}
19+
for (let i = array.length - 1; i > 0; i--) {
20+
const j = Math.floor(Math.random() * (i + 1));
21+
[array[i], array[j]] = [array[j], array[i]];
22+
}
23+
return array;
24+
}
25+
1026
/**
1127
* google机器人
1228
* @returns

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

+13-9
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
import BLOG from '@/blog.config'
2-
import { getGlobalData, getPostBlocks, getPost } from '@/lib/db/getSiteData'
3-
import { idToUuid } from 'notion-utils'
4-
import Slug, { getRecommendPost } from '..'
2+
import { siteConfig } from '@/lib/config'
3+
import { getGlobalData, getPost, getPostBlocks } from '@/lib/db/getSiteData'
54
import { uploadDataToAlgolia } from '@/lib/plugins/algolia'
65
import { checkContainHttp } from '@/lib/utils'
6+
import { idToUuid } from 'notion-utils'
7+
import Slug, { getRecommendPost } from '..'
78

89
/**
910
* 根据notion的slug访问页面
@@ -12,7 +13,7 @@ import { checkContainHttp } from '@/lib/utils'
1213
* @returns
1314
*/
1415
const PrefixSlug = props => {
15-
return <Slug {...props}/>
16+
return <Slug {...props} />
1617
}
1718

1819
/**
@@ -31,8 +32,11 @@ export async function getStaticPaths() {
3132
const { allPages } = await getGlobalData({ from })
3233

3334
return {
34-
paths: allPages?.filter(row => checkSlug(row))
35-
.map(row => ({ params: { prefix: row.slug.split('/')[0], slug: row.slug.split('/')[1], suffix: row.slug.split('/').slice(1) } })),
35+
paths: allPages
36+
?.filter(row => checkSlug(row))
37+
.map(row => ({
38+
params: { prefix: row.slug.split('/')[0], slug: row.slug.split('/')[1], suffix: row.slug.split('/').slice(1) }
39+
})),
3640
fallback: true
3741
}
3842
}
@@ -52,8 +56,8 @@ export async function getStaticProps({ params: { prefix, slug, suffix } }) {
5256
const from = `slug-props-${fullSlug}`
5357
const props = await getGlobalData({ from })
5458
// 在列表内查找文章
55-
props.post = props?.allPages?.find((p) => {
56-
return (p.type.indexOf('Menu') < 0) && (p.slug === fullSlug || p.id === idToUuid(fullSlug))
59+
props.post = props?.allPages?.find(p => {
60+
return p.type.indexOf('Menu') < 0 && (p.slug === fullSlug || p.id === idToUuid(fullSlug))
5761
})
5862

5963
// 处理非列表内文章的内信息
@@ -86,7 +90,7 @@ export async function getStaticProps({ params: { prefix, slug, suffix } }) {
8690
const index = allPosts.indexOf(props.post)
8791
props.prev = allPosts.slice(index - 1, index)[0] ?? allPosts.slice(-1)[0]
8892
props.next = allPosts.slice(index + 1, index + 2)[0] ?? allPosts[0]
89-
props.recommendPosts = getRecommendPost(props.post, allPosts, BLOG.POST_RECOMMEND_COUNT)
93+
props.recommendPosts = getRecommendPost(props.post, allPosts, siteConfig('POST_RECOMMEND_COUNT'))
9094
} else {
9195
props.prev = null
9296
props.next = null

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

+10-8
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
import BLOG from '@/blog.config'
2-
import { getGlobalData, getPostBlocks, getPost } from '@/lib/db/getSiteData'
3-
import { idToUuid } from 'notion-utils'
4-
import Slug, { getRecommendPost } from '..'
2+
import { siteConfig } from '@/lib/config'
3+
import { getGlobalData, getPost, getPostBlocks } from '@/lib/db/getSiteData'
54
import { uploadDataToAlgolia } from '@/lib/plugins/algolia'
65
import { checkContainHttp } from '@/lib/utils'
6+
import { idToUuid } from 'notion-utils'
7+
import Slug, { getRecommendPost } from '..'
78

89
/**
910
* 根据notion的slug访问页面
@@ -12,7 +13,7 @@ import { checkContainHttp } from '@/lib/utils'
1213
* @returns
1314
*/
1415
const PrefixSlug = props => {
15-
return <Slug {...props}/>
16+
return <Slug {...props} />
1617
}
1718

1819
export async function getStaticPaths() {
@@ -25,7 +26,8 @@ export async function getStaticPaths() {
2526

2627
const from = 'slug-paths'
2728
const { allPages } = await getGlobalData({ from })
28-
const paths = allPages?.filter(row => checkSlug(row))
29+
const paths = allPages
30+
?.filter(row => checkSlug(row))
2931
.map(row => ({ params: { prefix: row.slug.split('/')[0], slug: row.slug.split('/')[1] } }))
3032
return {
3133
paths: paths,
@@ -43,8 +45,8 @@ export async function getStaticProps({ params: { prefix, slug } }) {
4345
const from = `slug-props-${fullSlug}`
4446
const props = await getGlobalData({ from })
4547
// 在列表内查找文章
46-
props.post = props?.allPages?.find((p) => {
47-
return (p.type.indexOf('Menu') < 0) && (p.slug === fullSlug || p.id === idToUuid(fullSlug))
48+
props.post = props?.allPages?.find(p => {
49+
return p.type.indexOf('Menu') < 0 && (p.slug === fullSlug || p.id === idToUuid(fullSlug))
4850
})
4951

5052
// 处理非列表内文章的内信息
@@ -77,7 +79,7 @@ export async function getStaticProps({ params: { prefix, slug } }) {
7779
const index = allPosts.indexOf(props.post)
7880
props.prev = allPosts.slice(index - 1, index)[0] ?? allPosts.slice(-1)[0]
7981
props.next = allPosts.slice(index + 1, index + 2)[0] ?? allPosts[0]
80-
props.recommendPosts = getRecommendPost(props.post, allPosts, BLOG.POST_RECOMMEND_COUNT)
82+
props.recommendPosts = getRecommendPost(props.post, allPosts, siteConfig('POST_RECOMMEND_COUNT'))
8183
} else {
8284
props.prev = null
8385
props.next = null

pages/[prefix]/index.js

+16-15
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
import BLOG from '@/blog.config'
2-
import { getGlobalData, getPostBlocks, getPost } from '@/lib/db/getSiteData'
3-
import { useEffect, useState } from 'react'
4-
import { idToUuid } from 'notion-utils'
5-
import { useRouter } from 'next/router'
2+
import { siteConfig } from '@/lib/config'
3+
import { getGlobalData, getPost, getPostBlocks } from '@/lib/db/getSiteData'
64
import { getPageTableOfContents } from '@/lib/notion/getPageTableOfContents'
5+
import { uploadDataToAlgolia } from '@/lib/plugins/algolia'
6+
import { checkContainHttp } from '@/lib/utils'
77
import { getLayoutByTheme } from '@/themes/theme'
88
import md5 from 'js-md5'
9-
import { checkContainHttp } from '@/lib/utils'
10-
import { uploadDataToAlgolia } from '@/lib/plugins/algolia'
11-
import { siteConfig } from '@/lib/config'
9+
import { useRouter } from 'next/router'
10+
import { idToUuid } from 'notion-utils'
11+
import { useEffect, useState } from 'react'
1212

1313
/**
1414
* 根据notion的slug访问页面
@@ -25,7 +25,7 @@ const Slug = props => {
2525
/**
2626
* 验证文章密码
2727
* @param {*} result
28-
*/
28+
*/
2929
const validPassword = passInput => {
3030
const encrypt = md5(post.slug + passInput)
3131
if (passInput && encrypt === post.password) {
@@ -43,7 +43,9 @@ const Slug = props => {
4343
} else {
4444
setLock(false)
4545
if (!lock && post?.blockMap?.block) {
46-
post.content = Object.keys(post.blockMap.block).filter(key => post.blockMap.block[key]?.value?.parent_id === post.id)
46+
post.content = Object.keys(post.blockMap.block).filter(
47+
key => post.blockMap.block[key]?.value?.parent_id === post.id
48+
)
4749
post.toc = getPageTableOfContents(post, post.blockMap)
4850
}
4951
}
@@ -65,8 +67,7 @@ export async function getStaticPaths() {
6567

6668
const from = 'slug-paths'
6769
const { allPages } = await getGlobalData({ from })
68-
const paths = allPages?.filter(row => checkSlug(row))
69-
.map(row => ({ params: { prefix: row.slug } }))
70+
const paths = allPages?.filter(row => checkSlug(row)).map(row => ({ params: { prefix: row.slug } }))
7071
return {
7172
paths: paths,
7273
fallback: true
@@ -83,8 +84,8 @@ export async function getStaticProps({ params: { prefix } }) {
8384
const from = `slug-props-${fullSlug}`
8485
const props = await getGlobalData({ from })
8586
// 在列表内查找文章
86-
props.post = props?.allPages?.find((p) => {
87-
return (p.type.indexOf('Menu') < 0) && (p.slug === fullSlug || p.id === idToUuid(fullSlug))
87+
props.post = props?.allPages?.find(p => {
88+
return p.type.indexOf('Menu') < 0 && (p.slug === fullSlug || p.id === idToUuid(fullSlug))
8889
})
8990

9091
// 处理非列表内文章的内信息
@@ -117,7 +118,7 @@ export async function getStaticProps({ params: { prefix } }) {
117118
const index = allPosts.indexOf(props.post)
118119
props.prev = allPosts.slice(index - 1, index)[0] ?? allPosts.slice(-1)[0]
119120
props.next = allPosts.slice(index + 1, index + 2)[0] ?? allPosts[0]
120-
props.recommendPosts = getRecommendPost(props.post, allPosts, BLOG.POST_RECOMMEND_COUNT)
121+
props.recommendPosts = getRecommendPost(props.post, allPosts, siteConfig('POST_RECOMMEND_COUNT'))
121122
} else {
122123
props.prev = null
123124
props.next = null
@@ -171,7 +172,7 @@ function checkSlug(row) {
171172
if (slug.startsWith('/')) {
172173
slug = slug.substring(1)
173174
}
174-
return ((slug.match(/\//g) || []).length === 0 && !checkContainHttp(slug)) && row.type.indexOf('Menu') < 0
175+
return (slug.match(/\//g) || []).length === 0 && !checkContainHttp(slug) && row.type.indexOf('Menu') < 0
175176
}
176177

177178
export default Slug

pages/category/[category]/index.js

+6-7
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
1-
import { getGlobalData } from '@/lib/db/getSiteData'
2-
import React from 'react'
31
import BLOG from '@/blog.config'
4-
import { useRouter } from 'next/router'
5-
import { getLayoutByTheme } from '@/themes/theme'
62
import { siteConfig } from '@/lib/config'
3+
import { getGlobalData } from '@/lib/db/getSiteData'
4+
import { getLayoutByTheme } from '@/themes/theme'
5+
import { useRouter } from 'next/router'
76

87
/**
98
* 分类页
@@ -28,10 +27,10 @@ export async function getStaticProps({ params: { category } }) {
2827
// 处理文章页数
2928
props.postCount = props.posts.length
3029
// 处理分页
31-
if (BLOG.POST_LIST_STYLE === 'scroll') {
30+
if (siteConfig('POST_LIST_STYLE') === 'scroll') {
3231
// 滚动列表 给前端返回所有数据
33-
} else if (BLOG.POST_LIST_STYLE === 'page') {
34-
props.posts = props.posts?.slice(0, BLOG.POSTS_PER_PAGE)
32+
} else if (siteConfig('POST_LIST_STYLE') === 'page') {
33+
props.posts = props.posts?.slice(0, siteConfig('POSTS_PER_PAGE'))
3534
}
3635

3736
delete props.allPages

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

+11-8
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
1-
import { getGlobalData } from '@/lib/db/getSiteData'
2-
import React from 'react'
31
import BLOG from '@/blog.config'
4-
import { useRouter } from 'next/router'
5-
import { getLayoutByTheme } from '@/themes/theme'
62
import { siteConfig } from '@/lib/config'
3+
import { getGlobalData } from '@/lib/db/getSiteData'
4+
import { getLayoutByTheme } from '@/themes/theme'
5+
import { useRouter } from 'next/router'
76

87
/**
98
* 分类页
@@ -23,11 +22,13 @@ export async function getStaticProps({ params: { category, page } }) {
2322
let props = await getGlobalData({ from })
2423

2524
// 过滤状态类型
26-
props.posts = props.allPages?.filter(page => page.type === 'Post' && page.status === 'Published').filter(post => post && post.category && post.category.includes(category))
25+
props.posts = props.allPages
26+
?.filter(page => page.type === 'Post' && page.status === 'Published')
27+
.filter(post => post && post.category && post.category.includes(category))
2728
// 处理文章页数
2829
props.postCount = props.posts.length
2930
// 处理分页
30-
props.posts = props.posts.slice(BLOG.POSTS_PER_PAGE * (page - 1), BLOG.POSTS_PER_PAGE * page)
31+
props.posts = props.posts.slice(siteConfig('POSTS_PER_PAGE') * (page - 1), siteConfig('POSTS_PER_PAGE') * page)
3132

3233
delete props.allPages
3334
props.page = page
@@ -47,10 +48,12 @@ export async function getStaticPaths() {
4748

4849
categoryOptions?.forEach(category => {
4950
// 过滤状态类型
50-
const categoryPosts = allPages?.filter(page => page.type === 'Post' && page.status === 'Published').filter(post => post && post.category && post.category.includes(category.name))
51+
const categoryPosts = allPages
52+
?.filter(page => page.type === 'Post' && page.status === 'Published')
53+
.filter(post => post && post.category && post.category.includes(category.name))
5154
// 处理文章页数
5255
const postCount = categoryPosts.length
53-
const totalPages = Math.ceil(postCount / BLOG.POSTS_PER_PAGE)
56+
const totalPages = Math.ceil(postCount / siteConfig('POSTS_PER_PAGE'))
5457
if (totalPages > 1) {
5558
for (let i = 1; i <= totalPages; i++) {
5659
paths.push({ params: { category: category.name, page: '' + i } })

0 commit comments

Comments
 (0)