多语言配置,starter主题微调、兼容

This commit is contained in:
tangly1024.com
2024-04-12 12:44:02 +08:00
parent 16779a0614
commit aefca7171e
10 changed files with 597 additions and 500 deletions

View File

@@ -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
}
}

View File

@@ -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 {}
}
}