Skip to content

Commit e4dc717

Browse files
committed
【格式】注释 getMarkdown 函数
- 详细注释了 getMarkdown 函数的各个部分,解释了其工作原理和实现细节 - 对原有代码逻辑未作任何修改,仅添加注释以提高代码可读性
1 parent 2d36802 commit e4dc717

1 file changed

Lines changed: 15 additions & 15 deletions

File tree

index.html

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -457,7 +457,7 @@
457457

458458
// 读取并解析指定文件
459459
const getMarkdown = async (mdPath, response = null) => {
460-
// 生成目录
460+
//// 生成目录
461461
function generateOutline(doc) {
462462
const headings = Array.from(doc.querySelectorAll('h1, h2, h3, h4, h5, h6'));
463463
if (headings.length === 0) return '<i>无目录</i>';
@@ -467,10 +467,10 @@
467467
headings.forEach(heading => {
468468
const level = parseInt(heading.tagName.charAt(1));
469469
const id = heading.textContent.toLowerCase()
470-
.replace(/ /g, '-') // 将空格替换为连字符
471-
.replace(/[^\w\u4e00-\u9fa5-]/g, ''); // 保留字母、数字、下划线、中文字符和连字符
470+
.replace(/ /g, '-') //// 将空格替换为连字符
471+
.replace(/[^\w\u4e00-\u9fa5-]/g, ''); //// 保留字母、数字、下划线、中文字符和连字符
472472

473-
// 设置 id 属性以便链接跳转
473+
//// 设置 id 属性以便链接跳转
474474
heading.setAttribute('id', id);
475475

476476
while (stack.length > 0 && stack[stack.length - 1].level >= level) {
@@ -505,16 +505,16 @@
505505
}
506506
var markdown_content = await response.text();
507507
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 是否为相对路径……最后转换为 <到该文件的绝对路径>
513513

514514
const linkParser = new DOMParser();
515515
const linkDoc = linkParser.parseFromString(`<div>${content}</div>`, 'text/html');
516516

517-
// 处理所有以 .md 结尾的链接
517+
//// 处理所有以 .md 结尾的链接
518518
const mdLinks = linkDoc.querySelectorAll('a[href$=".md"]');
519519
mdLinks.forEach(link => {
520520
const href = link.getAttribute('href');
@@ -525,7 +525,7 @@
525525
}
526526
});
527527

528-
// 处理所有图片
528+
//// 处理所有图片
529529
const images = linkDoc.querySelectorAll('img');
530530
images.forEach(img => {
531531
const src = img.getAttribute('src');
@@ -537,15 +537,15 @@
537537
}
538538
});
539539

540-
// 更新处理后的 markdown 内容
540+
//// 更新处理后的 markdown 内容
541541
content = linkDoc.body.innerHTML.replace(/^<div>|<\/div>$/g, '');
542542

543-
// 空文件
543+
//// 空文件
544544
if (content === '') {
545545
content = '<i>空文件</i>';
546546
}
547547

548-
// 添加目录
548+
//// 添加目录
549549
content = `<details class="outline"><summary>${decodeURIComponent(mdPath)}</summary></details><hr>${content}`;
550550
const parser = new DOMParser();
551551
const doc = parser.parseFromString(content, 'text/html');
@@ -558,7 +558,7 @@
558558

559559
// 初始化自定义 PJAX 响应
560560
function initCustomPJAXResponse() {
561-
// 覆写 PJAX 处理响应的函数,在内容插入页面之前实现 markdown 的渲染
561+
//// 覆写 PJAX 处理响应的函数,在内容插入页面之前实现 markdown 的渲染
562562
PJAX._handleResponse = PJAX.handleResponse;
563563
PJAX.handleResponse = async function (responseText, request, href, options) {
564564
if (request.responseText.startsWith('<!doctype html><html><markdown-html') || request.responseText.startsWith(`<!doctype html>

0 commit comments

Comments
 (0)