1
0

index.html 42 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327
  1. <!DOCTYPE html>
  2. <html>
  3. <markdown-html
  4. version="1.18.9"
  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. //// 处理所有 a 链接
  553. const mdLinks = linkDoc.querySelectorAll('a[href]:not([href=""])');
  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. if (absolutePath.endsWith(".md")) {
  563. link.setAttribute("href", `?p=${absolutePath}`);
  564. } else {
  565. link.setAttribute("href", absolutePath);
  566. }
  567. }
  568. });
  569. //// 处理所有图片
  570. const images = linkDoc.querySelectorAll("img");
  571. images.forEach((img) => {
  572. const src = img.getAttribute("src");
  573. img.setAttribute("loading", "lazy");
  574. if (src && !isExternalLink(src) && isRelativePath(src)) {
  575. var absolutePath = pValue
  576. ? getDirectory(mdPath) + src
  577. : getPath() + src;
  578. absolutePath = new URL(absolutePath, "http://example.com")
  579. .pathname;
  580. img.setAttribute("src", absolutePath);
  581. }
  582. });
  583. //// 更新处理后的 markdown 内容
  584. content = linkDoc.body.innerHTML.replace(/^<div>|<\/div>$/g, "");
  585. //// 空文件
  586. if (content === "") {
  587. content = "<i>空文件</i>";
  588. }
  589. //// 添加目录
  590. content = `<details class="outline"><summary>${decodeURIComponent(
  591. mdPath
  592. )}</summary></details><hr>${content}`;
  593. const parser = new DOMParser();
  594. const doc = parser.parseFromString(content, "text/html");
  595. const outline = generateOutline(doc);
  596. doc.querySelector(".outline").innerHTML += outline;
  597. return doc.body.innerHTML;
  598. };
  599. // 初始化自定义 PJAX 响应
  600. function initCustomPJAXResponse() {
  601. //// 覆写 PJAX 处理响应的函数,在内容插入页面之前实现 markdown 的渲染
  602. PJAX._handleResponse = PJAX.handleResponse;
  603. PJAX.handleResponse = async function (
  604. responseText,
  605. request,
  606. href,
  607. options
  608. ) {
  609. if (
  610. /^<!doctype html>[\s\S]*?<html>[\s\S]*?<markdown-html/i.test(
  611. request.responseText
  612. )
  613. ) {
  614. await showMarkdown(await decodeMDPath(href));
  615. } else {
  616. PJAX._handleResponse(responseText, request, href, options);
  617. }
  618. };
  619. }
  620. // 初始化自定义 PJAX 事件监听器
  621. function initCustomPJAXEventListener() {
  622. // 监听自定义链接的点击事件
  623. if (existPushState) {
  624. document
  625. .querySelector(".markdown-body")
  626. .addEventListener("click", async function (event) {
  627. const link = event.target.closest('a[href$=".md"]');
  628. if (link) {
  629. try {
  630. event.preventDefault();
  631. startLoad();
  632. await showMarkdown(await decodeMDPath(link.href));
  633. } catch (e) {
  634. console.error("自定义 PJAX 出错:" + e);
  635. }
  636. }
  637. });
  638. }
  639. }
  640. // 自定义推送状态
  641. function customPushState(url = window.location.href) {
  642. if (existPushState) {
  643. if (level > lastLevel) {
  644. history.pushState(
  645. { url: url, title: document.title, uid: level },
  646. null,
  647. url
  648. );
  649. lastLevel = level++;
  650. } else {
  651. lastLevel = level - 1;
  652. }
  653. }
  654. }
  655. // 初始化内容
  656. const initContent = async () => {
  657. try {
  658. await showMarkdown(await getPValue());
  659. } catch (error) {
  660. handleError(await getPValue(), "初始化内容时出错:" + error);
  661. }
  662. };
  663. // 初始化返回首页按钮
  664. function initHomeBtn() {
  665. const homeBtn = document.querySelector(".to-home");
  666. homeBtn.href = window.location.pathname;
  667. }
  668. // 初始化返回首页按钮
  669. function initAboutBtn() {
  670. const aboutBtn = document.querySelector(".about-btn");
  671. aboutBtn.href = `?p=${getDirectory(
  672. window.location.pathname
  673. )}about.md`;
  674. }
  675. // 初始化
  676. function initialize() {
  677. startLoad(); //// 开始加载动画
  678. initDarkmode(); //// 初始化深色模式
  679. initHomeBtn(); //// 初始化返回首页按钮
  680. initAboutBtn(); //// 初始化关于按钮
  681. initContent(); //// 初始化内容
  682. initTranslate(); //// 初始化页面翻译
  683. initPJAX(); //// 初始化 PJAX
  684. initCustomPJAXResponse(); //// 初始化自定义 PJAX 响应
  685. initCustomPJAXEventListener(); //// 初始化自定义 PJAX 事件监听器
  686. loadCSS(
  687. "https://chinese-fonts-cdn.deno.dev/packages/maple-mono-cn/dist/MapleMono-CN-Regular/result.css"
  688. ); //// 加载字体 CSS
  689. endLoad(); //// 结束加载动画
  690. }
  691. // 触发器
  692. //// 网页加载完毕后触发初始化
  693. window.addEventListener("DOMContentLoaded", () => initialize());
  694. //// 翻译执行完成后触发
  695. translate.listener.renderTaskFinish = function (_task) {
  696. endLoad();
  697. };
  698. //// PJAX 开始时执行的函数
  699. document.addEventListener("pjax:send", function () {
  700. startLoad();
  701. });
  702. //// 监听 PJAX 完成后,重新加载
  703. document.addEventListener("pjax:complete", async function () {
  704. await showMarkdown(await getPValue());
  705. initCustomPJAXEventListener(); //// 重新初始化自定义 PJAX 事件监听器
  706. });
  707. //// 监听 animationend 事件
  708. document.addEventListener(
  709. "animationend",
  710. function handleAnimationEnd(event) {
  711. if (event.animationName === "Animation-slideInRight") {
  712. const element = document.querySelector(".markdown-body .content");
  713. element.classList.remove("Animated");
  714. element.classList.remove("Animate--slideInRight");
  715. } else if (event.animationName === "Animation-slideInLeft") {
  716. document.querySelector(".markdown-body .old-content").remove();
  717. }
  718. }
  719. );
  720. //// 点击事件委托
  721. document.addEventListener("click", function (event) {
  722. if (event.target.closest(".back-btn")) {
  723. window.history.back();
  724. } else if (event.target.closest(".refresh-btn")) {
  725. window.location.reload();
  726. } else if (event.target.closest(".dark-btn")) {
  727. darkmode.toggle();
  728. } else if (event.target.closest(".translate-switch")) {
  729. translate.changeLanguage(translate.language.getLocal());
  730. /* 跳转。禁用原因是有些带有特殊字符的链接会出错或无法跳转
  731. } else if (event.target.closest('.content a[href^="#"]')) {
  732. // 目录平滑移动
  733. const href = event.target.getAttribute('href');
  734. if (href !== '#') {
  735. event.preventDefault();
  736. const targetElement = document.querySelector(href);
  737. if (targetElement) {
  738. targetElement.scrollIntoView({
  739. behavior: 'smooth'
  740. });
  741. } else {
  742. console.error(`ID 为 ${targetId} 的元素不存在。`);
  743. }
  744. }
  745. */
  746. } else if (event.target.closest('.content a[href]:not([href=""])')) {
  747. const href = event.target.getAttribute("href");
  748. if (isExternalLink(href)) {
  749. window.open(href, "_blank");
  750. event.preventDefault();
  751. }
  752. } else if (event.target.closest(".content img, .content svg")) {
  753. const url =
  754. event.target.getAttribute("src") ||
  755. event.target.getAttribute("xlink:href");
  756. if (url) {
  757. window.open(url, "_blank");
  758. event.preventDefault();
  759. } else {
  760. const newWindow = window.open("", "_blank");
  761. newWindow.document.write(imgOrSvg.outerHTML);
  762. newWindow.document.close();
  763. event.preventDefault();
  764. }
  765. }
  766. });
  767. //// 文档内容加载完毕时触发
  768. async function showMarkdownEnded() {
  769. refreshTranslate();
  770. infoTranslate(); //// 检查并提示翻译
  771. await mermaid.init({ noteMargin: 10 }, ".language-mermaid"); //// 初始化 mermaid
  772. translate.execute(); //// 手动触发翻译,但是理论上 translate-js 会监听页面变化
  773. hljs.highlightAll(); //// 触发代码块语法高亮
  774. endLoad();
  775. window.scrollTo({
  776. top: 0,
  777. behavior: "smooth",
  778. });
  779. }
  780. //// 浏览器返回时触发
  781. window.onpopstate = function (event) {
  782. if (level > 1) {
  783. --level;
  784. }
  785. };
  786. })();
  787. </script>
  788. <title>加载中</title>
  789. <style>
  790. /* 全局 */
  791. :root,
  792. html,
  793. body,
  794. button,
  795. input,
  796. select,
  797. textarea,
  798. code,
  799. pre {
  800. font-family: "Sarasa Term SC Nerd", "Maple Mono CN", Ubuntu, Roboto,
  801. "Open Sans", "Microsoft YaHei", Arial, sans-serif;
  802. text-decoration: none;
  803. }
  804. :root,
  805. html,
  806. body {
  807. background-color: #e6e6e6;
  808. }
  809. ::selection {
  810. background-color: #dcedc8;
  811. }
  812. body {
  813. margin: 0;
  814. }
  815. a {
  816. color: #66bb6a;
  817. cursor: pointer;
  818. text-decoration: none;
  819. }
  820. a:hover {
  821. color: #2e7d32;
  822. }
  823. /* 背景 */
  824. .background {
  825. position: fixed;
  826. left: 0;
  827. top: 0;
  828. height: 100%;
  829. width: 100%;
  830. z-index: -1;
  831. background-color: #e6e6e6;
  832. }
  833. /* 导航栏 */
  834. nav {
  835. position: fixed;
  836. top: 0;
  837. right: 0;
  838. width: min-content;
  839. background-color: #616161;
  840. display: flex;
  841. justify-content: space-between;
  842. align-items: center;
  843. z-index: 1;
  844. }
  845. nav a {
  846. color: #fafafa;
  847. padding: 0.3rem;
  848. transition: color 0.2s, background-color 0.2s;
  849. }
  850. nav a:hover {
  851. color: #fff;
  852. background-color: #212121;
  853. }
  854. /* 动画 */
  855. .Animated {
  856. animation-fill-mode: both;
  857. animation-duration: 1s;
  858. }
  859. .Animated-remove {
  860. position: absolute;
  861. }
  862. .Animated--reverse {
  863. animation-direction: reverse;
  864. }
  865. .Animate--fast {
  866. animation-duration: 0.5s;
  867. }
  868. .Animate--noDelay {
  869. animation-delay: 0s !important;
  870. }
  871. .Animate--slideInRight {
  872. animation-name: Animation-slideInRight;
  873. }
  874. @keyframes Animation-slideInRight {
  875. 0% {
  876. transform: translateX(200%);
  877. }
  878. 100% {
  879. transform: translateX(0);
  880. }
  881. }
  882. .Animate--slideInLeft {
  883. animation-name: Animation-slideInLeft;
  884. }
  885. @keyframes Animation-slideInLeft {
  886. 0% {
  887. transform: translateX(-100%);
  888. }
  889. 100% {
  890. transform: translateX(0);
  891. }
  892. }
  893. /* 主要内容 */
  894. .markdown-body {
  895. position: relative;
  896. margin: 3rem auto;
  897. width: 100%;
  898. max-width: 950px;
  899. min-height: 300px;
  900. background-color: #fafafa;
  901. color: #212121;
  902. box-sizing: border-box;
  903. overflow-x: hidden;
  904. box-shadow: 0 2px 6px rgba(100, 100, 100, 0.3);
  905. }
  906. .markdown-body .content,
  907. .markdown-body .old-content {
  908. margin: 2rem;
  909. }
  910. .markdown-body img,
  911. .markdown-body video,
  912. .markdown-body iframe,
  913. .markdown-body object,
  914. .markdown-body embed,
  915. .markdown-body svg,
  916. .markdown-body canvas,
  917. .markdown-body audio {
  918. max-width: 100%;
  919. background-color: rgba(0, 0, 0, 0.05);
  920. }
  921. /* Markdown 样式 */
  922. .markdown-body a {
  923. transition: color 0.2s, background-color 0.2s;
  924. }
  925. .markdown-body a:hover {
  926. background-color: rgba(255, 255, 255, 0.1);
  927. }
  928. .markdown-body ul,
  929. .markdown-body ol {
  930. padding-left: 2rem;
  931. }
  932. .markdown-body pre {
  933. background-color: rgba(0, 0, 0, 0.05);
  934. padding: 0.4rem;
  935. border-radius: 0.1rem;
  936. overflow-y: scroll;
  937. }
  938. .markdown-body code {
  939. font-family: "cousine nerd font mono", monospace;
  940. background-color: rgba(0, 0, 0, 0.05);
  941. font-size: small;
  942. padding: 0.2rem;
  943. border-radius: 0.1rem;
  944. }
  945. .markdown-body pre code {
  946. background-color: unset;
  947. font-size: unset;
  948. padding: unset;
  949. border-radius: unset;
  950. }
  951. .markdown-body blockquote {
  952. background-color: rgba(0, 0, 0, 0.05);
  953. font-size: small;
  954. padding: 0.4rem;
  955. margin: 0.5rem 2rem;
  956. border-left: 5px solid rgba(0, 0, 0, 0.1);
  957. }
  958. .markdown-body details {
  959. border: 1px solid rgba(0, 0, 0, 0.1);
  960. border-radius: 0.1rem;
  961. }
  962. .markdown-body summary {
  963. font-size: small;
  964. cursor: pointer;
  965. background-color: rgba(0, 0, 0, 0.05);
  966. padding: 0.4rem;
  967. font-weight: bold;
  968. }
  969. .markdown-body details a {
  970. margin-left: -2rem;
  971. }
  972. .markdown-body table {
  973. border-spacing: 0;
  974. border-radius: 0.1rem;
  975. width: 100%;
  976. border-collapse: separate;
  977. border: 0.1rem solid rgba(0, 0, 0, 0.05);
  978. }
  979. .markdown-body table > * {
  980. overflow-y: scroll;
  981. }
  982. .markdown-body table td,
  983. .markdown-body table th {
  984. border-right: 0.1rem solid rgba(0, 0, 0, 0.05);
  985. border-top: 0.1rem solid rgba(0, 0, 0, 0.05);
  986. padding: 0.4rem;
  987. }
  988. .markdown-body table td:last-child,
  989. .markdown-body th:last-child {
  990. border-right-color: transparent;
  991. }
  992. .markdown-body table thead tr:first-child > * {
  993. border-top: 0.1rem solid transparent;
  994. }
  995. /* 加载进度条 */
  996. .loading-bar {
  997. position: fixed;
  998. top: 0;
  999. left: 0;
  1000. z-index: 2;
  1001. opacity: 0;
  1002. transition: opacity 0.4s linear;
  1003. .progress {
  1004. position: fixed;
  1005. top: 0;
  1006. left: 0;
  1007. width: 0;
  1008. height: 4px;
  1009. background-color: #2e7d32;
  1010. box-shadow: 0 0 10px rgba(102, 187, 106, 0.7);
  1011. }
  1012. &.loading {
  1013. opacity: 1;
  1014. transition: none;
  1015. .progress {
  1016. transition: width 0.4s ease;
  1017. }
  1018. }
  1019. }
  1020. /* 翻译按钮相关 */
  1021. .translateSelectLanguage {
  1022. background-color: #616161;
  1023. color: inherit;
  1024. z-index: 3;
  1025. width: 17%;
  1026. height: 100%;
  1027. opacity: 0;
  1028. cursor: pointer;
  1029. position: absolute;
  1030. top: 0;
  1031. left: 50%;
  1032. }
  1033. /* 深色模式相关 */
  1034. .darkmode--activated .markdown-body img,
  1035. .darkmode--activated .markdown-body svg {
  1036. isolation: isolate;
  1037. mix-blend-mode: difference;
  1038. }
  1039. .darkmode-layer,
  1040. .darkmode-toggle {
  1041. z-index: 3;
  1042. }
  1043. /* 小屏响应式优化 */
  1044. @media only screen and (max-width: 950px) {
  1045. html,
  1046. body,
  1047. .markdown-body {
  1048. height: 100%;
  1049. }
  1050. .markdown-body {
  1051. overflow-y: scroll;
  1052. min-width: 256px;
  1053. top: 0;
  1054. margin: 0 0;
  1055. }
  1056. }
  1057. </style>
  1058. <link
  1059. href="https://lf26-cdn-tos.bytecdntp.com/cdn/expire-1-y/highlight.js/11.4.0/styles/default.min.css"
  1060. type="text/css"
  1061. rel="stylesheet"
  1062. />
  1063. </head>
  1064. <body>
  1065. <!-- 导航栏 -->
  1066. <nav>
  1067. <a title="返回" class="back-btn">
  1068. <svg
  1069. xmlns="http://www.w3.org/2000/svg"
  1070. width="24"
  1071. height="24"
  1072. viewBox="0 0 24 24"
  1073. fill="none"
  1074. stroke="currentColor"
  1075. stroke-width="2"
  1076. stroke-linecap="round"
  1077. stroke-linejoin="round"
  1078. class="icon icon-tabler icons-tabler-outline icon-tabler-arrow-back"
  1079. >
  1080. <path stroke="none" d="M0 0h24v24H0z" fill="none" />
  1081. <path d="M9 11l-4 4l4 4m-4 -4h11a4 4 0 0 0 0 -8h-1" />
  1082. </svg>
  1083. </a>
  1084. <a title="刷新" class="refresh-btn" href="">
  1085. <svg
  1086. xmlns="http://www.w3.org/2000/svg"
  1087. width="24"
  1088. height="24"
  1089. viewBox="0 0 24 24"
  1090. fill="none"
  1091. stroke="currentColor"
  1092. stroke-width="2"
  1093. stroke-linecap="round"
  1094. stroke-linejoin="round"
  1095. class="icon icon-tabler icons-tabler-outline icon-tabler-refresh"
  1096. >
  1097. <path stroke="none" d="M0 0h24v24H0z" fill="none" />
  1098. <path d="M20 11a8.1 8.1 0 0 0 -15.5 -2m-.5 -4v4h4" />
  1099. <path d="M4 13a8.1 8.1 0 0 0 15.5 2m.5 4v-4h-4" />
  1100. </svg>
  1101. </a>
  1102. <a title="主页" class="to-home">
  1103. <svg
  1104. xmlns="http://www.w3.org/2000/svg"
  1105. width="24"
  1106. height="24"
  1107. viewBox="0 0 24 24"
  1108. fill="none"
  1109. stroke="currentColor"
  1110. stroke-width="2"
  1111. stroke-linecap="round"
  1112. stroke-linejoin="round"
  1113. class="icon icon-tabler icons-tabler-outline icon-tabler-home"
  1114. >
  1115. <path stroke="none" d="M0 0h24v24H0z" fill="none" />
  1116. <path d="M5 12l-2 0l9 -9l9 9l-2 0" />
  1117. <path d="M5 12v7a2 2 0 0 0 2 2h10a2 2 0 0 0 2 -2v-7" />
  1118. <path d="M9 21v-6a2 2 0 0 1 2 -2h2a2 2 0 0 1 2 2v6" />
  1119. </svg>
  1120. </a>
  1121. <a title="更改语言" id="translate">
  1122. <svg
  1123. xmlns="http://www.w3.org/2000/svg"
  1124. width="24"
  1125. height="24"
  1126. viewBox="0 0 24 24"
  1127. fill="none"
  1128. stroke="currentColor"
  1129. stroke-width="2"
  1130. stroke-linecap="round"
  1131. stroke-linejoin="round"
  1132. class="icon icon-tabler icons-tabler-outline icon-tabler-language"
  1133. >
  1134. <path stroke="none" d="M0 0h24v24H0z" fill="none" />
  1135. <path d="M4 5h7" />
  1136. <path d="M9 3v2c0 4.418 -2.239 8 -5 8" />
  1137. <path d="M5 9c0 2.144 2.952 3.908 6.7 4" />
  1138. <path d="M12 20l4 -9l4 9" />
  1139. <path d="M19.1 18h-6.2" />
  1140. </svg>
  1141. </a>
  1142. <a title="切换显示模式" class="dark-btn" href="javascript:;">
  1143. <svg
  1144. xmlns="http://www.w3.org/2000/svg"
  1145. width="24"
  1146. height="24"
  1147. viewBox="0 0 24 24"
  1148. fill="none"
  1149. stroke="currentColor"
  1150. stroke-width="2"
  1151. stroke-linecap="round"
  1152. stroke-linejoin="round"
  1153. class="icon icon-tabler icons-tabler-outline icon-tabler-brightness"
  1154. >
  1155. <path stroke="none" d="M0 0h24v24H0z" fill="none" />
  1156. <path d="M12 12m-9 0a9 9 0 1 0 18 0a9 9 0 1 0 -18 0" />
  1157. <path d="M12 3l0 18" />
  1158. <path d="M12 9l4.65 -4.65" />
  1159. <path d="M12 14.3l7.37 -7.37" />
  1160. <path d="M12 19.6l8.85 -8.85" />
  1161. </svg>
  1162. </a>
  1163. <a title="关于" class="about-btn">
  1164. <svg
  1165. xmlns="http://www.w3.org/2000/svg"
  1166. width="24"
  1167. height="24"
  1168. viewBox="0 0 24 24"
  1169. fill="none"
  1170. stroke="currentColor"
  1171. stroke-width="2"
  1172. stroke-linecap="round"
  1173. stroke-linejoin="round"
  1174. class="icon icon-tabler icons-tabler-outline icon-tabler-info-circle"
  1175. >
  1176. <path stroke="none" d="M0 0h24v24H0z" fill="none" />
  1177. <path d="M3 12a9 9 0 1 0 18 0a9 9 0 0 0 -18 0" />
  1178. <path d="M12 9h.01" />
  1179. <path d="M11 12h1v4h1" />
  1180. </svg>
  1181. </a>
  1182. </nav>
  1183. <!-- 内容主体 -->
  1184. <div class="markdown-body">
  1185. <div class="content">
  1186. <h1>加载中……</h1>
  1187. <p>请稍等。</p>
  1188. </div>
  1189. </div>
  1190. <!-- 加载动画 -->
  1191. <div class="loading-bar">
  1192. <div class="progress"></div>
  1193. </div>
  1194. <div class="background"></div>
  1195. <script
  1196. src="https://lf6-cdn-tos.bytecdntp.com/cdn/expire-1-y/instant.page/5.1.0/instantpage.min.js"
  1197. type="text/javascript"
  1198. ></script>
  1199. </body>
  1200. </html>