diff --git a/lib/config.js b/lib/config.js index bef4dcd0..b4f70e82 100644 --- a/lib/config.js +++ b/lib/config.js @@ -55,8 +55,8 @@ export const siteConfig = (key, defaultVal = null, extendConfig = {}) => { let siteInfo = null if (global) { - val = global.NOTION_CONFIG?.[key] siteInfo = global.siteInfo + val = global.NOTION_CONFIG?.[key] || global.THEME_CONFIG?.[key] } if (!val) { diff --git a/lib/global.js b/lib/global.js index 71f2a74f..5a38b00a 100644 --- a/lib/global.js +++ b/lib/global.js @@ -1,5 +1,6 @@ import { THEMES, + getThemeConfig, initDarkMode, saveDarkModeToLocalStorage } from '@/themes/theme' @@ -29,11 +30,14 @@ export function GlobalContextProvider(props) { tagOptions, NOTION_CONFIG } = props + const [lang, updateLang] = useState(NOTION_CONFIG?.LANG || LANG) // 默认语言 const [locale, updateLocale] = useState( generateLocaleDict(NOTION_CONFIG?.LANG || LANG) ) // 默认语言 const [theme, setTheme] = useState(NOTION_CONFIG?.THEME || THEME) // 默认博客主题 + const [THEME_CONFIG, SET_THEME_CONFIG] = useState(null) //主题配置 + const defaultDarkMode = NOTION_CONFIG?.APPEARANCE || APPEARANCE const [isDarkMode, updateDarkMode] = useState(defaultDarkMode === 'dark') // 默认深色模式 const [onLoading, setOnLoading] = useState(false) // 抓取文章数据 @@ -54,6 +58,12 @@ export function GlobalContextProvider(props) { return newTheme } + // 抓取主题配置 + const updateThemeConfig = async theme => { + const config = await getThemeConfig(theme) + SET_THEME_CONFIG(config) + } + // 切换深色模式 const toggleDarkMode = () => { const newStatus = !isDarkMode @@ -99,6 +109,9 @@ export function GlobalContextProvider(props) { setOnLoading(false) } + const currentTheme = router?.query?.theme || theme + updateThemeConfig(currentTheme) + router.events.on('routeChangeStart', handleStart) router.events.on('routeChangeError', handleStop) router.events.on('routeChangeComplete', handleStop) @@ -114,6 +127,7 @@ export function GlobalContextProvider(props) { value={{ fullWidth, NOTION_CONFIG, + THEME_CONFIG, toggleDarkMode, onLoading, setOnLoading, diff --git a/themes/theme.js b/themes/theme.js index 1c04e335..1931d46e 100644 --- a/themes/theme.js +++ b/themes/theme.js @@ -7,6 +7,20 @@ import { getQueryParam, getQueryVariable, isBrowser } from '../lib/utils' // 在next.config.js中扫描所有主题 export const { THEMES = [] } = getConfig().publicRuntimeConfig +/** + * 获取主体配置 + */ +export const getThemeConfig = async themeQuery => { + if (themeQuery && themeQuery !== BLOG.THEME) { + const THEME_CONFIG = await import(`@/themes/${themeQuery}`).then( + m => m.THEME_CONFIG + ) + return THEME_CONFIG + } else { + return ThemeComponents?.THEME_CONFIG + } +} + /** * 加载全局布局 * @param {*} themeQuery