Skip to content

Commit 8ae0c47

Browse files
committed
【修复】相对路径前未加上当前路径问题
- 更新 mermaid.js CDN 链接 - 优化 getPValue 函数以避免重复解析 URL - 重构 isRelativePath 函数以使用正则表达式 - 在请求 Markdown 内容前预处理内部链接 - 移除重复的链接处理代码
1 parent 05f5e5f commit 8ae0c47

2 files changed

Lines changed: 42 additions & 29 deletions

File tree

index.html

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

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

77
<head>
@@ -16,7 +16,8 @@
1616
<script src="https://lib.baomitu.com/translate.js/3.7.2/translate.js" type="text/javascript"></script>
1717
<script src="https://lf6-cdn-tos.bytecdntp.com/cdn/expire-1-y/Darkmode.js/1.5.7/darkmode-js.min.js"
1818
type="text/javascript"></script>
19-
<script src="https://cdnjs.cloudflare.com/ajax/libs/mermaid/11.5.0/mermaid.min.js" type="text/javascript"></script>
19+
<script src="https://lf9-cdn-tos.bytecdntp.com/cdn/expire-1-y/mermaid/8.14.0/mermaid.min.js"
20+
type="text/javascript"></script>
2021
<script>
2122
// 使用 IIFE 包裹以避免全局变量污染
2223
(function () {
@@ -85,8 +86,13 @@
8586

8687
// 文档解析功能相关
8788
var notExistIndexPage = null; // 是否不存在索引页
89+
var pValue = null;
8890
async function getPValue() {
89-
return await decodeMDPath(window.location.href);
91+
if (pValue) {
92+
return pValue;
93+
} else {
94+
return await decodeMDPath(window.location.href);
95+
}
9096
}
9197

9298
// PJAX 相关
@@ -251,7 +257,9 @@
251257
}
252258

253259
// 判断路径是否为相对路径
254-
const isRelativePath = (path) => !path.startsWith('/');
260+
function isRelativePath(path) {
261+
return !path.match(/^\//);
262+
}
255263

256264
// 处理错误
257265
function updateContent(article, title = null, mdPath = null) {
@@ -346,6 +354,7 @@
346354
}
347355
}
348356
try {
357+
pValue = mdPath ? mdPath : path;
349358
updateContent(await getMarkdown((mdPath ? mdPath : path), response))
350359
document.querySelector('html').classList.add('loaded'); //// 防止缓存的页面被自定义 PJAX 中覆写的处理响应的函数捕获
351360
waitForAnimationsEnd(document.querySelector('.markdown-body'), '.content, .old-content', ['Animation-slideInRight', 'Animation-slideInLeft']).then(async () => {
@@ -405,7 +414,32 @@
405414
throw new Error('请求失败:' + mdPath + ',' + response.headers.get('content-type') + ',' + response.status + ',' + response.statusText);
406415
}
407416
}
408-
var content = marked.parse(await response.text());
417+
var markdown_content = await response.text();
418+
// 如果 markdown_content 中发现 `[任意标题](路径文件名.md)`
419+
/*
420+
如果 href 是相对路径,则在其前加上当前读取的文档的绝对路径;
421+
如果不存在当前读取的文档(默认读取的 index.md 或 README.md),
422+
则在其前加上 markdown.html 的绝对路径。
423+
*/
424+
const links = markdown_content.match(/\[.*\]\(.*\.md\)/g);
425+
if (links) {
426+
links.forEach(link => {
427+
const linkText = link.match(/\[.*\]/)[0].slice(1, -1);
428+
var linkHref = link.match(/\(.*\)/)[0].slice(1, -1);
429+
if (isRelativePath(linkHref)) {
430+
if (pValue) {
431+
linkHref = getDirectory(mdPath) + linkHref;
432+
} else {
433+
linkHref = getPath() + linkHref;
434+
}
435+
console.log(isRelativePath(linkHref), linkHref, pValue);
436+
linkHref = new URL(linkHref, 'http://example.com').pathname;
437+
}
438+
const newLink = `[${linkText}](${window.location.pathname}?p=${linkHref})`;
439+
markdown_content = markdown_content.replace(link, newLink);
440+
});
441+
}
442+
var content = marked.parse(markdown_content);
409443
if (content === '') {
410444
content = '<i>空文件</i>';
411445
}
@@ -416,27 +450,6 @@
416450
const outline = generateOutline(doc);
417451
doc.querySelector('.outline').innerHTML += outline;
418452

419-
const links = doc.querySelectorAll('a[href$=".md"]');
420-
const pValue = await getPValue()
421-
links.forEach(link => {
422-
var href = link.getAttribute('href');
423-
/*
424-
如果 href 是相对路径,则在其前加上当前读取的文档的绝对路径;
425-
如果不存在当前读取的文档(默认读取的 index.md 或 README.md),
426-
则在其前加上 markdown.html 的绝对路径。
427-
*/
428-
if (isRelativePath(href)) {
429-
if (pValue) {
430-
href = getDirectory(mdPath) + href;
431-
} else {
432-
href = getPath() + href;
433-
}
434-
const absoluteHref = new URL(href, 'http://example.com').pathname;
435-
link.setAttribute('href', `${window.location.pathname}?p=${absoluteHref}`);
436-
} else if (href.startsWith('/')) {
437-
link.setAttribute('href', `${window.location.pathname}?p=${href}`);
438-
}
439-
});
440453
return doc.body.innerHTML;
441454
};
442455

0 commit comments

Comments
 (0)