diff --git a/components/DebugPanel.js b/components/DebugPanel.js
index 026b5ee0..29408994 100644
--- a/components/DebugPanel.js
+++ b/components/DebugPanel.js
@@ -1,9 +1,10 @@
-import BLOG from '@/blog.config'
import { useEffect, useState } from 'react'
import Select from './Select'
import { useGlobal } from '@/lib/global'
import { THEMES } from '@/themes/theme'
import { useRouter } from 'next/router'
+import { siteConfigMap } from '@/lib/config'
+import { getQueryParam } from '@/lib/utils'
/**
*
@@ -13,13 +14,14 @@ const DebugPanel = () => {
const [show, setShow] = useState(false)
const { theme, switchTheme, locale } = useGlobal()
const router = useRouter()
+ const currentTheme = getQueryParam(router.asPath, 'theme') || theme
const [siteConfig, updateSiteConfig] = useState({})
// 主题下拉框
const themeOptions = THEMES?.map(t => ({ value: t, text: t }))
useEffect(() => {
- updateSiteConfig(Object.assign({}, BLOG))
+ updateSiteConfig(Object.assign({}, siteConfigMap()))
// updateThemeConfig(Object.assign({}, ThemeMap[BLOG.THEME].THEME_CONFIG))
}, [])
@@ -71,7 +73,7 @@ const DebugPanel = () => {
diff --git a/components/ExternalPlugins.js b/components/ExternalPlugins.js
index c65836c4..1ea45d61 100644
--- a/components/ExternalPlugins.js
+++ b/components/ExternalPlugins.js
@@ -1,4 +1,4 @@
-import BLOG from 'blog.config'
+import { siteConfig } from '@/lib/config'
import dynamic from 'next/dynamic'
import WebWhiz from './Webwhiz'
@@ -30,26 +30,26 @@ const AdBlockDetect = dynamic(() => import('@/components/AdBlockDetect'), { ssr:
*/
const ExternalPlugin = (props) => {
return <>
- {JSON.parse(BLOG.THEME_SWITCH) &&
}
- {JSON.parse(BLOG.DEBUG) &&
}
- {BLOG.ANALYTICS_ACKEE_TRACKER &&
}
- {BLOG.ANALYTICS_GOOGLE_ID &&
}
- {BLOG.ANALYTICS_VERCEL &&
}
- {JSON.parse(BLOG.ANALYTICS_BUSUANZI_ENABLE) &&
}
- {BLOG.ADSENSE_GOOGLE_ID &&
}
- {BLOG.FACEBOOK_APP_ID && BLOG.FACEBOOK_PAGE_ID &&
}
- {JSON.parse(BLOG.FIREWORKS) &&
}
- {JSON.parse(BLOG.SAKURA) &&
}
- {JSON.parse(BLOG.STARRY_SKY) &&
}
- {JSON.parse(BLOG.MUSIC_PLAYER) &&
}
- {JSON.parse(BLOG.NEST) &&
}
- {JSON.parse(BLOG.FLUTTERINGRIBBON) &&
}
- {JSON.parse(BLOG.COMMENT_TWIKOO_COUNT_ENABLE) &&
}
- {JSON.parse(BLOG.RIBBON) &&
}
- {JSON.parse(BLOG.CUSTOM_RIGHT_CLICK_CONTEXT_MENU) &&
}
- {!JSON.parse(BLOG.CAN_COPY) &&
}
- {JSON.parse(BLOG.WEB_WHIZ_ENABLED) &&
}
- {JSON.parse(BLOG.AD_WWADS_BLOCK_DETECT) &&
}
+ {JSON.parse(siteConfig('THEME_SWITCH')) &&
}
+ {JSON.parse(siteConfig('DEBUG')) &&
}
+ {siteConfig('ANALYTICS_ACKEE_TRACKER') &&
}
+ {siteConfig('ANALYTICS_GOOGLE_ID') &&
}
+ {siteConfig('ANALYTICS_VERCEL') &&
}
+ {JSON.parse(siteConfig('ANALYTICS_BUSUANZI_ENABLE')) &&
}
+ {siteConfig('ADSENSE_GOOGLE_ID') &&
}
+ {siteConfig('FACEBOOK_APP_ID') && siteConfig('FACEBOOK_PAGE_ID') &&
}
+ {JSON.parse(siteConfig('FIREWORKS')) &&
}
+ {JSON.parse(siteConfig('SAKURA')) &&
}
+ {JSON.parse(siteConfig('STARRY_SKY')) &&
}
+ {JSON.parse(siteConfig('MUSIC_PLAYER')) &&
}
+ {JSON.parse(siteConfig('NEST')) &&
}
+ {JSON.parse(siteConfig('FLUTTERINGRIBBON')) &&
}
+ {JSON.parse(siteConfig('COMMENT_TWIKOO_COUNT_ENABLE')) &&
}
+ {JSON.parse(siteConfig('RIBBON')) &&
}
+ {JSON.parse(siteConfig('CUSTOM_RIGHT_CLICK_CONTEXT_MENU')) &&
}
+ {!JSON.parse(siteConfig('CAN_COPY')) &&
}
+ {JSON.parse(siteConfig('WEB_WHIZ_ENABLED')) &&
}
+ {JSON.parse(siteConfig('AD_WWADS_BLOCK_DETECT')) &&
}
>
}
diff --git a/components/ThemeSwitch.js b/components/ThemeSwitch.js
index 1e140d79..a4b13f31 100644
--- a/components/ThemeSwitch.js
+++ b/components/ThemeSwitch.js
@@ -1,9 +1,10 @@
import { useGlobal } from '@/lib/global'
-import React, { useState } from 'react'
+import { useState } from 'react'
import { Draggable } from './Draggable'
import { THEMES } from '@/themes/theme'
import { useRouter } from 'next/router'
import DarkModeButton from './DarkModeButton'
+import { getQueryParam } from '@/lib/utils'
/**
*
* @returns 主题切换
@@ -11,6 +12,7 @@ import DarkModeButton from './DarkModeButton'
const ThemeSwitch = () => {
const { theme } = useGlobal()
const router = useRouter()
+ const currentTheme = getQueryParam(router.asPath, 'theme') || theme
const [isLoading, setIsLoading] = useState(false)
// 修改当前路径url中的 theme 参数
@@ -31,13 +33,13 @@ const ThemeSwitch = () => {
-
-
+
{/* 切换主题加载时的全屏遮罩 */}
diff --git a/lib/config.js b/lib/config.js
index ff931386..56ae36ee 100644
--- a/lib/config.js
+++ b/lib/config.js
@@ -2,6 +2,7 @@
import BLOG from '@/blog.config'
import { useGlobal } from './global'
+import { deepClone } from './utils'
/**
* 读取配置
@@ -47,6 +48,23 @@ export const siteConfig = (key) => {
if (!val) {
val = BLOG[key]
}
- console.log('实际配置', key, val)
+ // console.log('实际配置', key, val)
+ return val
+}
+
+/**
+ * 读取所有配置
+ * 1. 优先读取NotionConfig表
+ * 2. 其次读取环境变量
+ * 3. 再读取blog.config.js文件
+ * @param {*} key
+ * @returns
+ */
+export const siteConfigMap = () => {
+ const val = deepClone(BLOG)
+ for (const key in val) {
+ val[key] = siteConfig(key)
+ console.log('site', key, val[key], siteConfig(key))
+ }
return val
}
diff --git a/lib/global.js b/lib/global.js
index 548b6d8c..204fc432 100644
--- a/lib/global.js
+++ b/lib/global.js
@@ -17,12 +17,11 @@ const GlobalContext = createContext()
*/
export function GlobalContextProvider(props) {
const { children, siteInfo, categoryOptions, tagOptions, NOTION_CONFIG } = props
- console.log('config', NOTION_CONFIG)
const router = useRouter()
- const [lang, updateLang] = useState(NOTION_CONFIG.LANG || BLOG.LANG) // 默认语言
- const [locale, updateLocale] = useState(generateLocaleDict(NOTION_CONFIG.LANG || BLOG.LANG)) // 默认语言
- const [theme, setTheme] = useState(NOTION_CONFIG.THEME || BLOG.THEME) // 默认博客主题
- const [isDarkMode, updateDarkMode] = useState(NOTION_CONFIG.APPEARANCE || BLOG.APPEARANCE === 'dark') // 默认深色模式
+ const [lang, updateLang] = useState(NOTION_CONFIG?.LANG || BLOG.LANG) // 默认语言
+ const [locale, updateLocale] = useState(generateLocaleDict(NOTION_CONFIG?.LANG || BLOG.LANG)) // 默认语言
+ const [theme, setTheme] = useState(NOTION_CONFIG?.THEME || BLOG.THEME) // 默认博客主题
+ const [isDarkMode, updateDarkMode] = useState(NOTION_CONFIG?.APPEARANCE || BLOG.APPEARANCE === 'dark') // 默认深色模式
const [onLoading, setOnLoading] = useState(false) // 抓取文章数据
// 切换主题
@@ -98,7 +97,6 @@ export function GlobalContextProvider(props) {
/**
* 切换主题时的特殊处理
- * @param {*} setTheme
*/
const checkThemeDOM = () => {
if (isBrowser) {
diff --git a/lib/lang.js b/lib/lang.js
index d69da08f..15106572 100644
--- a/lib/lang.js
+++ b/lib/lang.js
@@ -34,7 +34,7 @@ export function generateLocaleDict(langString) {
let userLocale
// 将语言字符串拆分为语言和地区代码,例如将 "zh-CN" 拆分为 "zh" 和 "CN"
- const [language, region] = langString.split(/[-_]/)
+ const [language, region] = langString?.split(/[-_]/)
// 优先匹配语言和地区都匹配的情况
const specificLocale = `${language}-${region}`
@@ -70,6 +70,7 @@ export function initLocale(lang, locale, changeLang, changeLocale) {
if (queryLang !== lang) {
currentLang = queryLang
}
+ console.log('初始化语言', currentLang)
changeLang(currentLang)
saveLangToCookies(currentLang)
diff --git a/pages/404.js b/pages/404.js
index 1c9e5c30..ae8eea37 100644
--- a/pages/404.js
+++ b/pages/404.js
@@ -14,7 +14,7 @@ const NoFound = props => {
props = { ...props, meta }
// 根据页面路径加载不同Layout文件
- const Layout = getLayoutByTheme(useRouter())
+ const Layout = getLayoutByTheme({ theme: siteConfig('THEME'), router: useRouter() })
return