Skip to content

Commit 0b6b353

Browse files
committed
【新增】增加外链判断功能并优化内链处理逻辑
- 新增 isExternalLink 函数用于判断链接是否为外链 - 优化相对路径判断逻辑,增加对特殊协议的排除 - 为图片链接添加外链判断和处理 - 优化点击事件处理,仅当链接为外链时在新窗口打开
1 parent 245dd47 commit 0b6b353

1 file changed

Lines changed: 55 additions & 8 deletions

File tree

index.html

Lines changed: 55 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<!doctype html>
22

33
<html>
4-
<markdown-html version="1.16.0" author="PJ568" repo="https://github.com/PJ-568/markdown.html"
4+
<markdown-html version="1.17.0" author="PJ568" repo="https://github.com/PJ-568/markdown.html"
55
license="CC BY-SA 4.0 International"></markdown-html>
66

77
<head>
@@ -283,9 +283,36 @@
283283
return mdPath;
284284
}
285285

286+
// 判断链接是否为外链
287+
function isExternalLink(url) {
288+
if (!url || typeof url !== 'string') return false;
289+
290+
//// 排除特殊协议
291+
const excludedProtocols = ['#', 'javascript:', 'mailto:', 'tel:', 'data:'];
292+
if (excludedProtocols.some(proto => url.startsWith(proto))) {
293+
return false;
294+
}
295+
296+
try {
297+
const currentUrl = new URL(window.location.href);
298+
const targetUrl = new URL(url, currentUrl.origin);
299+
300+
//// 排除非 HTTP(S) 协议
301+
if (!['http:', 'https:'].includes(targetUrl.protocol)) return false;
302+
303+
//// 域名统一转小写比较
304+
const currentHost = currentUrl.hostname.toLowerCase();
305+
const targetHost = targetUrl.hostname.toLowerCase();
306+
307+
return currentHost !== targetHost;
308+
} catch (e) {
309+
return false;
310+
}
311+
}
312+
286313
// 判断路径是否为相对路径
287314
function isRelativePath(path) {
288-
return !path.match(/^\//);
315+
return !path.match(/^\//); //// 不以 / 开头
289316
}
290317

291318
// 处理错误
@@ -454,7 +481,9 @@
454481
links.forEach(link => {
455482
const linkText = link.match(/\[.*\]/)[0].slice(1, -1);
456483
var linkHref = link.match(/\(.*\)/)[0].slice(1, -1);
457-
if (isRelativePath(linkHref)) {
484+
if (isExternalLink(linkHref)) {
485+
;
486+
} else if (isRelativePath(linkHref)) {
458487
if (pValue) {
459488
linkHref = getDirectory(mdPath) + linkHref;
460489
} else {
@@ -466,6 +495,25 @@
466495
markdown_content = markdown_content.replace(link, newLink);
467496
});
468497
}
498+
const images = markdown_content.match(/!\[.*\]\(.*\)/g);
499+
if (images) {
500+
images.forEach(image => {
501+
const imageText = image.match(/\[.*\]/)[0].slice(1, -1);
502+
var imageHref = image.match(/\(.*\)/)[0].slice(1, -1);
503+
if (isExternalLink(imageHref)) {
504+
;
505+
} else if (isRelativePath(imageHref)) {
506+
if (pValue) {
507+
imageHref = getDirectory(mdPath) + imageHref;
508+
} else {
509+
imageHref = getPath() + imageHref;
510+
}
511+
imageHref = new URL(imageHref, 'http://example.com').pathname;
512+
}
513+
const newImage = `![${imageText}](${imageHref})`;
514+
markdown_content = markdown_content.replace(image, newImage);
515+
});
516+
}
469517
var content = marked.parse(markdown_content);
470518
if (content === '') {
471519
content = '<i>空文件</i>';
@@ -625,12 +673,11 @@
625673
// console.error(`ID 为 ${targetId} 的元素不存在。`);
626674
// }
627675
// }
628-
} else if (event.target.closest('.content a:not([href^="?"]):not([href^="#"]):not([href^="javascript:"])')) {
629-
const link = event.target.closest('a');
630-
if (link && link.href) {
631-
window.open(link.href, '_blank');
676+
} else if (event.target.closest('.content a[href]:not([href=""])')) {
677+
const href = event.target.getAttribute('href');
678+
if (isExternalLink(href)) {
679+
window.open(href, '_blank');
632680
event.preventDefault();
633-
return;
634681
}
635682
}
636683
});

0 commit comments

Comments
 (0)