|
457 | 457 |
|
458 | 458 | // 读取并解析指定文件 |
459 | 459 | const getMarkdown = async (mdPath, response = null) => { |
460 | | - // 生成目录 |
| 460 | + //// 生成目录 |
461 | 461 | function generateOutline(doc) { |
462 | 462 | const headings = Array.from(doc.querySelectorAll('h1, h2, h3, h4, h5, h6')); |
463 | 463 | if (headings.length === 0) return '<i>无目录</i>'; |
|
467 | 467 | headings.forEach(heading => { |
468 | 468 | const level = parseInt(heading.tagName.charAt(1)); |
469 | 469 | const id = heading.textContent.toLowerCase() |
470 | | - .replace(/ /g, '-') // 将空格替换为连字符 |
471 | | - .replace(/[^\w\u4e00-\u9fa5-]/g, ''); // 保留字母、数字、下划线、中文字符和连字符 |
| 470 | + .replace(/ /g, '-') //// 将空格替换为连字符 |
| 471 | + .replace(/[^\w\u4e00-\u9fa5-]/g, ''); //// 保留字母、数字、下划线、中文字符和连字符 |
472 | 472 |
|
473 | | - // 设置 id 属性以便链接跳转 |
| 473 | + //// 设置 id 属性以便链接跳转 |
474 | 474 | heading.setAttribute('id', id); |
475 | 475 |
|
476 | 476 | while (stack.length > 0 && stack[stack.length - 1].level >= level) { |
|
505 | 505 | } |
506 | 506 | var markdown_content = await response.text(); |
507 | 507 | var content = marked.parse(markdown_content); |
508 | | - // 因为是在本页面渲染一个其它页面的文档: |
509 | | - // - 从 content 中获取所有 href 以 .md 结尾的 a 标签; |
510 | | - // - 遍历这些 a 标签,如果 href 不是外链,则判断 href 是否为相对路径……最后转换为 ?p=<到该文件的绝对路径> |
511 | | - // - 从 content 中获取所有 img 标签; |
512 | | - // - 遍历这些 img 标签,如果 src 不是外链,则判断 src 是否为相对路径……最后转换为 <到该文件的绝对路径> |
| 508 | + //// 因为是在本页面渲染一个其它页面的文档: |
| 509 | + //// - 从 content 中获取所有 href 以 .md 结尾的 a 标签; |
| 510 | + //// - 遍历这些 a 标签,如果 href 不是外链,则判断 href 是否为相对路径……最后转换为 ?p=<到该文件的绝对路径> |
| 511 | + //// - 从 content 中获取所有 img 标签; |
| 512 | + //// - 遍历这些 img 标签,如果 src 不是外链,则判断 src 是否为相对路径……最后转换为 <到该文件的绝对路径> |
513 | 513 |
|
514 | 514 | const linkParser = new DOMParser(); |
515 | 515 | const linkDoc = linkParser.parseFromString(`<div>${content}</div>`, 'text/html'); |
516 | 516 |
|
517 | | - // 处理所有以 .md 结尾的链接 |
| 517 | + //// 处理所有以 .md 结尾的链接 |
518 | 518 | const mdLinks = linkDoc.querySelectorAll('a[href$=".md"]'); |
519 | 519 | mdLinks.forEach(link => { |
520 | 520 | const href = link.getAttribute('href'); |
|
525 | 525 | } |
526 | 526 | }); |
527 | 527 |
|
528 | | - // 处理所有图片 |
| 528 | + //// 处理所有图片 |
529 | 529 | const images = linkDoc.querySelectorAll('img'); |
530 | 530 | images.forEach(img => { |
531 | 531 | const src = img.getAttribute('src'); |
|
537 | 537 | } |
538 | 538 | }); |
539 | 539 |
|
540 | | - // 更新处理后的 markdown 内容 |
| 540 | + //// 更新处理后的 markdown 内容 |
541 | 541 | content = linkDoc.body.innerHTML.replace(/^<div>|<\/div>$/g, ''); |
542 | 542 |
|
543 | | - // 空文件 |
| 543 | + //// 空文件 |
544 | 544 | if (content === '') { |
545 | 545 | content = '<i>空文件</i>'; |
546 | 546 | } |
547 | 547 |
|
548 | | - // 添加目录 |
| 548 | + //// 添加目录 |
549 | 549 | content = `<details class="outline"><summary>${decodeURIComponent(mdPath)}</summary></details><hr>${content}`; |
550 | 550 | const parser = new DOMParser(); |
551 | 551 | const doc = parser.parseFromString(content, 'text/html'); |
|
558 | 558 |
|
559 | 559 | // 初始化自定义 PJAX 响应 |
560 | 560 | function initCustomPJAXResponse() { |
561 | | - // 覆写 PJAX 处理响应的函数,在内容插入页面之前实现 markdown 的渲染 |
| 561 | + //// 覆写 PJAX 处理响应的函数,在内容插入页面之前实现 markdown 的渲染 |
562 | 562 | PJAX._handleResponse = PJAX.handleResponse; |
563 | 563 | PJAX.handleResponse = async function (responseText, request, href, options) { |
564 | 564 | if (request.responseText.startsWith('<!doctype html><html><markdown-html') || request.responseText.startsWith(`<!doctype html> |
|
0 commit comments