diff --git a/lib/config.js b/lib/config.js index 546597c2..ee56f91d 100644 --- a/lib/config.js +++ b/lib/config.js @@ -52,7 +52,7 @@ export const siteConfig = (key, defaultVal = null, extendConfig = null) => { } } - // 其次 有传入的配置参考,则尝试读取 + // 其次 有传入的extendConfig,则尝试读取 if (!val && extendConfig) { val = extendConfig[key] } @@ -64,24 +64,37 @@ export const siteConfig = (key, defaultVal = null, extendConfig = null) => { if (!val) { return defaultVal - } else { - if (typeof val === 'string') { - if (val === 'true' || val === 'false') { - return JSON.parse(val) - } - if (/^\d+$/.test(val)) { - // 如果是数字,使用parseFloat或者parseInt将字符串转换为数字 - return parseInt(val) - } - return val - } else { - try { - return JSON.parse(val) - } catch (error) { - // 如果值是一个字符串但不是有效的 JSON 格式,直接返回字符串 - return val - } + } + + // 从Notion_CONFIG读取的配置通常都是字符串,适当转义 + if (typeof val === 'string') { + // 解析布尔 + if (val === 'true' || val === 'false') { + return JSON.parse(val) } + + // 解析数字,parseInt将字符串转换为数字 + if (/^\d+$/.test(val)) { + return parseInt(val) + } + // 转移 [] , {} 这种json串为json对象 + try { + const parsedJson = JSON.parse(val) + // 检查解析后的结果是否是对象或数组 + if (typeof parsedJson === 'object' && parsedJson !== null) { + return parsedJson + } + } catch (error) { + // JSON 解析失败,返回原始字符串值 + return val + } + } + + try { + return JSON.parse(val) + } catch (error) { + // 如果值是一个字符串但不是有效的 JSON 格式,直接返回字符串 + return val } } diff --git a/lib/notion/getNotionConfig.js b/lib/notion/getNotionConfig.js index 9985d977..889005f2 100644 --- a/lib/notion/getNotionConfig.js +++ b/lib/notion/getNotionConfig.js @@ -7,6 +7,7 @@ * */ import { getDateValue, getTextContent } from 'notion-utils' +import { deepClone } from '../utils' import getAllPageIds from './getAllPageIds' import { getPostBlocks } from './getPostBlocks' @@ -162,9 +163,12 @@ export async function getConfigMapFromConfigPage(allPages) { } // 最后检查Notion_Config页面的INLINE_CONFIG,是否是一个js对象 - const inlineConfigs = parseConfig(configPage?.INLINE_CONFIG) - - return Object.assign({}, notionConfig, inlineConfigs) + const combine = Object.assign( + {}, + deepClone(notionConfig), + parseConfig(notionConfig?.INLINE_CONFIG) + ) + return combine } /** @@ -176,19 +180,13 @@ export function parseConfig(configString) { if (!configString) { return {} } + // 解析对象 try { - // 尝试解析为 JSON 对象 - const jsonConfig = JSON.parse(configString) - return jsonConfig - } catch (error) { - // 如果解析失败,则尝试使用 eval() - try { - // eslint-disable-next-line no-eval - const config = eval('(' + configString + ')') - return config - } catch (evalError) { - console.error('解析 INLINE_CONFIG 配置时出错:', evalError) - return {} - } + // eslint-disable-next-line no-eval + const config = eval('(' + configString + ')') + return config + } catch (evalError) { + console.error('解析 eval(INLINE_CONFIG) 配置时出错:', evalError) + return {} } } diff --git a/pages/[prefix]/[slug]/[...suffix].js b/pages/[prefix]/[slug]/[...suffix].js index 0d912a0b..fb958af6 100644 --- a/pages/[prefix]/[slug]/[...suffix].js +++ b/pages/[prefix]/[slug]/[...suffix].js @@ -55,13 +55,14 @@ export async function getStaticProps({ locale }) { let fullSlug = prefix + '/' + slug + '/' + suffix.join('/') - if (JSON.parse(BLOG.PSEUDO_STATIC)) { + const from = `slug-props-${fullSlug}` + const props = await getGlobalData({ from, locale }) + if (siteConfig('PSEUDO_STATIC', BLOG.PSEUDO_STATIC, props.NOTION_CONFIG)) { if (!fullSlug.endsWith('.html')) { fullSlug += '.html' } } - const from = `slug-props-${fullSlug}` - const props = await getGlobalData({ from, locale }) + // 在列表内查找文章 props.post = props?.allPages?.find(p => { return ( diff --git a/pages/[prefix]/[slug]/index.js b/pages/[prefix]/[slug]/index.js index 88c1fc42..d2c00861 100644 --- a/pages/[prefix]/[slug]/index.js +++ b/pages/[prefix]/[slug]/index.js @@ -39,13 +39,14 @@ export async function getStaticPaths() { export async function getStaticProps({ params: { prefix, slug }, locale }) { let fullSlug = prefix + '/' + slug - if (JSON.parse(BLOG.PSEUDO_STATIC)) { + const from = `slug-props-${fullSlug}` + const props = await getGlobalData({ from, locale }) + + if (siteConfig('PSEUDO_STATIC', BLOG.PSEUDO_STATIC, props.NOTION_CONFIG)) { if (!fullSlug.endsWith('.html')) { fullSlug += '.html' } } - const from = `slug-props-${fullSlug}` - const props = await getGlobalData({ from, locale }) // 在列表内查找文章 props.post = props?.allPages?.find(p => { return ( diff --git a/pages/[prefix]/index.js b/pages/[prefix]/index.js index 408a3cc3..a1eb0088 100644 --- a/pages/[prefix]/index.js +++ b/pages/[prefix]/index.js @@ -81,13 +81,14 @@ export async function getStaticPaths() { export async function getStaticProps({ params: { prefix }, locale }) { let fullSlug = prefix - if (JSON.parse(BLOG.PSEUDO_STATIC)) { + const from = `slug-props-${fullSlug}` + const props = await getGlobalData({ from, locale }) + if (siteConfig('PSEUDO_STATIC', BLOG.PSEUDO_STATIC, props.NOTION_CONFIG)) { if (!fullSlug.endsWith('.html')) { fullSlug += '.html' } } - const from = `slug-props-${fullSlug}` - const props = await getGlobalData({ from, locale }) + // 在列表内查找文章 props.post = props?.allPages?.find(p => { return ( diff --git a/pages/_document.js b/pages/_document.js index 9c8beadb..a61e2b96 100644 --- a/pages/_document.js +++ b/pages/_document.js @@ -1,6 +1,6 @@ // eslint-disable-next-line @next/next/no-document-import-in-page -import Document, { Html, Head, Main, NextScript } from 'next/document' import BLOG from '@/blog.config' +import Document, { Head, Html, Main, NextScript } from 'next/document' class MyDocument extends Document { static async getInitialProps(ctx) { @@ -10,29 +10,52 @@ class MyDocument extends Document { render() { return ( - -
- - {/* 预加载字体 */} - {BLOG.FONT_AWESOME && <> - - - >} + + + + {/* 预加载字体 */} + {BLOG.FONT_AWESOME && ( + <> + + + > + )} - {BLOG.FONT_URL?.map((fontUrl, index) => { - if (fontUrl.endsWith('.css') || fontUrl.includes('googleapis.com/css')) { - return - } else { - return - } - })} - + {BLOG.FONT_URL?.map((fontUrl, index) => { + if ( + fontUrl.endsWith('.css') || + fontUrl.includes('googleapis.com/css') + ) { + return + } else { + return ( + + ) + } + })} + - - -