Просмотр исходного кода

【修复】主页访问问题

PJ568 1 год назад
Родитель
Сommit
b2f61fd6b5
1 измененных файлов с 41 добавлено и 36 удалено
  1. 41 36
      index.html

+ 41 - 36
index.html

@@ -1,7 +1,8 @@
 <!doctype html>
-<markdown-html version="1.4.1" author="PJ568" repo="https://github.com/PJ-568/markdown.html"
-  license="CC BY-SA 4.0 International" />
+
 <html>
+<markdown-html version="1.4.2" author="PJ568" repo="https://github.com/PJ-568/markdown.html"
+  license="CC BY-SA 4.0 International"></markdown-html>
 
 <head>
   <meta charset="utf-8">
@@ -168,9 +169,7 @@
       }
 
       // 获取 markdown.html 的当前路径
-      function getPath() {
-        return getDirectory(window.location);
-      }
+      const getPath = getDirectory(window.location);
 
       // 将 ?p= 后面的值转换为绝对路径
       /* 
@@ -181,25 +180,6 @@
         const url = new URL(path);
         const params = new URLSearchParams(url.search);
         path = formatMDPath(params.get('p'));
-        // 如果 path 为 null
-        if (!path) {
-          const currentDir = getPath();
-          // 如果存在 index.md 文件,则返回 index.md,否则返回 README.md
-          if (notExistIndexPage) {
-            path = currentDir + 'README.md';
-          } else if (notExistIndexPage === false) {
-            path = currentDir + 'index.md';
-          } else {
-            const response = await fetch(currentDir + 'index.md');
-            if (response.ok) {
-              notExistIndexPage = false;
-              path = currentDir + 'index.md';
-            } else {
-              notExistIndexPage = true;
-              path = currentDir + 'README.md';
-            }
-          }
-        }
         return path;
       }
 
@@ -232,17 +212,37 @@
 
       // 更新页面的 markdown 内容
       /* 
-        path: 要加载的 markdown 文件路径,最好是带主机名的绝对路径(要用于 fetch())
+        mdPath: 要加载的 markdown 文件路径,最好是带主机名的绝对路径(要用于 fetch())
         response: fetch() 的返回值,用于防止二次访问
        */
       const showMarkdown = async (mdPath, response = null) => {
+        if (mdPath === null) {
+          var path = null;
+          // 如果存在 index.md 文件,则返回 index.md,否则返回 README.md
+          if (notExistIndexPage) {
+            path = getPath + 'README.md';
+          } else if (notExistIndexPage === false) {
+            path = getPath + 'index.md';
+          } else {
+            response = await fetch(getPath + 'index.md');
+            if (response.ok) {
+              notExistIndexPage = false;
+              path = getPath + 'index.md';
+            } else {
+              response = null;
+              notExistIndexPage = true;
+              path = getPath + 'README.md';
+            }
+          }
+        }
         try {
-          const content = await getMarkdown(mdPath, response);
+          const content = await getMarkdown((mdPath ? mdPath : path), response);
           document.querySelector('.markdown-body').innerHTML = content;
           const firstH1 = document.querySelector('h1');
-          document.title = firstH1 ? firstH1.textContent : mdPath;
+          document.title = firstH1 ? firstH1.textContent : (mdPath ? mdPath : path);
+          document.querySelector('html').classList.add('loaded');	//// 防止缓存的页面被自定义 PJAX 中覆写的处理响应的函数捕获
+          customPushState(window.location.origin + window.location.pathname + (mdPath ? ('?p=' + mdPath) : ''));
           showMarkdownEnded();
-          customPushState(window.location.origin + window.location.pathname + '?p=' + mdPath);
         } catch (error) {
           handleError(error)
         }
@@ -316,7 +316,7 @@
             if (pValue) {
               href = getDirectory(mdPath) + href;
             } else {
-              href = getPath() + href;
+              href = getPath + href;
             }
             const absoluteHref = new URL(href, 'http://example.com').pathname;
             link.setAttribute('href', `${window.location.pathname}?p=${absoluteHref}`);
@@ -327,17 +327,21 @@
         return doc.body.innerHTML;
       };
 
-      // 初始化自定义 PJAX
-      function initCustomPJAX() {
+      // 初始化自定义 PJAX 响应
+      function initCustomPJAXResponse() {
         // 覆写 PJAX 处理响应的函数,在内容插入页面之前实现 markdown 的渲染
         PJAX._handleResponse = PJAX.handleResponse;
         PJAX.handleResponse = async function (responseText, request, href, options) {
-          if (request.responseText.startsWith('<!doctype html><markdown-html' || '<!doctype html>\n<markdown-html')) {
+          if (request.responseText.startsWith('<!doctype html><html><markdown-html') || request.responseText.startsWith('<!doctype html>\n\n<html>\n<markdown-html')) {
             await showMarkdown(await decodeMDPath(href));
           } else {
             PJAX._handleResponse(responseText, request, href, options);
           }
         }
+      }
+
+      // 初始化自定义 PJAX 事件监听器
+      function initCustomPJAXEventListener() {
         // 监听自定义链接的点击事件
         if (existPushState) {
           document.querySelector('.markdown-body').addEventListener('click', async function (event) {
@@ -389,7 +393,8 @@
         initContent();	//// 初始化内容
         initTranslate();	//// 初始化页面翻译
         initPJAX();	//// 初始化 PJAX
-        initCustomPJAX();	//// 初始化自定义 PJAX
+        initCustomPJAXResponse();	//// 初始化自定义 PJAX 响应
+        initCustomPJAXEventListener();	//// 初始化自定义 PJAX 事件监听器
       }
 
       // 触发器
@@ -404,8 +409,9 @@
         startLoad();
       });
       //// 监听 PJAX 完成后,重新加载
-      document.addEventListener('pjax:complete', function () {
-        initCustomPJAX();	//// 初始化自定义 PJAX
+      document.addEventListener('pjax:complete', async function () {
+        await showMarkdown(await getPValue());
+        initCustomPJAXEventListener();	//// 重新初始化自定义 PJAX 事件监听器
       });
       //// 点击事件委托
       document.addEventListener('click', function (event) {
@@ -439,7 +445,6 @@
         infoTranslate();	//// 检查并提示翻译
         translate.execute();	//// 手动触发翻译,但是理论上 translate-js 会监听页面变化
         endLoad();
-        document.querySelector('html').classList.add('loaded');	//// 防止缓存的页面被自定义 PJAX 中覆写的处理响应的函数捕获
         window.scrollTo({
           top: 0,
           behavior: 'smooth'