1
0

index.html 37 KB

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