mirror of
https://github.com/d0zingcat/NotionNext.git
synced 2026-06-01 15:10:14 +00:00
{配置兼容性}
This commit is contained in:
@@ -2,7 +2,7 @@
|
|||||||
|
|
||||||
import BLOG from '@/blog.config'
|
import BLOG from '@/blog.config'
|
||||||
import { useGlobal } from './global'
|
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
|
return defaultVal
|
||||||
}
|
}
|
||||||
|
|
||||||
// 从Notion_CONFIG读取的配置通常都是字符串,适当转义
|
|
||||||
return convertVal(val)
|
return convertVal(val)
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 配置默认都是string类型;
|
* 从环境变量和NotionConfig读取的配置都是string类型;
|
||||||
* 识别配置的值是否数字、布尔、[]数组,若是则转成对应类型
|
* 这里识别出配置的字符值若为否 数字、布尔、[]数组,{}对象,若是则转成对应类型
|
||||||
|
* 使用JSON和eval两个函数
|
||||||
* @param {*} val
|
* @param {*} val
|
||||||
* @returns
|
* @returns
|
||||||
*/
|
*/
|
||||||
export const convertVal = val => {
|
export const convertVal = val => {
|
||||||
if (typeof val === 'string') {
|
// 如果传入参数本身就是obj、数组、boolean 就无需处理
|
||||||
// 解析布尔
|
if (typeof val !== 'string' || !val) {
|
||||||
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
|
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
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|||||||
@@ -6,7 +6,7 @@ import formatDate from '../utils/formatDate'
|
|||||||
import md5 from 'js-md5'
|
import md5 from 'js-md5'
|
||||||
import { siteConfig } from '../config'
|
import { siteConfig } from '../config'
|
||||||
import {
|
import {
|
||||||
checkContainHttp,
|
checkStartWithHttp,
|
||||||
convertUrlStartWithOneSlash,
|
convertUrlStartWithOneSlash,
|
||||||
sliceUrlFromHttp
|
sliceUrlFromHttp
|
||||||
} from '../utils'
|
} 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)
|
? sliceUrlFromHttp(properties?.href)
|
||||||
: convertUrlStartWithOneSlash(properties?.href)
|
: convertUrlStartWithOneSlash(properties?.href)
|
||||||
|
|
||||||
|
|||||||
@@ -81,8 +81,21 @@ export function convertUrlStartWithOneSlash(str) {
|
|||||||
return 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
|
// 检查字符串是否包含http
|
||||||
if (str?.includes('http:') || str?.includes('https:')) {
|
if (str?.includes('http:') || str?.includes('https:')) {
|
||||||
// 如果包含,找到http的位置
|
// 如果包含,找到http的位置
|
||||||
|
|||||||
@@ -1,7 +1,7 @@
|
|||||||
/**
|
/**
|
||||||
* 文章相关工具
|
* 文章相关工具
|
||||||
*/
|
*/
|
||||||
import { checkContainHttp } from '.'
|
import { checkStartWithHttp } from '.'
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 获取文章的关联推荐文章列表,目前根据标签关联性筛选
|
* 获取文章的关联推荐文章列表,目前根据标签关联性筛选
|
||||||
@@ -50,7 +50,7 @@ export function checkSlugHasNoSlash(row) {
|
|||||||
}
|
}
|
||||||
return (
|
return (
|
||||||
(slug.match(/\//g) || []).length === 0 &&
|
(slug.match(/\//g) || []).length === 0 &&
|
||||||
!checkContainHttp(slug) &&
|
!checkStartWithHttp(slug) &&
|
||||||
row.type.indexOf('Menu') < 0
|
row.type.indexOf('Menu') < 0
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
@@ -67,7 +67,7 @@ export function checkSlugHasOneSlash(row) {
|
|||||||
}
|
}
|
||||||
return (
|
return (
|
||||||
(slug.match(/\//g) || []).length === 1 &&
|
(slug.match(/\//g) || []).length === 1 &&
|
||||||
!checkContainHttp(slug) &&
|
!checkStartWithHttp(slug) &&
|
||||||
row.type.indexOf('Menu') < 0
|
row.type.indexOf('Menu') < 0
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
@@ -85,6 +85,6 @@ export function checkSlugHasMorThanTwoSlash(row) {
|
|||||||
return (
|
return (
|
||||||
(slug.match(/\//g) || []).length >= 2 &&
|
(slug.match(/\//g) || []).length >= 2 &&
|
||||||
row.type.indexOf('Menu') < 0 &&
|
row.type.indexOf('Menu') < 0 &&
|
||||||
!checkContainHttp(slug)
|
!checkStartWithHttp(slug)
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,5 +1,5 @@
|
|||||||
import { siteConfig } from '@/lib/config'
|
import { siteConfig } from '@/lib/config'
|
||||||
import { checkContainHttp } from '@/lib/utils'
|
import { checkStartWithHttp } from '@/lib/utils'
|
||||||
import Link from 'next/link'
|
import Link from 'next/link'
|
||||||
import { useRouter } from 'next/router'
|
import { useRouter } from 'next/router'
|
||||||
import NotionIcon from './NotionIcon'
|
import NotionIcon from './NotionIcon'
|
||||||
@@ -23,7 +23,7 @@ const BlogPostCard = ({ post, className }) => {
|
|||||||
return (
|
return (
|
||||||
<Link
|
<Link
|
||||||
href={post?.href}
|
href={post?.href}
|
||||||
target={checkContainHttp(post.slug) ? '_blank' : '_self'}
|
target={checkStartWithHttp(post.slug) ? '_blank' : '_self'}
|
||||||
passHref>
|
passHref>
|
||||||
<div
|
<div
|
||||||
key={post.id}
|
key={post.id}
|
||||||
|
|||||||
Reference in New Issue
Block a user