From 19d9d896c493a842aecb5a67eb9286d95d4e9f3c Mon Sep 17 00:00:00 2001 From: tangly1024 Date: Sat, 15 Jun 2024 10:34:55 +0800 Subject: [PATCH] =?UTF-8?q?{=E9=85=8D=E7=BD=AE=E5=85=BC=E5=AE=B9=E6=80=A7}?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- lib/config.js | 78 ++++++++++++++++----------- lib/notion/getPageProperties.js | 4 +- lib/utils/index.js | 15 +++++- lib/utils/post.js | 8 +-- themes/nav/components/BlogPostCard.js | 4 +- 5 files changed, 69 insertions(+), 40 deletions(-) diff --git a/lib/config.js b/lib/config.js index 19287d8a..ee6ce65b 100644 --- a/lib/config.js +++ b/lib/config.js @@ -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 } /** diff --git a/lib/notion/getPageProperties.js b/lib/notion/getPageProperties.js index d24b5e2b..0b44c910 100644 --- a/lib/notion/getPageProperties.js +++ b/lib/notion/getPageProperties.js @@ -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) diff --git a/lib/utils/index.js b/lib/utils/index.js index b8d78f30..716bdfdb 100644 --- a/lib/utils/index.js +++ b/lib/utils/index.js @@ -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的位置 diff --git a/lib/utils/post.js b/lib/utils/post.js index 87852eda..e6402a02 100644 --- a/lib/utils/post.js +++ b/lib/utils/post.js @@ -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) ) } diff --git a/themes/nav/components/BlogPostCard.js b/themes/nav/components/BlogPostCard.js index b449c0d2..0f92a30a 100755 --- a/themes/nav/components/BlogPostCard.js +++ b/themes/nav/components/BlogPostCard.js @@ -1,5 +1,5 @@ import { siteConfig } from '@/lib/config' -import { checkContainHttp } from '@/lib/utils' +import { checkStartWithHttp } from '@/lib/utils' import Link from 'next/link' import { useRouter } from 'next/router' import NotionIcon from './NotionIcon' @@ -23,7 +23,7 @@ const BlogPostCard = ({ post, className }) => { return (