{配置兼容性}

This commit is contained in:
tangly1024
2024-06-15 10:34:55 +08:00
parent 2e983e4876
commit 19d9d896c4
5 changed files with 69 additions and 40 deletions

View File

@@ -2,7 +2,7 @@
import BLOG from '@/blog.config'
import { useGlobal } from './global'
import { deepClone } from './utils'
import { deepClone, isUrl } from './utils'
/**
* 读取配置顺序
@@ -91,46 +91,62 @@ export const siteConfig = (key, defaultVal = null, extendConfig = {}) => {
return defaultVal
}
// 从Notion_CONFIG读取的配置通常都是字符串适当转义
return convertVal(val)
}
/**
* 配置默认都是string类型
* 识别配置的值是否数字、布尔、[]数组,若是则转成对应类型
* 从环境变量和NotionConfig读取的配置都是string类型
* 这里识别配置的字符值若为否 数字、布尔、[]数组,{}对象,若是则转成对应类型
* 使用JSON和eval两个函数
* @param {*} val
* @returns
*/
export const convertVal = val => {
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 格式,直接返回字符串
// 如果传入参数本身就是obj、数组、boolean 就无需处理
if (typeof val !== 'string' || !val) {
return val
}
// 解析数字parseInt将字符串转换为数字
if (/^\d+$/.test(val)) {
return parseInt(val)
}
// 检测是否url
if (isUrl(val)) {
return val
}
// 检测是否url
if (val === 'true' || val === 'false') {
return JSON.parse(val)
}
// 配置值前可能有污染的空格
if (!val.indexOf('[') > 0 || val.indexOf('{')) {
return val
}
// 转换 [] , {} , true/false 这类字符串为对象
try {
// 尝试解析json
const parsedJson = JSON.parse(val)
if (parsedJson !== null) {
return parsedJson
}
} catch (error) {
try {
// 尝试解析对象,对象解析能力不如上一步的json
const evalObj = eval('(' + val + ')')
if (evalObj !== null) {
return evalObj
}
} catch (error) {
// Ojbject 解析失败,返回原始字符串值
return val
}
return val
}
return val
}
/**

View File

@@ -6,7 +6,7 @@ import formatDate from '../utils/formatDate'
import md5 from 'js-md5'
import { siteConfig } from '../config'
import {
checkContainHttp,
checkStartWithHttp,
convertUrlStartWithOneSlash,
sliceUrlFromHttp
} from '../utils'
@@ -197,7 +197,7 @@ export function adjustPageProperties(properties, NOTION_CONFIG) {
}
// 检查处理外链
properties.href = checkContainHttp(properties?.href)
properties.href = checkStartWithHttp(properties?.href)
? sliceUrlFromHttp(properties?.href)
: convertUrlStartWithOneSlash(properties?.href)

View File

@@ -81,8 +81,21 @@ export function convertUrlStartWithOneSlash(str) {
return str
}
/**
* 是否是一个相对或绝对路径的ur类
* @param {*} str
* @returns
*/
export function isUrl(str) {
if (!str) {
return false
}
return str?.indexOf('/') === 0 || checkStartWithHttp(str)
}
// 检查是否外链
export function checkContainHttp(str) {
export function checkStartWithHttp(str) {
// 检查字符串是否包含http
if (str?.includes('http:') || str?.includes('https:')) {
// 如果包含找到http的位置

View File

@@ -1,7 +1,7 @@
/**
* 文章相关工具
*/
import { checkContainHttp } from '.'
import { checkStartWithHttp } from '.'
/**
* 获取文章的关联推荐文章列表,目前根据标签关联性筛选
@@ -50,7 +50,7 @@ export function checkSlugHasNoSlash(row) {
}
return (
(slug.match(/\//g) || []).length === 0 &&
!checkContainHttp(slug) &&
!checkStartWithHttp(slug) &&
row.type.indexOf('Menu') < 0
)
}
@@ -67,7 +67,7 @@ export function checkSlugHasOneSlash(row) {
}
return (
(slug.match(/\//g) || []).length === 1 &&
!checkContainHttp(slug) &&
!checkStartWithHttp(slug) &&
row.type.indexOf('Menu') < 0
)
}
@@ -85,6 +85,6 @@ export function checkSlugHasMorThanTwoSlash(row) {
return (
(slug.match(/\//g) || []).length >= 2 &&
row.type.indexOf('Menu') < 0 &&
!checkContainHttp(slug)
!checkStartWithHttp(slug)
)
}