|
1 | 1 | <!doctype html> |
2 | 2 |
|
3 | 3 | <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" |
5 | 5 | license="CC BY-SA 4.0 International"></markdown-html> |
6 | 6 |
|
7 | 7 | <head> |
|
16 | 16 | <script src="https://lib.baomitu.com/translate.js/3.7.2/translate.js" type="text/javascript"></script> |
17 | 17 | <script src="https://lf6-cdn-tos.bytecdntp.com/cdn/expire-1-y/Darkmode.js/1.5.7/darkmode-js.min.js" |
18 | 18 | 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> |
20 | 21 | <script> |
21 | 22 | // 使用 IIFE 包裹以避免全局变量污染 |
22 | 23 | (function () { |
|
85 | 86 |
|
86 | 87 | // 文档解析功能相关 |
87 | 88 | var notExistIndexPage = null; // 是否不存在索引页 |
| 89 | + var pValue = null; |
88 | 90 | 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 | + } |
90 | 96 | } |
91 | 97 |
|
92 | 98 | // PJAX 相关 |
|
251 | 257 | } |
252 | 258 |
|
253 | 259 | // 判断路径是否为相对路径 |
254 | | - const isRelativePath = (path) => !path.startsWith('/'); |
| 260 | + function isRelativePath(path) { |
| 261 | + return !path.match(/^\//); |
| 262 | + } |
255 | 263 |
|
256 | 264 | // 处理错误 |
257 | 265 | function updateContent(article, title = null, mdPath = null) { |
|
346 | 354 | } |
347 | 355 | } |
348 | 356 | try { |
| 357 | + pValue = mdPath ? mdPath : path; |
349 | 358 | updateContent(await getMarkdown((mdPath ? mdPath : path), response)) |
350 | 359 | document.querySelector('html').classList.add('loaded'); //// 防止缓存的页面被自定义 PJAX 中覆写的处理响应的函数捕获 |
351 | 360 | waitForAnimationsEnd(document.querySelector('.markdown-body'), '.content, .old-content', ['Animation-slideInRight', 'Animation-slideInLeft']).then(async () => { |
|
405 | 414 | throw new Error('请求失败:' + mdPath + ',' + response.headers.get('content-type') + ',' + response.status + ',' + response.statusText); |
406 | 415 | } |
407 | 416 | } |
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); |
409 | 443 | if (content === '') { |
410 | 444 | content = '<i>空文件</i>'; |
411 | 445 | } |
|
416 | 450 | const outline = generateOutline(doc); |
417 | 451 | doc.querySelector('.outline').innerHTML += outline; |
418 | 452 |
|
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 | | - }); |
440 | 453 | return doc.body.innerHTML; |
441 | 454 | }; |
442 | 455 |
|
|
0 commit comments