mirror of
https://github.com/d0zingcat/NotionNext.git
synced 2026-05-20 15:09:40 +00:00
部分参数配置化
This commit is contained in:
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