Skip to content

Commit

Permalink
整理修改
Browse files Browse the repository at this point in the history
  • Loading branch information
tangly1024 committed Nov 2, 2023
1 parent a6cc5a1 commit 5ccf882
Show file tree
Hide file tree
Showing 34 changed files with 101 additions and 80 deletions.
8 changes: 5 additions & 3 deletions components/DebugPanel.js
Original file line number Diff line number Diff line change
@@ -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'

/**
*
Expand All @@ -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))
}, [])

Expand Down Expand Up @@ -71,7 +73,7 @@ const DebugPanel = () => {
<div className='flex'>
<Select
label={locale.COMMON.THEME_SWITCH}
value={theme}
value={currentTheme}
options={themeOptions}
onChange={handleUpdateDebugTheme}
/>
Expand Down
42 changes: 21 additions & 21 deletions components/ExternalPlugins.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import BLOG from 'blog.config'
import { siteConfig } from '@/lib/config'
import dynamic from 'next/dynamic'
import WebWhiz from './Webwhiz'

Expand Down Expand Up @@ -30,26 +30,26 @@ const AdBlockDetect = dynamic(() => import('@/components/AdBlockDetect'), { ssr:
*/
const ExternalPlugin = (props) => {
return <>
{JSON.parse(BLOG.THEME_SWITCH) && <ThemeSwitch />}
{JSON.parse(BLOG.DEBUG) && <DebugPanel />}
{BLOG.ANALYTICS_ACKEE_TRACKER && <Ackee />}
{BLOG.ANALYTICS_GOOGLE_ID && <Gtag />}
{BLOG.ANALYTICS_VERCEL && <Analytics />}
{JSON.parse(BLOG.ANALYTICS_BUSUANZI_ENABLE) && <Busuanzi />}
{BLOG.ADSENSE_GOOGLE_ID && <GoogleAdsense />}
{BLOG.FACEBOOK_APP_ID && BLOG.FACEBOOK_PAGE_ID && <Messenger />}
{JSON.parse(BLOG.FIREWORKS) && <Fireworks />}
{JSON.parse(BLOG.SAKURA) && <Sakura />}
{JSON.parse(BLOG.STARRY_SKY) && <StarrySky />}
{JSON.parse(BLOG.MUSIC_PLAYER) && <MusicPlayer />}
{JSON.parse(BLOG.NEST) && <Nest />}
{JSON.parse(BLOG.FLUTTERINGRIBBON) && <FlutteringRibbon />}
{JSON.parse(BLOG.COMMENT_TWIKOO_COUNT_ENABLE) && <TwikooCommentCounter {...props}/>}
{JSON.parse(BLOG.RIBBON) && <Ribbon />}
{JSON.parse(BLOG.CUSTOM_RIGHT_CLICK_CONTEXT_MENU) && <CustomContextMenu {...props} />}
{!JSON.parse(BLOG.CAN_COPY) && <DisableCopy/>}
{JSON.parse(BLOG.WEB_WHIZ_ENABLED) && <WebWhiz/>}
{JSON.parse(BLOG.AD_WWADS_BLOCK_DETECT) && <AdBlockDetect/>}
{JSON.parse(siteConfig('THEME_SWITCH')) && <ThemeSwitch />}
{JSON.parse(siteConfig('DEBUG')) && <DebugPanel />}
{siteConfig('ANALYTICS_ACKEE_TRACKER') && <Ackee />}
{siteConfig('ANALYTICS_GOOGLE_ID') && <Gtag />}
{siteConfig('ANALYTICS_VERCEL') && <Analytics />}
{JSON.parse(siteConfig('ANALYTICS_BUSUANZI_ENABLE')) && <Busuanzi />}
{siteConfig('ADSENSE_GOOGLE_ID') && <GoogleAdsense />}
{siteConfig('FACEBOOK_APP_ID') && siteConfig('FACEBOOK_PAGE_ID') && <Messenger />}
{JSON.parse(siteConfig('FIREWORKS')) && <Fireworks />}
{JSON.parse(siteConfig('SAKURA')) && <Sakura />}
{JSON.parse(siteConfig('STARRY_SKY')) && <StarrySky />}
{JSON.parse(siteConfig('MUSIC_PLAYER')) && <MusicPlayer />}
{JSON.parse(siteConfig('NEST')) && <Nest />}
{JSON.parse(siteConfig('FLUTTERINGRIBBON')) && <FlutteringRibbon />}
{JSON.parse(siteConfig('COMMENT_TWIKOO_COUNT_ENABLE')) && <TwikooCommentCounter {...props}/>}
{JSON.parse(siteConfig('RIBBON')) && <Ribbon />}
{JSON.parse(siteConfig('CUSTOM_RIGHT_CLICK_CONTEXT_MENU')) && <CustomContextMenu {...props} />}
{!JSON.parse(siteConfig('CAN_COPY')) && <DisableCopy/>}
{JSON.parse(siteConfig('WEB_WHIZ_ENABLED')) && <WebWhiz/>}
{JSON.parse(siteConfig('AD_WWADS_BLOCK_DETECT')) && <AdBlockDetect/>}
<VConsole/>
</>
}
Expand Down
8 changes: 5 additions & 3 deletions components/ThemeSwitch.js
Original file line number Diff line number Diff line change
@@ -1,16 +1,18 @@
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 主题切换
*/
const ThemeSwitch = () => {
const { theme } = useGlobal()
const router = useRouter()
const currentTheme = getQueryParam(router.asPath, 'theme') || theme
const [isLoading, setIsLoading] = useState(false)

// 修改当前路径url中的 theme 参数
Expand All @@ -31,13 +33,13 @@ const ThemeSwitch = () => {
<div className="p-3 w-full flex items-center text-sm group duration-200 transition-all">
<DarkModeButton className='mr-2' />
<div className='w-0 group-hover:w-20 transition-all duration-200 overflow-hidden'>
<select value={theme} onChange={onSelectChange} name="themes" className='appearance-none outline-none dark:text-white bg-gray-50 dark:bg-black uppercase cursor-pointer'>
<select value={currentTheme} onChange={onSelectChange} name="themes" className='appearance-none outline-none dark:text-white bg-gray-50 dark:bg-black uppercase cursor-pointer'>
{THEMES?.map(t => {
return <option key={t} value={t}>{t}</option>
})}
</select>
</div>
<i className="fa-solid fa-palette pl-2"></i>
<i className="fa-solid fa-palette pl-2"/>
</div>
</div>
{/* 切换主题加载时的全屏遮罩 */}
Expand Down
20 changes: 19 additions & 1 deletion lib/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import BLOG from '@/blog.config'
import { useGlobal } from './global'
import { deepClone } from './utils'

/**
* 读取配置
Expand Down Expand Up @@ -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
}
10 changes: 4 additions & 6 deletions lib/global.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) // 抓取文章数据

// 切换主题
Expand Down Expand Up @@ -98,7 +97,6 @@ export function GlobalContextProvider(props) {

/**
* 切换主题时的特殊处理
* @param {*} setTheme
*/
const checkThemeDOM = () => {
if (isBrowser) {
Expand Down
3 changes: 2 additions & 1 deletion lib/lang.js
Original file line number Diff line number Diff line change
Expand Up @@ -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}`
Expand Down Expand Up @@ -70,6 +70,7 @@ export function initLocale(lang, locale, changeLang, changeLocale) {
if (queryLang !== lang) {
currentLang = queryLang
}
console.log('初始化语言', currentLang)
changeLang(currentLang)
saveLangToCookies(currentLang)

Expand Down
2 changes: 1 addition & 1 deletion pages/404.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ const NoFound = props => {
props = { ...props, meta }

// 根据页面路径加载不同Layout文件
const Layout = getLayoutByTheme(useRouter())
const Layout = getLayoutByTheme({ theme: siteConfig('THEME'), router: useRouter() })

return <Layout {...props} />
}
Expand Down
4 changes: 2 additions & 2 deletions pages/[prefix]/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ const Slug = props => {
}, [post])

const meta = {
title: post ? `${post?.title} | ${siteConfig('TITLE')}` : `${props?.siteConfig('TITLE') || BLOG.TITLE} | loading`,
title: post ? `${post?.title} | ${siteConfig('TITLE')}` : `${siteConfig('TITLE')} | loading`,
description: post?.summary,
type: post?.type,
slug: post?.slug,
Expand All @@ -77,7 +77,7 @@ const Slug = props => {
}
props = { ...props, lock, meta, setLock, validPassword }
// 根据页面路径加载不同Layout文件
const Layout = getLayoutByTheme(useRouter())
const Layout = getLayoutByTheme({ theme: siteConfig('THEME'), router: useRouter() })
return <Layout {...props} />
}

Expand Down
2 changes: 1 addition & 1 deletion pages/archive/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ const ArchiveIndex = props => {
const { locale } = useGlobal()

// 根据页面路径加载不同Layout文件
const Layout = getLayoutByTheme(useRouter())
const Layout = getLayoutByTheme({ theme: siteConfig('THEME'), router: useRouter() })

useEffect(() => {
if (isBrowser) {
Expand Down
2 changes: 1 addition & 1 deletion pages/category/[category]/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ export default function Category(props) {
const { locale } = useGlobal()

// 根据页面路径加载不同Layout文件
const Layout = getLayoutByTheme(useRouter())
const Layout = getLayoutByTheme({ theme: siteConfig('THEME'), router: useRouter() })

const meta = {
title: `${props.category} | ${locale.COMMON.CATEGORY} | ${
Expand Down
2 changes: 1 addition & 1 deletion pages/category/[category]/page/[page].js
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ export default function Category(props) {
const { siteInfo } = props
const { locale } = useGlobal()
// 根据页面路径加载不同Layout文件
const Layout = getLayoutByTheme(useRouter())
const Layout = getLayoutByTheme({ theme: siteConfig('THEME'), router: useRouter() })

const meta = {
title: `${props.category} | ${locale.COMMON.CATEGORY} | ${
Expand Down
2 changes: 1 addition & 1 deletion pages/category/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ export default function Category(props) {
const { siteInfo } = props

// 根据页面路径加载不同Layout文件
const Layout = getLayoutByTheme(useRouter())
const Layout = getLayoutByTheme({ theme: siteConfig('THEME'), router: useRouter() })

const meta = {
title: `${locale.COMMON.CATEGORY} | ${siteConfig('TITLE')}`,
Expand Down
4 changes: 2 additions & 2 deletions pages/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@ import { getPostBlocks } from '@/lib/notion'
import { getGlobalData } from '@/lib/notion/getNotionData'
import { generateRss } from '@/lib/rss'
import { generateRobotsTxt } from '@/lib/robots.txt'
import { useRouter } from 'next/router'
import { getLayoutByTheme } from '@/themes/theme'
import { siteConfig } from '@/lib/config'
import { useRouter } from 'next/router'

/**
* 首页布局
Expand All @@ -14,7 +14,7 @@ import { siteConfig } from '@/lib/config'
*/
const Index = props => {
// 根据页面路径加载不同Layout文件
const Layout = getLayoutByTheme(useRouter())
const Layout = getLayoutByTheme({ theme: siteConfig('THEME'), router: useRouter() })

const meta = {
title: `${siteConfig('TITLE')} | ${siteConfig('BIO')}`,
Expand Down
2 changes: 1 addition & 1 deletion pages/page/[page].js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ const Page = props => {
const { siteInfo } = props

// 根据页面路径加载不同Layout文件
const Layout = getLayoutByTheme(useRouter())
const Layout = getLayoutByTheme({ theme: siteConfig('THEME'), router: useRouter() })

const meta = {
title: `${props?.page} | Page | ${siteConfig('TITLE')}`,
Expand Down
2 changes: 1 addition & 1 deletion pages/search/[keyword]/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ const Index = props => {
const { locale } = useGlobal()

// 根据页面路径加载不同Layout文件
const Layout = getLayoutByTheme(useRouter())
const Layout = getLayoutByTheme({ theme: siteConfig('THEME'), router: useRouter() })

const meta = {
title: `${keyword || ''}${keyword ? ' | ' : ''}${locale.NAV.SEARCH} | ${siteConfig('TITLE')}`,
Expand Down
2 changes: 1 addition & 1 deletion pages/search/[keyword]/page/[page].js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ const Index = props => {
const { locale } = useGlobal()

// 根据页面路径加载不同Layout文件
const Layout = getLayoutByTheme(useRouter())
const Layout = getLayoutByTheme({ theme: siteConfig('THEME'), router: useRouter() })

const meta = {
title: `${keyword || ''}${keyword ? ' | ' : ''}${locale.NAV.SEARCH} | ${siteConfig('TITLE')}`,
Expand Down
2 changes: 1 addition & 1 deletion pages/search/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ const Search = props => {
const { locale } = useGlobal()

// 根据页面路径加载不同Layout文件
const Layout = getLayoutByTheme(useRouter())
const Layout = getLayoutByTheme({ theme: siteConfig('THEME'), router: useRouter() })

const router = useRouter()
const keyword = getSearchKey(router)
Expand Down
2 changes: 1 addition & 1 deletion pages/tag/[tag]/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ const Tag = props => {
const { tag, siteInfo } = props

// 根据页面路径加载不同Layout文件
const Layout = getLayoutByTheme(useRouter())
const Layout = getLayoutByTheme({ theme: siteConfig('THEME'), router: useRouter() })

const meta = {
title: `${tag} | ${locale.COMMON.TAGS} | ${siteConfig('TITLE')}`,
Expand Down
2 changes: 1 addition & 1 deletion pages/tag/[tag]/page/[page].js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ const Tag = props => {
const { tag, siteInfo } = props

// 根据页面路径加载不同Layout文件
const Layout = getLayoutByTheme(useRouter())
const Layout = getLayoutByTheme({ theme: siteConfig('THEME'), router: useRouter() })

const meta = {
title: `${tag} | ${locale.COMMON.TAGS} | ${siteConfig('TITLE')}`,
Expand Down
2 changes: 1 addition & 1 deletion pages/tag/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ const TagIndex = props => {
const { siteInfo } = props

// 根据页面路径加载不同Layout文件
const Layout = getLayoutByTheme(useRouter())
const Layout = getLayoutByTheme({ theme: siteConfig('THEME'), router: useRouter() })

const meta = {
title: `${locale.COMMON.TAGS} | ${siteConfig('TITLE')}`,
Expand Down
4 changes: 2 additions & 2 deletions themes/example/components/BlogPostCard.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,9 +41,9 @@ const BlogPostCard = ({ post }) => {
</div>
{/* 图片封面 */}
{showPageCover && (
<div className="md:w-5/12 w-full overflow-hidden p-1">
<div className="md:w-5/12 w-full h-44 overflow-hidden p-1">
<Link href={`${BLOG.SUB_PATH}/${post.slug}`} passHref legacyBehavior>
<LazyImage src={post?.pageCoverThumbnail} className='h-44 bg-center bg-cover hover:scale-110 duration-200' />
<LazyImage src={post?.pageCoverThumbnail} className='w-full bg-cover hover:scale-110 duration-200' />
</Link>
</div>
)}
Expand Down
2 changes: 1 addition & 1 deletion themes/example/components/Title.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import { siteConfig } from '@/lib/config'
*/
export const Title = (props) => {
const { post } = props
const title = post?.title || siteConfig('HOME_BANNER_IMAGE')
const title = post?.title || siteConfig('TITLE')
const description = post?.description || siteConfig('AUTHOR')

return <div className="text-center px-6 py-12 mb-6 bg-gray-100 dark:bg-hexo-black-gray dark:border-hexo-black-gray border-b">
Expand Down
2 changes: 1 addition & 1 deletion themes/fukasawa/components/AsideLeft.js
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ function AsideLeft(props) {
<Logo {...props} />

<section className='siteInfo flex flex-col dark:text-gray-300 pt-8'>
{siteConfig('HOME_BANNER_IMAGE')}
{siteConfig('DESCRIPTION')}
</section>

<section className='flex flex-col text-gray-600'>
Expand Down
4 changes: 2 additions & 2 deletions themes/fukasawa/components/MenuItemCollapse.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,8 @@ export const MenuItemCollapse = (props) => {

{/* 折叠子菜单 */}
{hasSubMenu && <Collapse isOpen={isOpen} onHeightChange={props.onHeightChange}>
{link.subMenus.map(sLink => {
return <div key={sLink.id} className='whitespace-nowrap dark:text-gray-200
{link.subMenus.map((sLink, index) => {
return <div key={index} className='whitespace-nowrap dark:text-gray-200
not:last-child:border-b-0 border-b dark:border-gray-800 py-2 px-14 cursor-pointer hover:bg-gray-100
font-extralight dark:bg-black text-left justify-start text-gray-600 bg-gray-50 dark:hover:bg-gray-900 tracking-widest transition-all duration-200'>
<Link href={sLink.to} target={link?.to?.indexOf('http') === 0 ? '_blank' : '_self'}>
Expand Down
4 changes: 2 additions & 2 deletions themes/heo/components/MenuItemCollapse.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,8 @@ export const MenuItemCollapse = ({ link }) => {

{/* 折叠子菜单 */}
{hasSubMenu && <Collapse isOpen={isOpen} className='rounded-xl'>
{link.subMenus.map(sLink => {
return <div key={sLink.id} className='dark:bg-black dark:text-gray-200 text-left px-3 justify-start bg-gray-50 hover:bg-gray-50 dark:hover:bg-gray-900 tracking-widest transition-all duration-200 py-3 pr-6'>
{link.subMenus.map((sLink, index) => {
return <div key={index} className='dark:bg-black dark:text-gray-200 text-left px-3 justify-start bg-gray-50 hover:bg-gray-50 dark:hover:bg-gray-900 tracking-widest transition-all duration-200 py-3 pr-6'>
<Link href={sLink.to} target={link?.to?.indexOf('http') === 0 ? '_blank' : '_self'}>
<span className='text-sm ml-4 whitespace-nowrap'>{link?.icon && <i className={sLink.icon + ' mr-2'} />} {sLink.title}</span>
</Link>
Expand Down
Loading

0 comments on commit 5ccf882

Please sign in to comment.