1
0

index.html 42 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323
  1. <!DOCTYPE html>
  2. <html>
  3. <markdown-html
  4. version="1.18.8"
  5. author="PJ568"
  6. repo="https://github.com/PJ-568/markdown.html"
  7. license="CC BY-SA 4.0 International"
  8. ></markdown-html>
  9. <head>
  10. <meta charset="utf-8" />
  11. <meta name="description" content="HTML read markdown" />
  12. <meta
  13. name="keywords"
  14. content="markdown, html, renderer, low-dependency, one-click deployment"
  15. />
  16. <meta name="author" content="PJ568" />
  17. <link
  18. rel="icon"
  19. type="image/svg+xml"
  20. href="data:image/svg+xml;charset=utf-8;base64,PHN2ZyB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciIHdpZHRoPSI1MTIiIGhlaWdodD0iNTEyIiB2aWV3Qm94PSIwIDAgNTEyIDUxMiI+CiAgPCEtLSDog4zmma/mlrnlnZfku6PooajmlofmoaMgLS0+CiAgPHJlY3QgeD0iNjQiIHk9IjY0IiB3aWR0aD0iMzg0IiBoZWlnaHQ9IjM4NCIgcng9IjI0IiBmaWxsPSIjMkU3RDMyIiBzdHJva2U9IiMxQjVFMjAiIHN0cm9rZS13aWR0aD0iOCIvPgogIAogIDwhLS0g5paH5qGj5YaF5a6557q/5p2hIC0tPgogIDxyZWN0IHg9Ijk2IiB5PSIxMjgiIHdpZHRoPSIzMjAiIGhlaWdodD0iMTYiIHJ4PSI0IiBmaWxsPSIjRThGNUU5Ii8+CiAgPHJlY3QgeD0iOTYiIHk9IjE2OCIgd2lkdGg9IjI4MCIgaGVpZ2h0PSIxNiIgcng9IjQiIGZpbGw9IiNFOEY1RTkiLz4KICA8cmVjdCB4PSI5NiIgeT0iMjA4IiB3aWR0aD0iMjQwIiBoZWlnaHQ9IjE2IiByeD0iNCIgZmlsbD0iI0U4RjVFOSIvPgogIDxyZWN0IHg9Ijk2IiB5PSIyNDgiIHdpZHRoPSIzMjAiIGhlaWdodD0iMTYiIHJ4PSI0IiBmaWxsPSIjRThGNUU5Ii8+CiAgPHJlY3QgeD0iOTYiIHk9IjI4OCIgd2lkdGg9IjI4MCIgaGVpZ2h0PSIxNiIgcng9IjQiIGZpbGw9IiNFOEY1RTkiLz4KICA8cmVjdCB4PSI5NiIgeT0iMzI4IiB3aWR0aD0iMjQwIiBoZWlnaHQ9IjE2IiByeD0iNCIgZmlsbD0iI0U4RjVFOSIvPgogIAogIDwhLS0g6aG555uu5ZCN56ew57yp5YaZIC0tPgogIDx0ZXh0IHg9IjI1NiIgeT0iNDIwIiBmb250LWZhbWlseT0iU2FyYXNhIFRlcm0gU0MgTmVyZCcsICdNYXBsZSBNb25vIENOJywgVWJ1bnR1LCBSb2JvdG8sICdPcGVuIFNhbnMnLCAnTWljcm9zb2Z0IFlhSGVpJywgQXJpYWwsIHNhbnMtc2VyaWYiIGZvbnQtc2l6ZT0iNDgiIGZvbnQtd2VpZ2h0PSJib2xkIiB0ZXh0LWFuY2hvcj0ibWlkZGxlIiBmaWxsPSIjRThGNUU5Ij4mbHQ7TUgmZ3Q7PC90ZXh0Pgo8L3N2Zz4="
  21. />
  22. <meta name="viewport" content="width=256, initial-scale=1" />
  23. <script
  24. defer
  25. src="https://lf6-cdn-tos.bytecdntp.com/cdn/expire-1-y/marked/4.0.2/marked.min.js"
  26. type="text/javascript"
  27. ></script>
  28. <script
  29. src="https://lib.baomitu.com/pjax/0.2.8/pjax.min.js"
  30. type="text/javascript"
  31. ></script>
  32. <script
  33. src="https://lib.baomitu.com/translate.js/3.7.2/translate.js"
  34. type="text/javascript"
  35. ></script>
  36. <script
  37. src="https://lf6-cdn-tos.bytecdntp.com/cdn/expire-1-y/Darkmode.js/1.5.7/darkmode-js.min.js"
  38. type="application/javascript"
  39. ></script>
  40. <script
  41. src="https://lf9-cdn-tos.bytecdntp.com/cdn/expire-1-y/mermaid/8.14.0/mermaid.min.js"
  42. type="application/javascript"
  43. ></script>
  44. <script
  45. src="https://lf3-cdn-tos.bytecdntp.com/cdn/expire-1-y/highlight.js/11.4.0/highlight.min.js"
  46. type="application/javascript"
  47. ></script>
  48. <script>
  49. // 使用 IIFE 包裹以避免全局变量污染
  50. (function () {
  51. function getVersion() {
  52. const markdownElement = document.querySelector("markdown-html");
  53. if (markdownElement) {
  54. const version = markdownElement.getAttribute("version");
  55. return version;
  56. } else {
  57. return "未知";
  58. }
  59. }
  60. function getAbout() {
  61. return `
  62. # 关于
  63. ## MARKDOWN.HTML
  64. 版本:${getVersion()}
  65. 旨在提供低依赖、一键部署的 Markdown 网页渲染方案。
  66. ### 特性
  67. - **依赖少**:仅需一个 HTML 文件,即可完成 Markdown 网页渲染。
  68. - **Markdown 支持**:将 Markdown 文件渲染为 HTML。
  69. - **Mermaid 支持**:将 Markdown 中的 Mermaid 语法渲染为图表。
  70. - **多语言支持**:支持多种语言间的机器互译。
  71. - **深色模式**:支持浅色、深色、跟随系统。
  72. - **Pjax 支持**:支持无刷新页面跳转。
  73. 详情请访问 [GitHub](https://github.com/PJ-568/MARKDOWN.HTML) 或 [Gitee](https://gitee.com/PJ-568/MARKDOWN.HTML) 仓库。
  74. `;
  75. }
  76. // 加载动画相关
  77. var loadingState = {
  78. countLoading: 0,
  79. };
  80. Object.defineProperty(loadingState, "countLoading", {
  81. get: function () {
  82. return this._countLoading;
  83. },
  84. set: function (newValue) {
  85. if (newValue < 0) {
  86. newValue = 0;
  87. }
  88. const loadingBar = document.querySelector(".loading-bar");
  89. const progress = document.querySelector(".loading-bar .progress");
  90. if (this.countLoading === 0 && newValue > 0) {
  91. var loadingBarWidth = 20;
  92. var MAX_LOADING_WIDTH = 95;
  93. loadingBar.classList.add("loading");
  94. progress.style.width = loadingBarWidth + "%";
  95. clearInterval(timer);
  96. timer = setInterval(function () {
  97. loadingBarWidth += 3;
  98. if (loadingBarWidth > MAX_LOADING_WIDTH) {
  99. loadingBarWidth = MAX_LOADING_WIDTH;
  100. }
  101. progress.style.width = loadingBarWidth + "%";
  102. }, 500);
  103. } else if (this.countLoading > 0 && newValue === 0) {
  104. clearInterval(timer);
  105. progress.style.width = "100%";
  106. loadingBar.classList.remove("loading");
  107. setTimeout(function () {
  108. progress.style.width = 0;
  109. }, 400);
  110. }
  111. this._countLoading = newValue;
  112. },
  113. });
  114. loadingState.countLoading = 0;
  115. function startLoad() {
  116. loadingState.countLoading = loadingState.countLoading + 1;
  117. }
  118. function endLoad() {
  119. loadingState.countLoading = loadingState.countLoading - 1;
  120. }
  121. var timer = null;
  122. // 文档解析功能相关
  123. var notExistIndexPage = null; // 是否不存在索引页
  124. var pValue = null;
  125. async function getPValue() {
  126. if (pValue) {
  127. return pValue;
  128. } else {
  129. return await decodeMDPath(window.location.href);
  130. }
  131. }
  132. // PJAX 相关
  133. let PJAX;
  134. // 深色模式相关
  135. let darkmode;
  136. // 自定义 PJAX 相关
  137. const existPushState = history && history.pushState;
  138. var level = 1; // 当前页面的层级
  139. var lastLevel = 0;
  140. // 初始化 PJAX
  141. function initPJAX() {
  142. try {
  143. const Pjax =
  144. window.Pjax ||
  145. function () {
  146. console.warn("Pjax 不存在?");
  147. };
  148. PJAX = new Pjax({
  149. selectors: ["head title", ".markdown-body", ".pjax-reload"],
  150. switches: {
  151. ".markdown-body": Pjax.switches.sideBySide,
  152. },
  153. switchesOptions: {
  154. ".markdown-body": {
  155. classNames: {
  156. remove:
  157. "Animated Animated-remove Animated--reverse Animate--fast Animate--noDelay",
  158. add: "Animated",
  159. backward: "Animate--slideInRight",
  160. forward: "Animate--slideInLeft",
  161. },
  162. },
  163. },
  164. cacheBust: false,
  165. });
  166. } catch (e) {
  167. console.error("PJAX 初始化出错:" + e);
  168. }
  169. }
  170. // translate-js 翻译系统
  171. //// 初始化翻译系统
  172. function initTranslate() {
  173. try {
  174. translate.service.use("client.edge");
  175. translate.request.listener.start();
  176. translate.setAutoDiscriminateLocalLanguage();
  177. translate.language.setUrlParamControl();
  178. translate.ignore.class.push("notTranslate");
  179. translate.execute();
  180. } catch (e) {
  181. console.error("翻译系统出错:" + e);
  182. }
  183. }
  184. // Darkmode.js 深色模式
  185. function initDarkmode() {
  186. try {
  187. const Darkmode =
  188. window.Darkmode ||
  189. function () {
  190. console.warn("Darkmode.js 不存在?");
  191. };
  192. darkmode = new Darkmode();
  193. } catch (e) {
  194. console.error("初始化 Darkmode.js 出错:" + e);
  195. }
  196. }
  197. //// 检查并提示翻译
  198. function infoTranslate() {
  199. try {
  200. const currentLang = translate.language.getCurrent();
  201. const originalLang = translate.language.getLocal();
  202. if (currentLang !== originalLang) {
  203. const translateInfo =
  204. document.querySelectorAll(".translate-info");
  205. if (translateInfo) {
  206. translateInfo.forEach(function (item) {
  207. item.remove();
  208. });
  209. }
  210. // 获取 .markdown-body 元素
  211. var bq = document.createElement("blockquote");
  212. bq.classList.add("translate-info");
  213. var infoBody = document.createElement("p");
  214. infoBody.innerText = "当前页面已翻译。您可以:";
  215. var msgBody = document.createElement("p");
  216. var switchButton = document.createElement("a");
  217. switchButton.classList.add("translate-switch");
  218. switchButton.href = "javascript:;";
  219. switchButton.title = "切换到原语言";
  220. switchButton.innerText = "切换到文章原本的语言";
  221. bq.appendChild(infoBody);
  222. bq.appendChild(switchButton);
  223. const mainElement = document.querySelector(
  224. ".markdown-body .content"
  225. );
  226. mainElement.insertBefore(bq, mainElement.children[1]);
  227. }
  228. } catch (e) {
  229. console.error("检查并提示翻译出错:" + e);
  230. }
  231. }
  232. //// 刷新翻译
  233. function refreshTranslate() {
  234. try {
  235. translate.selectLanguageTag.refreshRender();
  236. } catch (e) {
  237. console.error("刷新翻译出错:" + e);
  238. }
  239. }
  240. // 获取文件所在目录
  241. function getDirectory(path) {
  242. path = new URL(path, "http://example.com").pathname;
  243. const lastSlashIndex = path.lastIndexOf("/");
  244. return path.substring(0, lastSlashIndex + 1);
  245. }
  246. // 获取 markdown.html 当前所加载的 md 文件的所在路径
  247. function getPath() {
  248. return getDirectory(window.location.href);
  249. }
  250. // 将 ?p= 后面的值转换为绝对路径
  251. /*
  252. 输出的值是 /path/to/file.md
  253. 当其为 null 时,根据是否存在 index.md 文件来决定返回的值
  254. */
  255. async function decodeMDPath(path) {
  256. const url = new URL(path);
  257. const params = new URLSearchParams(url.search);
  258. path = formatMDPath(params.get("p"));
  259. return path;
  260. }
  261. // 格式化路径
  262. /*
  263. 如果 mdPath 为 null,则返回 null,如果 mdPath 是路径,则返回该路径下 /index.md
  264. return: 返回格式化后的文件路径,一定是绝对路径,或者 null
  265. */
  266. function formatMDPath(mdPath) {
  267. if (mdPath) {
  268. if (isRelativePath(mdPath)) {
  269. mdPath = getPath() + mdPath;
  270. }
  271. mdPath = new URL(mdPath, "http://example.com").pathname;
  272. if (!mdPath.endsWith(".md")) {
  273. if (!mdPath.endsWith("/")) {
  274. mdPath += "/";
  275. }
  276. mdPath += "index.md";
  277. }
  278. }
  279. return mdPath;
  280. }
  281. // 判断链接是否为外链
  282. function isExternalLink(url) {
  283. if (!url || typeof url !== "string") return false;
  284. //// 排除特殊协议
  285. const excludedProtocols = [
  286. "#",
  287. "javascript:",
  288. "mailto:",
  289. "tel:",
  290. "data:",
  291. ];
  292. if (excludedProtocols.some((proto) => url.startsWith(proto))) {
  293. return false;
  294. }
  295. try {
  296. const currentUrl = new URL(window.location.href);
  297. const targetUrl = new URL(url, currentUrl.origin);
  298. //// 排除非 HTTP(S) 协议
  299. if (!["http:", "https:"].includes(targetUrl.protocol)) return false;
  300. //// 域名统一转小写比较
  301. const currentHost = currentUrl.hostname.toLowerCase();
  302. const targetHost = targetUrl.hostname.toLowerCase();
  303. return currentHost !== targetHost;
  304. } catch (e) {
  305. return false;
  306. }
  307. }
  308. // 判断路径是否为相对路径
  309. function isRelativePath(path) {
  310. return !path.match(/^\//); //// 不以 / 开头
  311. }
  312. // 处理错误
  313. function updateContent(article, title = null, mdPath = pValue) {
  314. try {
  315. document.querySelector(
  316. ".markdown-body"
  317. ).innerHTML = `<div class="old-content Animated Animated-remove Animated--reverse Animate--fast Animate--noDelay Animate--slideInLeft">${
  318. document.querySelector(".markdown-body .content").innerHTML
  319. }</div><div class="content Animated Animate--slideInRight">${article}</div>`;
  320. firstH1 = document.querySelector(".markdown-body .content h1");
  321. document.title = title
  322. ? title
  323. : firstH1
  324. ? firstH1.textContent
  325. : mdPath;
  326. } catch (error) {
  327. throw error;
  328. }
  329. }
  330. // 加载 CSS
  331. function loadCSS(url) {
  332. return new Promise((resolve, reject) => {
  333. //// 检查是否已加载
  334. const existingLinks = document.querySelectorAll(
  335. `link[href="${url}"]`
  336. );
  337. if (existingLinks.length > 0) {
  338. return resolve(url);
  339. }
  340. //// 创建新的 link 元素
  341. const link = document.createElement("link");
  342. link.rel = "stylesheet";
  343. link.type = "text/css";
  344. link.href = url;
  345. //// 成功加载处理
  346. link.onload = () => {
  347. link.onload = null;
  348. link.onerror = null;
  349. resolve(url);
  350. };
  351. //// 加载失败处理
  352. link.onerror = () => {
  353. link.onerror = null;
  354. link.onload = null;
  355. document.head.removeChild(link);
  356. reject(new Error(`无法加载CSS文件: ${url}`));
  357. };
  358. //// 添加到文档头部
  359. document.head.appendChild(link);
  360. });
  361. }
  362. // 等待动画结束
  363. function waitForAnimationsEnd(parentElement, selector, animationNames) {
  364. return new Promise((resolve) => {
  365. var completedCount = 0;
  366. const elements = parentElement.querySelectorAll(selector);
  367. const totalElements = elements.length;
  368. const handleAnimationEnd = function (event) {
  369. if (
  370. animationNames.includes(event.animationName) &&
  371. Array.from(elements).includes(event.target)
  372. ) {
  373. completedCount++;
  374. if (completedCount === totalElements) {
  375. parentElement.removeEventListener(
  376. "animationend",
  377. handleAnimationEnd
  378. );
  379. resolve();
  380. }
  381. }
  382. };
  383. parentElement.addEventListener("animationend", handleAnimationEnd);
  384. });
  385. }
  386. // 处理错误
  387. function handleError(mdPath, errorMessage) {
  388. updateContent(
  389. `<h1>加载失败</h1><p>加载文档<code>${mdPath}</code>时出错:<code>${errorMessage}</code>。您可以尝试<a class="refresh-btn" href="">重新加载</a>或<a class="back-btn">返回</a>。</p>`,
  390. "加载失败"
  391. );
  392. waitForAnimationsEnd(
  393. document.querySelector(".markdown-body"),
  394. ".content, .old-content",
  395. ["Animation-slideInRight", "Animation-slideInLeft"]
  396. ).then(() => {
  397. customPushState(
  398. window.location.origin +
  399. window.location.pathname +
  400. (mdPath ? "?p=" + mdPath : "")
  401. );
  402. });
  403. endLoad();
  404. }
  405. // 更新页面的 markdown 内容
  406. /*
  407. mdPath: 要加载的 markdown 文件路径,最好是带主机名的绝对路径(要用于 fetch())
  408. response: fetch() 的返回值,用于防止二次访问
  409. */
  410. const showMarkdown = async (mdPath, response = null) => {
  411. if (mdPath === null) {
  412. var path = null;
  413. const currentPath = getPath();
  414. // 如果存在 index.md 文件,则返回 index.md,否则返回 README.md
  415. if (notExistIndexPage) {
  416. path = currentPath + "README.md";
  417. } else if (notExistIndexPage === false) {
  418. path = currentPath + "index.md";
  419. } else {
  420. response = await fetch(getPath() + "index.md");
  421. if (
  422. response.ok &&
  423. response.headers.get("content-type").startsWith("text/markdown")
  424. ) {
  425. notExistIndexPage = false;
  426. path = currentPath + "index.md";
  427. } else {
  428. response = null;
  429. notExistIndexPage = true;
  430. path = currentPath + "README.md";
  431. }
  432. }
  433. } else if (mdPath.endsWith("about.md")) {
  434. // 特殊处理 about.md
  435. response = await fetch(mdPath);
  436. const about = getAbout();
  437. if (
  438. response.ok &&
  439. response.headers.get("content-type").startsWith("text/markdown")
  440. ) {
  441. var r_text = await response.text();
  442. const content = about + (await r_text.replace(/^# .*/m, ""));
  443. response = {
  444. ok: true,
  445. status: 200,
  446. statusText: "自生成。",
  447. text: async () => {
  448. return content;
  449. },
  450. };
  451. } else {
  452. response = {
  453. ok: true,
  454. status: 200,
  455. statusText: "自生成。",
  456. text: async () => {
  457. return about + "*无更多详细信息*";
  458. },
  459. };
  460. }
  461. }
  462. try {
  463. pValue = mdPath ? mdPath : path;
  464. updateContent(await getMarkdown(mdPath ? mdPath : path, response));
  465. document.querySelector("html").classList.add("loaded"); //// 防止缓存的页面被自定义 PJAX 中覆写的处理响应的函数捕获
  466. waitForAnimationsEnd(
  467. document.querySelector(".markdown-body"),
  468. ".content, .old-content",
  469. ["Animation-slideInRight", "Animation-slideInLeft"]
  470. ).then(async () => {
  471. await showMarkdownEnded();
  472. customPushState(
  473. window.location.origin +
  474. window.location.pathname +
  475. (mdPath ? "?p=" + mdPath : "")
  476. );
  477. });
  478. } catch (error) {
  479. handleError(mdPath ? mdPath : path, error);
  480. }
  481. };
  482. // 读取并解析指定文件
  483. const getMarkdown = async (mdPath, response = null) => {
  484. //// 生成目录
  485. function generateOutline(doc) {
  486. const headings = Array.from(
  487. doc.querySelectorAll("h1, h2, h3, h4, h5, h6")
  488. );
  489. if (headings.length === 0) return "<i>无目录</i>";
  490. var outline = "<ul>";
  491. var stack = [];
  492. headings.forEach((heading) => {
  493. const level = parseInt(heading.tagName.charAt(1));
  494. const id = heading.textContent
  495. .toLowerCase()
  496. .replace(/ /g, "-") //// 将空格替换为连字符
  497. .replace(/[^\w\u4e00-\u9fa5-]/g, ""); //// 保留字母、数字、下划线、中文字符和连字符
  498. //// 设置 id 属性以便链接跳转
  499. heading.setAttribute("id", id);
  500. while (
  501. stack.length > 0 &&
  502. stack[stack.length - 1].level >= level
  503. ) {
  504. stack.pop();
  505. outline += "</ul></li>";
  506. }
  507. if (stack.length === 0 || stack[stack.length - 1].level < level) {
  508. outline += "<li><ul>";
  509. } else {
  510. outline += "</li><li>";
  511. }
  512. outline += `<a href="#${id}">${heading.textContent}</a>`;
  513. stack.push({ level: level });
  514. });
  515. while (stack.length > 0) {
  516. outline += "</ul></li>";
  517. stack.pop();
  518. }
  519. outline += "</ul>";
  520. return outline;
  521. }
  522. if (!response) {
  523. response = await fetch(mdPath);
  524. if (
  525. !response.ok &&
  526. response.headers.get("content-type").startsWith("text/markdown")
  527. ) {
  528. throw new Error(
  529. "请求失败:" +
  530. mdPath +
  531. "," +
  532. response.headers.get("content-type") +
  533. "," +
  534. response.status +
  535. "," +
  536. response.statusText
  537. );
  538. }
  539. }
  540. var markdown_content = await response.text();
  541. var content = marked.parse(markdown_content);
  542. //// 因为是在本页面渲染一个其它页面的文档:
  543. //// - 从 content 中获取所有 href 以 .md 结尾的 a 标签;
  544. //// - 遍历这些 a 标签,如果 href 不是外链,则判断 href 是否为相对路径……最后转换为 ?p=<到该文件的绝对路径>
  545. //// - 从 content 中获取所有 img 标签;
  546. //// - 遍历这些 img 标签,如果 src 不是外链,则判断 src 是否为相对路径……最后转换为 <到该文件的绝对路径>
  547. const linkParser = new DOMParser();
  548. const linkDoc = linkParser.parseFromString(
  549. `<div>${content}</div>`,
  550. "text/html"
  551. );
  552. //// 处理所有以 .md 结尾的链接
  553. const mdLinks = linkDoc.querySelectorAll('a[href$=".md"]');
  554. mdLinks.forEach((link) => {
  555. const href = link.getAttribute("href");
  556. if (!isExternalLink(href) && isRelativePath(href)) {
  557. var absolutePath = pValue
  558. ? getDirectory(mdPath) + href
  559. : getPath() + href;
  560. absolutePath = new URL(absolutePath, "http://example.com")
  561. .pathname;
  562. link.setAttribute("href", `?p=${absolutePath}`);
  563. }
  564. });
  565. //// 处理所有图片
  566. const images = linkDoc.querySelectorAll("img");
  567. images.forEach((img) => {
  568. const src = img.getAttribute("src");
  569. img.setAttribute("loading", "lazy");
  570. if (src && !isExternalLink(src) && isRelativePath(src)) {
  571. var absolutePath = pValue
  572. ? getDirectory(mdPath) + src
  573. : getPath() + src;
  574. absolutePath = new URL(absolutePath, "http://example.com")
  575. .pathname;
  576. img.setAttribute("src", absolutePath);
  577. }
  578. });
  579. //// 更新处理后的 markdown 内容
  580. content = linkDoc.body.innerHTML.replace(/^<div>|<\/div>$/g, "");
  581. //// 空文件
  582. if (content === "") {
  583. content = "<i>空文件</i>";
  584. }
  585. //// 添加目录
  586. content = `<details class="outline"><summary>${decodeURIComponent(
  587. mdPath
  588. )}</summary></details><hr>${content}`;
  589. const parser = new DOMParser();
  590. const doc = parser.parseFromString(content, "text/html");
  591. const outline = generateOutline(doc);
  592. doc.querySelector(".outline").innerHTML += outline;
  593. return doc.body.innerHTML;
  594. };
  595. // 初始化自定义 PJAX 响应
  596. function initCustomPJAXResponse() {
  597. //// 覆写 PJAX 处理响应的函数,在内容插入页面之前实现 markdown 的渲染
  598. PJAX._handleResponse = PJAX.handleResponse;
  599. PJAX.handleResponse = async function (
  600. responseText,
  601. request,
  602. href,
  603. options
  604. ) {
  605. if (
  606. /^<!doctype html>[\s\S]*?<html>[\s\S]*?<markdown-html/i.test(
  607. request.responseText
  608. )
  609. ) {
  610. await showMarkdown(await decodeMDPath(href));
  611. } else {
  612. PJAX._handleResponse(responseText, request, href, options);
  613. }
  614. };
  615. }
  616. // 初始化自定义 PJAX 事件监听器
  617. function initCustomPJAXEventListener() {
  618. // 监听自定义链接的点击事件
  619. if (existPushState) {
  620. document
  621. .querySelector(".markdown-body")
  622. .addEventListener("click", async function (event) {
  623. const link = event.target.closest('a[href$=".md"]');
  624. if (link) {
  625. try {
  626. event.preventDefault();
  627. startLoad();
  628. await showMarkdown(await decodeMDPath(link.href));
  629. } catch (e) {
  630. console.error("自定义 PJAX 出错:" + e);
  631. }
  632. }
  633. });
  634. }
  635. }
  636. // 自定义推送状态
  637. function customPushState(url = window.location.href) {
  638. if (existPushState) {
  639. if (level > lastLevel) {
  640. history.pushState(
  641. { url: url, title: document.title, uid: level },
  642. null,
  643. url
  644. );
  645. lastLevel = level++;
  646. } else {
  647. lastLevel = level - 1;
  648. }
  649. }
  650. }
  651. // 初始化内容
  652. const initContent = async () => {
  653. try {
  654. await showMarkdown(await getPValue());
  655. } catch (error) {
  656. handleError(await getPValue(), "初始化内容时出错:" + error);
  657. }
  658. };
  659. // 初始化返回首页按钮
  660. function initHomeBtn() {
  661. const homeBtn = document.querySelector(".to-home");
  662. homeBtn.href = window.location.pathname;
  663. }
  664. // 初始化返回首页按钮
  665. function initAboutBtn() {
  666. const aboutBtn = document.querySelector(".about-btn");
  667. aboutBtn.href = `?p=${getDirectory(
  668. window.location.pathname
  669. )}about.md`;
  670. }
  671. // 初始化
  672. function initialize() {
  673. startLoad(); //// 开始加载动画
  674. initDarkmode(); //// 初始化深色模式
  675. initHomeBtn(); //// 初始化返回首页按钮
  676. initAboutBtn(); //// 初始化关于按钮
  677. initContent(); //// 初始化内容
  678. initTranslate(); //// 初始化页面翻译
  679. initPJAX(); //// 初始化 PJAX
  680. initCustomPJAXResponse(); //// 初始化自定义 PJAX 响应
  681. initCustomPJAXEventListener(); //// 初始化自定义 PJAX 事件监听器
  682. loadCSS(
  683. "https://chinese-fonts-cdn.deno.dev/packages/maple-mono-cn/dist/MapleMono-CN-Regular/result.css"
  684. ); //// 加载字体 CSS
  685. endLoad(); //// 结束加载动画
  686. }
  687. // 触发器
  688. //// 网页加载完毕后触发初始化
  689. window.addEventListener("DOMContentLoaded", () => initialize());
  690. //// 翻译执行完成后触发
  691. translate.listener.renderTaskFinish = function (_task) {
  692. endLoad();
  693. };
  694. //// PJAX 开始时执行的函数
  695. document.addEventListener("pjax:send", function () {
  696. startLoad();
  697. });
  698. //// 监听 PJAX 完成后,重新加载
  699. document.addEventListener("pjax:complete", async function () {
  700. await showMarkdown(await getPValue());
  701. initCustomPJAXEventListener(); //// 重新初始化自定义 PJAX 事件监听器
  702. });
  703. //// 监听 animationend 事件
  704. document.addEventListener(
  705. "animationend",
  706. function handleAnimationEnd(event) {
  707. if (event.animationName === "Animation-slideInRight") {
  708. const element = document.querySelector(".markdown-body .content");
  709. element.classList.remove("Animated");
  710. element.classList.remove("Animate--slideInRight");
  711. } else if (event.animationName === "Animation-slideInLeft") {
  712. document.querySelector(".markdown-body .old-content").remove();
  713. }
  714. }
  715. );
  716. //// 点击事件委托
  717. document.addEventListener("click", function (event) {
  718. if (event.target.closest(".back-btn")) {
  719. window.history.back();
  720. } else if (event.target.closest(".refresh-btn")) {
  721. window.location.reload();
  722. } else if (event.target.closest(".dark-btn")) {
  723. darkmode.toggle();
  724. } else if (event.target.closest(".translate-switch")) {
  725. translate.changeLanguage(translate.language.getLocal());
  726. /* 跳转。禁用原因是有些带有特殊字符的链接会出错或无法跳转
  727. } else if (event.target.closest('.content a[href^="#"]')) {
  728. // 目录平滑移动
  729. const href = event.target.getAttribute('href');
  730. if (href !== '#') {
  731. event.preventDefault();
  732. const targetElement = document.querySelector(href);
  733. if (targetElement) {
  734. targetElement.scrollIntoView({
  735. behavior: 'smooth'
  736. });
  737. } else {
  738. console.error(`ID 为 ${targetId} 的元素不存在。`);
  739. }
  740. }
  741. */
  742. } else if (event.target.closest('.content a[href]:not([href=""])')) {
  743. const href = event.target.getAttribute("href");
  744. if (isExternalLink(href)) {
  745. window.open(href, "_blank");
  746. event.preventDefault();
  747. }
  748. } else if (event.target.closest(".content img, .content svg")) {
  749. const url =
  750. event.target.getAttribute("src") ||
  751. event.target.getAttribute("xlink:href");
  752. if (url) {
  753. window.open(url, "_blank");
  754. event.preventDefault();
  755. } else {
  756. const newWindow = window.open("", "_blank");
  757. newWindow.document.write(imgOrSvg.outerHTML);
  758. newWindow.document.close();
  759. event.preventDefault();
  760. }
  761. }
  762. });
  763. //// 文档内容加载完毕时触发
  764. async function showMarkdownEnded() {
  765. refreshTranslate();
  766. infoTranslate(); //// 检查并提示翻译
  767. await mermaid.init({ noteMargin: 10 }, ".language-mermaid"); //// 初始化 mermaid
  768. translate.execute(); //// 手动触发翻译,但是理论上 translate-js 会监听页面变化
  769. hljs.highlightAll(); //// 触发代码块语法高亮
  770. endLoad();
  771. window.scrollTo({
  772. top: 0,
  773. behavior: "smooth",
  774. });
  775. }
  776. //// 浏览器返回时触发
  777. window.onpopstate = function (event) {
  778. if (level > 1) {
  779. --level;
  780. }
  781. };
  782. })();
  783. </script>
  784. <title>加载中</title>
  785. <style>
  786. /* 全局 */
  787. :root,
  788. html,
  789. body,
  790. button,
  791. input,
  792. select,
  793. textarea,
  794. code,
  795. pre {
  796. font-family: "Sarasa Term SC Nerd", "Maple Mono CN", Ubuntu, Roboto,
  797. "Open Sans", "Microsoft YaHei", Arial, sans-serif;
  798. text-decoration: none;
  799. }
  800. :root,
  801. html,
  802. body {
  803. background-color: #e6e6e6;
  804. }
  805. ::selection {
  806. background-color: #dcedc8;
  807. }
  808. body {
  809. margin: 0;
  810. }
  811. a {
  812. color: #66bb6a;
  813. cursor: pointer;
  814. text-decoration: none;
  815. }
  816. a:hover {
  817. color: #2e7d32;
  818. }
  819. /* 背景 */
  820. .background {
  821. position: fixed;
  822. left: 0;
  823. top: 0;
  824. height: 100%;
  825. width: 100%;
  826. z-index: -1;
  827. background-color: #e6e6e6;
  828. }
  829. /* 导航栏 */
  830. nav {
  831. position: fixed;
  832. top: 0;
  833. right: 0;
  834. width: min-content;
  835. background-color: #616161;
  836. display: flex;
  837. justify-content: space-between;
  838. align-items: center;
  839. z-index: 1;
  840. }
  841. nav a {
  842. color: #fafafa;
  843. padding: 0.3rem;
  844. transition: color 0.2s, background-color 0.2s;
  845. }
  846. nav a:hover {
  847. color: #fff;
  848. background-color: #212121;
  849. }
  850. /* 动画 */
  851. .Animated {
  852. animation-fill-mode: both;
  853. animation-duration: 1s;
  854. }
  855. .Animated-remove {
  856. position: absolute;
  857. }
  858. .Animated--reverse {
  859. animation-direction: reverse;
  860. }
  861. .Animate--fast {
  862. animation-duration: 0.5s;
  863. }
  864. .Animate--noDelay {
  865. animation-delay: 0s !important;
  866. }
  867. .Animate--slideInRight {
  868. animation-name: Animation-slideInRight;
  869. }
  870. @keyframes Animation-slideInRight {
  871. 0% {
  872. transform: translateX(200%);
  873. }
  874. 100% {
  875. transform: translateX(0);
  876. }
  877. }
  878. .Animate--slideInLeft {
  879. animation-name: Animation-slideInLeft;
  880. }
  881. @keyframes Animation-slideInLeft {
  882. 0% {
  883. transform: translateX(-100%);
  884. }
  885. 100% {
  886. transform: translateX(0);
  887. }
  888. }
  889. /* 主要内容 */
  890. .markdown-body {
  891. position: relative;
  892. margin: 3rem auto;
  893. width: 100%;
  894. max-width: 950px;
  895. min-height: 300px;
  896. background-color: #fafafa;
  897. color: #212121;
  898. box-sizing: border-box;
  899. overflow-x: hidden;
  900. box-shadow: 0 2px 6px rgba(100, 100, 100, 0.3);
  901. }
  902. .markdown-body .content,
  903. .markdown-body .old-content {
  904. margin: 2rem;
  905. }
  906. .markdown-body img,
  907. .markdown-body video,
  908. .markdown-body iframe,
  909. .markdown-body object,
  910. .markdown-body embed,
  911. .markdown-body svg,
  912. .markdown-body canvas,
  913. .markdown-body audio {
  914. max-width: 100%;
  915. background-color: rgba(0, 0, 0, 0.05);
  916. }
  917. /* Markdown 样式 */
  918. .markdown-body a {
  919. transition: color 0.2s, background-color 0.2s;
  920. }
  921. .markdown-body a:hover {
  922. background-color: rgba(255, 255, 255, 0.1);
  923. }
  924. .markdown-body ul,
  925. .markdown-body ol {
  926. padding-left: 2rem;
  927. }
  928. .markdown-body pre {
  929. background-color: rgba(0, 0, 0, 0.05);
  930. padding: 0.4rem;
  931. border-radius: 0.1rem;
  932. overflow-y: scroll;
  933. }
  934. .markdown-body code {
  935. font-family: "cousine nerd font mono", monospace;
  936. background-color: rgba(0, 0, 0, 0.05);
  937. font-size: small;
  938. padding: 0.2rem;
  939. border-radius: 0.1rem;
  940. }
  941. .markdown-body pre code {
  942. background-color: unset;
  943. font-size: unset;
  944. padding: unset;
  945. border-radius: unset;
  946. }
  947. .markdown-body blockquote {
  948. background-color: rgba(0, 0, 0, 0.05);
  949. font-size: small;
  950. padding: 0.4rem;
  951. margin: 0.5rem 2rem;
  952. border-left: 5px solid rgba(0, 0, 0, 0.1);
  953. }
  954. .markdown-body details {
  955. border: 1px solid rgba(0, 0, 0, 0.1);
  956. border-radius: 0.1rem;
  957. }
  958. .markdown-body summary {
  959. font-size: small;
  960. cursor: pointer;
  961. background-color: rgba(0, 0, 0, 0.05);
  962. padding: 0.4rem;
  963. font-weight: bold;
  964. }
  965. .markdown-body details a {
  966. margin-left: -2rem;
  967. }
  968. .markdown-body table {
  969. border-spacing: 0;
  970. border-radius: 0.1rem;
  971. width: 100%;
  972. border-collapse: separate;
  973. border: 0.1rem solid rgba(0, 0, 0, 0.05);
  974. }
  975. .markdown-body table > * {
  976. overflow-y: scroll;
  977. }
  978. .markdown-body table td,
  979. .markdown-body table th {
  980. border-right: 0.1rem solid rgba(0, 0, 0, 0.05);
  981. border-top: 0.1rem solid rgba(0, 0, 0, 0.05);
  982. padding: 0.4rem;
  983. }
  984. .markdown-body table td:last-child,
  985. .markdown-body th:last-child {
  986. border-right-color: transparent;
  987. }
  988. .markdown-body table thead tr:first-child > * {
  989. border-top: 0.1rem solid transparent;
  990. }
  991. /* 加载进度条 */
  992. .loading-bar {
  993. position: fixed;
  994. top: 0;
  995. left: 0;
  996. z-index: 2;
  997. opacity: 0;
  998. transition: opacity 0.4s linear;
  999. .progress {
  1000. position: fixed;
  1001. top: 0;
  1002. left: 0;
  1003. width: 0;
  1004. height: 4px;
  1005. background-color: #2e7d32;
  1006. box-shadow: 0 0 10px rgba(102, 187, 106, 0.7);
  1007. }
  1008. &.loading {
  1009. opacity: 1;
  1010. transition: none;
  1011. .progress {
  1012. transition: width 0.4s ease;
  1013. }
  1014. }
  1015. }
  1016. /* 翻译按钮相关 */
  1017. .translateSelectLanguage {
  1018. background-color: #616161;
  1019. color: inherit;
  1020. z-index: 3;
  1021. width: 17%;
  1022. height: 100%;
  1023. opacity: 0;
  1024. cursor: pointer;
  1025. position: absolute;
  1026. top: 0;
  1027. left: 50%;
  1028. }
  1029. /* 深色模式相关 */
  1030. .darkmode--activated .markdown-body img,
  1031. .darkmode--activated .markdown-body svg {
  1032. isolation: isolate;
  1033. mix-blend-mode: difference;
  1034. }
  1035. .darkmode-layer,
  1036. .darkmode-toggle {
  1037. z-index: 3;
  1038. }
  1039. /* 小屏响应式优化 */
  1040. @media only screen and (max-width: 950px) {
  1041. html,
  1042. body,
  1043. .markdown-body {
  1044. height: 100%;
  1045. }
  1046. .markdown-body {
  1047. overflow-y: scroll;
  1048. min-width: 256px;
  1049. top: 0;
  1050. margin: 0 0;
  1051. }
  1052. }
  1053. </style>
  1054. <link
  1055. href="https://lf26-cdn-tos.bytecdntp.com/cdn/expire-1-y/highlight.js/11.4.0/styles/default.min.css"
  1056. type="text/css"
  1057. rel="stylesheet"
  1058. />
  1059. </head>
  1060. <body>
  1061. <!-- 导航栏 -->
  1062. <nav>
  1063. <a title="返回" class="back-btn">
  1064. <svg
  1065. xmlns="http://www.w3.org/2000/svg"
  1066. width="24"
  1067. height="24"
  1068. viewBox="0 0 24 24"
  1069. fill="none"
  1070. stroke="currentColor"
  1071. stroke-width="2"
  1072. stroke-linecap="round"
  1073. stroke-linejoin="round"
  1074. class="icon icon-tabler icons-tabler-outline icon-tabler-arrow-back"
  1075. >
  1076. <path stroke="none" d="M0 0h24v24H0z" fill="none" />
  1077. <path d="M9 11l-4 4l4 4m-4 -4h11a4 4 0 0 0 0 -8h-1" />
  1078. </svg>
  1079. </a>
  1080. <a title="刷新" class="refresh-btn" href="">
  1081. <svg
  1082. xmlns="http://www.w3.org/2000/svg"
  1083. width="24"
  1084. height="24"
  1085. viewBox="0 0 24 24"
  1086. fill="none"
  1087. stroke="currentColor"
  1088. stroke-width="2"
  1089. stroke-linecap="round"
  1090. stroke-linejoin="round"
  1091. class="icon icon-tabler icons-tabler-outline icon-tabler-refresh"
  1092. >
  1093. <path stroke="none" d="M0 0h24v24H0z" fill="none" />
  1094. <path d="M20 11a8.1 8.1 0 0 0 -15.5 -2m-.5 -4v4h4" />
  1095. <path d="M4 13a8.1 8.1 0 0 0 15.5 2m.5 4v-4h-4" />
  1096. </svg>
  1097. </a>
  1098. <a title="主页" class="to-home">
  1099. <svg
  1100. xmlns="http://www.w3.org/2000/svg"
  1101. width="24"
  1102. height="24"
  1103. viewBox="0 0 24 24"
  1104. fill="none"
  1105. stroke="currentColor"
  1106. stroke-width="2"
  1107. stroke-linecap="round"
  1108. stroke-linejoin="round"
  1109. class="icon icon-tabler icons-tabler-outline icon-tabler-home"
  1110. >
  1111. <path stroke="none" d="M0 0h24v24H0z" fill="none" />
  1112. <path d="M5 12l-2 0l9 -9l9 9l-2 0" />
  1113. <path d="M5 12v7a2 2 0 0 0 2 2h10a2 2 0 0 0 2 -2v-7" />
  1114. <path d="M9 21v-6a2 2 0 0 1 2 -2h2a2 2 0 0 1 2 2v6" />
  1115. </svg>
  1116. </a>
  1117. <a title="更改语言" id="translate">
  1118. <svg
  1119. xmlns="http://www.w3.org/2000/svg"
  1120. width="24"
  1121. height="24"
  1122. viewBox="0 0 24 24"
  1123. fill="none"
  1124. stroke="currentColor"
  1125. stroke-width="2"
  1126. stroke-linecap="round"
  1127. stroke-linejoin="round"
  1128. class="icon icon-tabler icons-tabler-outline icon-tabler-language"
  1129. >
  1130. <path stroke="none" d="M0 0h24v24H0z" fill="none" />
  1131. <path d="M4 5h7" />
  1132. <path d="M9 3v2c0 4.418 -2.239 8 -5 8" />
  1133. <path d="M5 9c0 2.144 2.952 3.908 6.7 4" />
  1134. <path d="M12 20l4 -9l4 9" />
  1135. <path d="M19.1 18h-6.2" />
  1136. </svg>
  1137. </a>
  1138. <a title="切换显示模式" class="dark-btn" href="javascript:;">
  1139. <svg
  1140. xmlns="http://www.w3.org/2000/svg"
  1141. width="24"
  1142. height="24"
  1143. viewBox="0 0 24 24"
  1144. fill="none"
  1145. stroke="currentColor"
  1146. stroke-width="2"
  1147. stroke-linecap="round"
  1148. stroke-linejoin="round"
  1149. class="icon icon-tabler icons-tabler-outline icon-tabler-brightness"
  1150. >
  1151. <path stroke="none" d="M0 0h24v24H0z" fill="none" />
  1152. <path d="M12 12m-9 0a9 9 0 1 0 18 0a9 9 0 1 0 -18 0" />
  1153. <path d="M12 3l0 18" />
  1154. <path d="M12 9l4.65 -4.65" />
  1155. <path d="M12 14.3l7.37 -7.37" />
  1156. <path d="M12 19.6l8.85 -8.85" />
  1157. </svg>
  1158. </a>
  1159. <a title="关于" class="about-btn">
  1160. <svg
  1161. xmlns="http://www.w3.org/2000/svg"
  1162. width="24"
  1163. height="24"
  1164. viewBox="0 0 24 24"
  1165. fill="none"
  1166. stroke="currentColor"
  1167. stroke-width="2"
  1168. stroke-linecap="round"
  1169. stroke-linejoin="round"
  1170. class="icon icon-tabler icons-tabler-outline icon-tabler-info-circle"
  1171. >
  1172. <path stroke="none" d="M0 0h24v24H0z" fill="none" />
  1173. <path d="M3 12a9 9 0 1 0 18 0a9 9 0 0 0 -18 0" />
  1174. <path d="M12 9h.01" />
  1175. <path d="M11 12h1v4h1" />
  1176. </svg>
  1177. </a>
  1178. </nav>
  1179. <!-- 内容主体 -->
  1180. <div class="markdown-body">
  1181. <div class="content">
  1182. <h1>加载中……</h1>
  1183. <p>请稍等。</p>
  1184. </div>
  1185. </div>
  1186. <!-- 加载动画 -->
  1187. <div class="loading-bar">
  1188. <div class="progress"></div>
  1189. </div>
  1190. <div class="background"></div>
  1191. <script
  1192. src="https://lf6-cdn-tos.bytecdntp.com/cdn/expire-1-y/instant.page/5.1.0/instantpage.min.js"
  1193. type="text/javascript"
  1194. ></script>
  1195. </body>
  1196. </html>