mirror of
https://github.com/d0zingcat/NotionNext.git
synced 2026-05-21 15:09:43 +00:00
部分参数配置化
This commit is contained in:
@@ -9,10 +9,11 @@ import { deepClone } from './utils'
|
||||
* 1. 优先读取NotionConfig表
|
||||
* 2. 其次读取环境变量
|
||||
* 3. 再读取blog.config.js文件
|
||||
* @param {*} key
|
||||
* @param {*} key ; 参数名
|
||||
* @param {*} defaultVal ; 参数不存在默认返回值
|
||||
* @returns
|
||||
*/
|
||||
export const siteConfig = (key) => {
|
||||
export const siteConfig = (key, defaultVal = null) => {
|
||||
let global = null
|
||||
try {
|
||||
// eslint-disable-next-line react-hooks/rules-of-hooks
|
||||
@@ -36,7 +37,7 @@ export const siteConfig = (key) => {
|
||||
val = siteInfo?.pageCover // 封面图取Notion的封面
|
||||
break
|
||||
case 'AVATAR':
|
||||
val = siteInfo?.icon // 封面图取Notion的封面
|
||||
val = siteInfo?.icon // 封面图取Notion的头像
|
||||
break
|
||||
case 'TITLE':
|
||||
val = siteInfo?.title // 标题取Notion中的标题
|
||||
@@ -48,7 +49,10 @@ export const siteConfig = (key) => {
|
||||
if (!val) {
|
||||
val = BLOG[key]
|
||||
}
|
||||
console.log('实际配置', key, val)
|
||||
if (!val) {
|
||||
val = defaultVal
|
||||
}
|
||||
// console.log('实际配置', key, val)
|
||||
return val
|
||||
}
|
||||
|
||||
|
||||
50
lib/utils.js
50
lib/utils.js
@@ -75,7 +75,6 @@ export function getQueryVariable(key) {
|
||||
}
|
||||
return (false)
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取 URL 中指定参数的值
|
||||
* @param {string} url
|
||||
@@ -83,8 +82,10 @@ export function getQueryVariable(key) {
|
||||
* @returns {string|null}
|
||||
*/
|
||||
export function getQueryParam(url, param) {
|
||||
const searchParams = new URLSearchParams(url.split('?')[1])
|
||||
return searchParams.get(param)
|
||||
// 移除哈希部分
|
||||
const urlWithoutHash = url.split('#')[0];
|
||||
const searchParams = new URLSearchParams(urlWithoutHash.split('?')[1]);
|
||||
return searchParams.get(param);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -202,3 +203,46 @@ export const isMobile = () => {
|
||||
|
||||
return isMobile
|
||||
}
|
||||
|
||||
/**
|
||||
* 扫描页面上的所有文本节点,将url格式的文本转为可点击链接
|
||||
* @param {*} node
|
||||
*/
|
||||
export const scanAndConvertToLinks = (node) => {
|
||||
if (node.nodeType === Node.TEXT_NODE) {
|
||||
const text = node.textContent;
|
||||
const urlRegex = /https?:\/\/[^\s]+/g;
|
||||
let lastIndex = 0;
|
||||
let match;
|
||||
|
||||
const newNode = document.createElement('span');
|
||||
|
||||
while ((match = urlRegex.exec(text)) !== null) {
|
||||
const beforeText = text.substring(lastIndex, match.index);
|
||||
const url = match[0];
|
||||
|
||||
if (beforeText) {
|
||||
newNode.appendChild(document.createTextNode(beforeText));
|
||||
}
|
||||
|
||||
const link = document.createElement('a');
|
||||
link.href = url;
|
||||
link.target = '_blank'
|
||||
link.textContent = url;
|
||||
|
||||
newNode.appendChild(link);
|
||||
|
||||
lastIndex = urlRegex.lastIndex;
|
||||
}
|
||||
|
||||
if (lastIndex < text.length) {
|
||||
newNode.appendChild(document.createTextNode(text.substring(lastIndex)));
|
||||
}
|
||||
|
||||
node.parentNode.replaceChild(newNode, node);
|
||||
} else if (node.nodeType === Node.ELEMENT_NODE) {
|
||||
for (const childNode of node.childNodes) {
|
||||
scanAndConvertToLinks(childNode);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user