mirror of
https://github.com/d0zingcat/NotionNext.git
synced 2026-05-17 15:09:31 +00:00
部分配置siteConfig化
This commit is contained in:
49
lib/config.js
Normal file
49
lib/config.js
Normal file
@@ -0,0 +1,49 @@
|
||||
'use client'
|
||||
|
||||
import BLOG from '@/blog.config'
|
||||
import { useGlobal } from './global'
|
||||
|
||||
/**
|
||||
* 读取配置
|
||||
* @param {*} key
|
||||
* @returns
|
||||
*/
|
||||
export const siteConfig = (key) => {
|
||||
let global = null
|
||||
try {
|
||||
// eslint-disable-next-line react-hooks/rules-of-hooks
|
||||
global = useGlobal()
|
||||
} catch (error) {}
|
||||
|
||||
// 首先 配置最优先读取NOTION中的表格配置
|
||||
let val = null
|
||||
let siteInfo = null
|
||||
|
||||
if (global) {
|
||||
val = global.NOTION_CONFIG?.[key]
|
||||
siteInfo = global.siteInfo
|
||||
console.log('当前变量', key, val)
|
||||
}
|
||||
|
||||
if (!val) {
|
||||
// 这里针对部分key做一些兼容处理
|
||||
switch (key) {
|
||||
case 'HOME_BANNER_IMAGE':
|
||||
val = siteInfo?.pageCover // 封面图取Notion的封面
|
||||
break
|
||||
case 'AVATAR':
|
||||
val = siteInfo?.icon // 封面图取Notion的封面
|
||||
break
|
||||
case 'TITLE':
|
||||
val = siteConfig('TITLE') // 标题取Notion中的标题
|
||||
break
|
||||
}
|
||||
}
|
||||
|
||||
// 其次 NOTION没有找到配置,则会读取blog.config.js文件
|
||||
if (!val) {
|
||||
val = BLOG[key]
|
||||
}
|
||||
console.log('配置', key, val)
|
||||
return val
|
||||
}
|
||||
@@ -1,10 +1,10 @@
|
||||
import { generateLocaleDict, initLocale } from './lang'
|
||||
import { createContext, useContext, useEffect, useState } from 'react'
|
||||
import { useRouter } from 'next/router'
|
||||
import BLOG from '@/blog.config'
|
||||
import { THEMES, initDarkMode } from '@/themes/theme'
|
||||
import NProgress from 'nprogress'
|
||||
import { getQueryVariable, isBrowser } from './utils'
|
||||
import BLOG from '@/blog.config'
|
||||
|
||||
const GlobalContext = createContext()
|
||||
|
||||
@@ -15,7 +15,7 @@ const GlobalContext = createContext()
|
||||
* @constructor
|
||||
*/
|
||||
export function GlobalContextProvider(props) {
|
||||
const { children, siteInfo, categoryOptions, tagOptions } = props
|
||||
const { children, siteInfo, categoryOptions, tagOptions, NOTION_CONFIG } = props
|
||||
const router = useRouter()
|
||||
const [lang, updateLang] = useState(BLOG.LANG) // 默认语言
|
||||
const [locale, updateLocale] = useState(generateLocaleDict(BLOG.LANG)) // 默认语言
|
||||
@@ -68,6 +68,7 @@ export function GlobalContextProvider(props) {
|
||||
|
||||
return (
|
||||
<GlobalContext.Provider value={{
|
||||
NOTION_CONFIG,
|
||||
onLoading,
|
||||
setOnLoading,
|
||||
locale,
|
||||
|
||||
@@ -69,7 +69,7 @@ export async function getConfigMapFromConfigPage(allPages) {
|
||||
console.error(`pageId "${configTableId}" is not a database`)
|
||||
return null
|
||||
}
|
||||
console.log('表格', databaseRecordMap, block, rawMetadata)
|
||||
// console.log('表格', databaseRecordMap, block, rawMetadata)
|
||||
const collectionId = rawMetadata?.collection_id
|
||||
const collection = pageRecordMap.collection[collectionId].value
|
||||
const collectionQuery = pageRecordMap.collection_query
|
||||
@@ -127,7 +127,7 @@ export async function getConfigMapFromConfigPage(allPages) {
|
||||
|
||||
// 只导入生效的配置
|
||||
if (config.enable) {
|
||||
console.log('[覆盖代码配置]', config.key)
|
||||
// console.log('[覆盖代码配置]', config.key)
|
||||
notionConfig[config.key] = config.value
|
||||
}
|
||||
}
|
||||
|
||||
@@ -285,7 +285,7 @@ async function getDataBaseInfoByNotionAPI({ pageId, from }) {
|
||||
})
|
||||
|
||||
// 站点配置优先读取配置表格,否则读取blog.config.js 文件
|
||||
const CONFIG = await getConfigMapFromConfigPage(collectionData) || BLOG
|
||||
const NOTION_CONFIG = await getConfigMapFromConfigPage(collectionData) || BLOG
|
||||
|
||||
// Sort by date
|
||||
if (BLOG.POSTS_SORT_BY === 'date') {
|
||||
@@ -305,7 +305,7 @@ async function getDataBaseInfoByNotionAPI({ pageId, from }) {
|
||||
const allNavPages = getNavPages({ allPages })
|
||||
|
||||
return {
|
||||
CONFIG,
|
||||
NOTION_CONFIG,
|
||||
notice,
|
||||
siteInfo,
|
||||
allPages,
|
||||
|
||||
Reference in New Issue
Block a user