1
0

index.html 36 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068
  1. <!doctype html>
  2. <html>
  3. <markdown-html version="1.16.0" 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 isRelativePath(path) {
  254. return !path.match(/^\//);
  255. }
  256. // 处理错误
  257. function updateContent(article, title = null, mdPath = null) {
  258. try {
  259. 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>`;
  260. firstH1 = document.querySelector('.markdown-body .content h1');
  261. document.title = title ? title : (firstH1 ? firstH1.textContent : mdPath);
  262. } catch (error) {
  263. throw error;
  264. }
  265. }
  266. // 等待动画结束
  267. function waitForAnimationsEnd(parentElement, selector, animationNames) {
  268. return new Promise(resolve => {
  269. var completedCount = 0;
  270. const elements = parentElement.querySelectorAll(selector);
  271. const totalElements = elements.length;
  272. const handleAnimationEnd = function (event) {
  273. if (animationNames.includes(event.animationName) && Array.from(elements).includes(event.target)) {
  274. completedCount++;
  275. if (completedCount === totalElements) {
  276. parentElement.removeEventListener('animationend', handleAnimationEnd);
  277. resolve();
  278. }
  279. }
  280. };
  281. parentElement.addEventListener('animationend', handleAnimationEnd);
  282. });
  283. }
  284. // 处理错误
  285. function handleError(mdPath, errorMessage) {
  286. updateContent(`<h1>加载失败</h1><p>加载文档<code>${mdPath}</code>时出错:<code>${errorMessage}</code>。您可以尝试<a class="refresh-btn" href="">重新加载</a>或<a class="back-btn">返回</a>。</p>`, '加载失败');
  287. customPushState(window.location.origin + window.location.pathname + '?p=error.md');
  288. waitForAnimationsEnd(document.querySelector('.markdown-body'), '.content, .old-content', ['Animation-slideInRight', 'Animation-slideInLeft']).then(() => {
  289. customPushState(window.location.origin + window.location.pathname + (mdPath ? ('?p=' + mdPath) : ''));
  290. });
  291. endLoad();
  292. }
  293. // 更新页面的 markdown 内容
  294. /*
  295. mdPath: 要加载的 markdown 文件路径,最好是带主机名的绝对路径(要用于 fetch())
  296. response: fetch() 的返回值,用于防止二次访问
  297. */
  298. const showMarkdown = async (mdPath, response = null) => {
  299. if (mdPath === null) {
  300. var path = null;
  301. const currentPath = getPath();
  302. // 如果存在 index.md 文件,则返回 index.md,否则返回 README.md
  303. if (notExistIndexPage) {
  304. path = currentPath + 'README.md';
  305. } else if (notExistIndexPage === false) {
  306. path = currentPath + 'index.md';
  307. } else {
  308. response = await fetch(getPath() + 'index.md');
  309. if (response.ok && response.headers.get('content-type').startsWith('text/markdown')) {
  310. notExistIndexPage = false;
  311. path = currentPath + 'index.md';
  312. } else {
  313. response = null;
  314. notExistIndexPage = true;
  315. path = currentPath + 'README.md';
  316. }
  317. }
  318. } else if (mdPath.endsWith('about.md')) {
  319. // 特殊处理 about.md
  320. response = await fetch(mdPath);
  321. const about = getAbout();
  322. if (response.ok && response.headers.get('content-type').startsWith('text/markdown')) {
  323. var r_text = await response.text();
  324. const content = about + await r_text.replace(/^# .*/m, '');
  325. response = {
  326. ok: true,
  327. status: 200,
  328. statusText: '自生成。',
  329. text: async () => {
  330. return content;
  331. }
  332. };
  333. } else {
  334. response = {
  335. ok: true,
  336. status: 200,
  337. statusText: '自生成。',
  338. text: async () => {
  339. return about + '*无更多详细信息*';
  340. }
  341. };
  342. }
  343. }
  344. try {
  345. pValue = mdPath ? mdPath : path;
  346. updateContent(await getMarkdown((mdPath ? mdPath : path), response))
  347. document.querySelector('html').classList.add('loaded'); //// 防止缓存的页面被自定义 PJAX 中覆写的处理响应的函数捕获
  348. waitForAnimationsEnd(document.querySelector('.markdown-body'), '.content, .old-content', ['Animation-slideInRight', 'Animation-slideInLeft']).then(async () => {
  349. await showMarkdownEnded();
  350. customPushState(window.location.origin + window.location.pathname + (mdPath ? ('?p=' + mdPath) : ''));
  351. });
  352. } catch (error) {
  353. handleError((mdPath ? mdPath : path), error)
  354. }
  355. }
  356. // 读取并解析指定文件
  357. const getMarkdown = async (mdPath, response = null) => {
  358. // 生成目录
  359. function generateOutline(doc) {
  360. const headings = Array.from(doc.querySelectorAll('h1, h2, h3, h4, h5, h6'));
  361. if (headings.length === 0) return '<i>无目录</i>';
  362. var outline = '<ul>';
  363. var stack = [];
  364. headings.forEach(heading => {
  365. const level = parseInt(heading.tagName.charAt(1));
  366. const id = heading.textContent.toLowerCase()
  367. .replace(/ /g, '-') // 将空格替换为连字符
  368. .replace(/[^\w\u4e00-\u9fa5-]/g, ''); // 保留字母、数字、下划线、中文字符和连字符
  369. // 设置 id 属性以便链接跳转
  370. heading.setAttribute('id', id);
  371. while (stack.length > 0 && stack[stack.length - 1].level >= level) {
  372. stack.pop();
  373. outline += '</ul></li>';
  374. }
  375. if (stack.length === 0 || stack[stack.length - 1].level < level) {
  376. outline += '<li><ul>';
  377. } else {
  378. outline += '</li><li>';
  379. }
  380. outline += `<a href="#${id}">${heading.textContent}</a>`;
  381. stack.push({ level: level });
  382. });
  383. while (stack.length > 0) {
  384. outline += '</ul></li>';
  385. stack.pop();
  386. }
  387. outline += '</ul>';
  388. return outline;
  389. }
  390. if (!response) {
  391. response = await fetch(mdPath);
  392. if (!response.ok && response.headers.get('content-type').startsWith('text/markdown')) {
  393. throw new Error('请求失败:' + mdPath + ',' + response.headers.get('content-type') + ',' + response.status + ',' + response.statusText);
  394. }
  395. }
  396. var markdown_content = await response.text();
  397. // 如果 markdown_content 中发现 `[任意标题](路径文件名.md)`
  398. /*
  399. 如果 href 是相对路径,则在其前加上当前读取的文档的绝对路径;
  400. 如果不存在当前读取的文档(默认读取的 index.md 或 README.md),
  401. 则在其前加上 markdown.html 的绝对路径。
  402. */
  403. const links = markdown_content.match(/\[.*\]\(.*\.md\)/g);
  404. if (links) {
  405. links.forEach(link => {
  406. const linkText = link.match(/\[.*\]/)[0].slice(1, -1);
  407. var linkHref = link.match(/\(.*\)/)[0].slice(1, -1);
  408. if (isRelativePath(linkHref)) {
  409. if (pValue) {
  410. linkHref = getDirectory(mdPath) + linkHref;
  411. } else {
  412. linkHref = getPath() + linkHref;
  413. }
  414. linkHref = new URL(linkHref, 'http://example.com').pathname;
  415. }
  416. const newLink = `[${linkText}](?p=${linkHref})`; // 链接格式与 window.location.search 一致
  417. markdown_content = markdown_content.replace(link, newLink);
  418. });
  419. }
  420. var content = marked.parse(markdown_content);
  421. if (content === '') {
  422. content = '<i>空文件</i>';
  423. }
  424. content = `<details class="outline"><summary>${decodeURIComponent(mdPath)}</summary></details><hr>${content}`;
  425. const parser = new DOMParser();
  426. const doc = parser.parseFromString(content, 'text/html');
  427. const outline = generateOutline(doc);
  428. doc.querySelector('.outline').innerHTML += outline;
  429. return doc.body.innerHTML;
  430. };
  431. // 初始化自定义 PJAX 响应
  432. function initCustomPJAXResponse() {
  433. // 覆写 PJAX 处理响应的函数,在内容插入页面之前实现 markdown 的渲染
  434. PJAX._handleResponse = PJAX.handleResponse;
  435. PJAX.handleResponse = async function (responseText, request, href, options) {
  436. if (request.responseText.startsWith('<!doctype html><html><markdown-html') || request.responseText.startsWith(`<!doctype html>
  437. <html>
  438. <markdown-html`)) {
  439. await showMarkdown(await decodeMDPath(href));
  440. } else {
  441. PJAX._handleResponse(responseText, request, href, options);
  442. }
  443. }
  444. }
  445. // 初始化自定义 PJAX 事件监听器
  446. function initCustomPJAXEventListener() {
  447. // 监听自定义链接的点击事件
  448. if (existPushState) {
  449. document.querySelector('.markdown-body').addEventListener('click', async function (event) {
  450. const link = event.target.closest('a[href$=".md"]');
  451. if (link) {
  452. try {
  453. event.preventDefault();
  454. startLoad();
  455. await showMarkdown(await decodeMDPath(link.href));
  456. } catch (e) {
  457. console.error('自定义 PJAX 出错:' + e);
  458. }
  459. }
  460. });
  461. }
  462. }
  463. // 自定义推送状态
  464. function customPushState(url = window.location.href) {
  465. if (existPushState) {
  466. if (level > lastLevel) {
  467. history.pushState({ url: url, title: document.title, uid: level }, null, url);
  468. lastLevel = level++;
  469. } else {
  470. lastLevel = level - 1;
  471. }
  472. }
  473. }
  474. // 初始化内容
  475. const initContent = async () => {
  476. try {
  477. await showMarkdown(await getPValue());
  478. } catch (error) {
  479. handleError(await getPValue(), '初始化内容时出错:' + error);
  480. }
  481. };
  482. // 初始化返回首页按钮
  483. function initHomeBtn() {
  484. const homeBtn = document.querySelector('.to-home');
  485. homeBtn.href = window.location.pathname;
  486. }
  487. // 初始化返回首页按钮
  488. function initAboutBtn() {
  489. const aboutBtn = document.querySelector('.about-btn');
  490. aboutBtn.href = `?p=${getDirectory(window.location.pathname)}about.md`;
  491. }
  492. // 初始化
  493. function initialize() {
  494. initDarkmode(); //// 初始化深色模式
  495. initHomeBtn(); //// 初始化返回首页按钮
  496. initAboutBtn(); //// 初始化关于按钮
  497. initContent(); //// 初始化内容
  498. initTranslate(); //// 初始化页面翻译
  499. initPJAX(); //// 初始化 PJAX
  500. initCustomPJAXResponse(); //// 初始化自定义 PJAX 响应
  501. initCustomPJAXEventListener(); //// 初始化自定义 PJAX 事件监听器
  502. }
  503. // 触发器
  504. //// 网页加载完毕后触发初始化
  505. window.addEventListener('DOMContentLoaded', () => initialize());
  506. //// 翻译执行完成后触发
  507. translate.listener.renderTaskFinish = function (_task) {
  508. endLoad();
  509. }
  510. //// PJAX 开始时执行的函数
  511. document.addEventListener('pjax:send', function () {
  512. startLoad();
  513. });
  514. //// 监听 PJAX 完成后,重新加载
  515. document.addEventListener('pjax:complete', async function () {
  516. await showMarkdown(await getPValue());
  517. initCustomPJAXEventListener(); //// 重新初始化自定义 PJAX 事件监听器
  518. });
  519. //// 监听 animationend 事件
  520. document.addEventListener('animationend', function handleAnimationEnd(event) {
  521. if (event.animationName === 'Animation-slideInRight') {
  522. const element = document.querySelector('.markdown-body .content');
  523. element.classList.remove('Animated');
  524. element.classList.remove('Animate--slideInRight');
  525. } else if (event.animationName === 'Animation-slideInLeft') {
  526. document.querySelector('.markdown-body .old-content').remove();
  527. }
  528. });
  529. //// 点击事件委托
  530. document.addEventListener('click', function (event) {
  531. const imgOrSvg = event.target.closest('.content img, .content svg');
  532. if (imgOrSvg) {
  533. const url = imgOrSvg.getAttribute('src') || imgOrSvg.getAttribute('xlink:href');
  534. if (url) {
  535. window.open(url, '_blank');
  536. event.preventDefault();
  537. } else {
  538. const newWindow = window.open('', '_blank');
  539. newWindow.document.write(imgOrSvg.outerHTML);
  540. newWindow.document.close();
  541. event.preventDefault();
  542. }
  543. return;
  544. }
  545. if (event.target.closest('.back-btn')) {
  546. window.history.back();
  547. } else if (event.target.closest('.refresh-btn')) {
  548. window.location.reload();
  549. } else if (event.target.closest('.dark-btn')) {
  550. darkmode.toggle();
  551. } else if (event.target.closest('.translate-switch')) {
  552. translate.changeLanguage(translate.language.getLocal());
  553. // 跳转。禁用原因是有些带有特殊字符的链接会出错或无法跳转
  554. // } else if (event.target.closest('.content a[href^="#"]')) {
  555. // // 目录平滑移动
  556. // const href = event.target.getAttribute('href');
  557. // if (href !== '#') {
  558. // event.preventDefault();
  559. // const targetElement = document.querySelector(href);
  560. // if (targetElement) {
  561. // targetElement.scrollIntoView({
  562. // behavior: 'smooth'
  563. // });
  564. // } else {
  565. // console.error(`ID 为 ${targetId} 的元素不存在。`);
  566. // }
  567. // }
  568. } else if (event.target.closest('.content a:not([href^="?"]):not([href^="#"]):not([href^="javascript:"])')) {
  569. const link = event.target.closest('a');
  570. if (link && link.href) {
  571. window.open(link.href, '_blank');
  572. event.preventDefault();
  573. return;
  574. }
  575. }
  576. });
  577. //// 文档内容加载完毕时触发
  578. async function showMarkdownEnded() {
  579. refreshTranslate();
  580. infoTranslate(); //// 检查并提示翻译
  581. await mermaid.init({ noteMargin: 10 }, '.language-mermaid'); //// 初始化 mermaid
  582. translate.execute(); //// 手动触发翻译,但是理论上 translate-js 会监听页面变化
  583. hljs.highlightAll(); //// 触发代码块语法高亮
  584. endLoad();
  585. window.scrollTo({
  586. top: 0,
  587. behavior: 'smooth'
  588. });
  589. }
  590. //// 浏览器返回时触发
  591. window.onpopstate = function (event) {
  592. if (level > 1) {
  593. --level;
  594. }
  595. };
  596. })();
  597. </script>
  598. <title>加载中</title>
  599. <style>
  600. /* 全局 */
  601. :root,
  602. html,
  603. body,
  604. button,
  605. input,
  606. select,
  607. textarea,
  608. code,
  609. pre {
  610. font-family: 'Sarasa Term SC Nerd', 'Maple Mono CN', Ubuntu, Roboto, 'Open Sans', 'Microsoft YaHei', Arial, sans-serif;
  611. text-decoration: none;
  612. }
  613. :root,
  614. html,
  615. body {
  616. background-color: #e6e6e6;
  617. }
  618. ::selection {
  619. background-color: #DCEDC8;
  620. }
  621. body {
  622. margin: 0;
  623. }
  624. a {
  625. color: #66BB6A;
  626. cursor: pointer;
  627. text-decoration: none;
  628. }
  629. a:hover {
  630. color: #2E7D32;
  631. }
  632. /* 背景 */
  633. .background {
  634. position: fixed;
  635. left: 0;
  636. top: 0;
  637. height: 100%;
  638. width: 100%;
  639. z-index: -1;
  640. background-color: #e6e6e6;
  641. }
  642. /* 导航栏 */
  643. nav {
  644. position: fixed;
  645. top: 0;
  646. right: 0;
  647. width: min-content;
  648. background-color: #616161;
  649. display: flex;
  650. justify-content: space-between;
  651. align-items: center;
  652. z-index: 1;
  653. }
  654. nav a {
  655. color: #FAFAFA;
  656. padding: .3rem;
  657. transition:
  658. color .2s,
  659. background-color .2s;
  660. }
  661. nav a:hover {
  662. color: #FFF;
  663. background-color: #212121;
  664. }
  665. /* 动画 */
  666. .Animated {
  667. animation-fill-mode: both;
  668. animation-duration: 1s;
  669. }
  670. .Animated-remove {
  671. position: absolute;
  672. }
  673. .Animated--reverse {
  674. animation-direction: reverse
  675. }
  676. .Animate--fast {
  677. animation-duration: .5s
  678. }
  679. .Animate--noDelay {
  680. animation-delay: 0s !important;
  681. }
  682. .Animate--slideInRight {
  683. animation-name: Animation-slideInRight
  684. }
  685. @keyframes Animation-slideInRight {
  686. 0% {
  687. transform: translateX(200%);
  688. }
  689. 100% {
  690. transform: translateX(0);
  691. }
  692. }
  693. .Animate--slideInLeft {
  694. animation-name: Animation-slideInLeft
  695. }
  696. @keyframes Animation-slideInLeft {
  697. 0% {
  698. transform: translateX(-100%);
  699. }
  700. 100% {
  701. transform: translateX(0);
  702. }
  703. }
  704. /* 主要内容 */
  705. .markdown-body {
  706. position: relative;
  707. margin: 3rem auto;
  708. width: 100%;
  709. max-width: 950px;
  710. min-height: 300px;
  711. background-color: #FAFAFA;
  712. color: #212121;
  713. box-sizing: border-box;
  714. overflow-x: hidden;
  715. box-shadow: 0 2px 6px rgba(100, 100, 100, .3);
  716. }
  717. .markdown-body .content,
  718. .markdown-body .old-content {
  719. margin: 2rem;
  720. }
  721. .markdown-body img,
  722. .markdown-body video,
  723. .markdown-body iframe,
  724. .markdown-body object,
  725. .markdown-body embed,
  726. .markdown-body svg,
  727. .markdown-body canvas,
  728. .markdown-body audio {
  729. max-width: 100%;
  730. background-color: rgba(0, 0, 0, 0.05);
  731. }
  732. /* Markdown 样式 */
  733. .markdown-body a {
  734. transition:
  735. color .2s,
  736. background-color .2s;
  737. }
  738. .markdown-body a:hover {
  739. background-color: rgba(255, 255, 255, 0.1);
  740. }
  741. .markdown-body ul,
  742. .markdown-body ol {
  743. padding-left: 2rem;
  744. }
  745. .markdown-body pre {
  746. background-color: rgba(0, 0, 0, 0.05);
  747. padding: .4rem;
  748. border-radius: .1rem;
  749. overflow-y: scroll;
  750. }
  751. .markdown-body code {
  752. font-family: "cousine nerd font mono", monospace;
  753. background-color: rgba(0, 0, 0, 0.05);
  754. font-size: small;
  755. padding: .2rem;
  756. border-radius: .1rem;
  757. }
  758. .markdown-body pre code {
  759. background-color: unset;
  760. font-size: unset;
  761. padding: unset;
  762. border-radius: unset;
  763. }
  764. .markdown-body blockquote {
  765. background-color: rgba(0, 0, 0, 0.05);
  766. font-size: small;
  767. padding: .4rem;
  768. margin: .5rem 2rem;
  769. border-left: 5px solid rgba(0, 0, 0, 0.1);
  770. }
  771. .markdown-body details {
  772. border: 1px solid rgba(0, 0, 0, 0.1);
  773. border-radius: .1rem;
  774. }
  775. .markdown-body summary {
  776. font-size: small;
  777. cursor: pointer;
  778. background-color: rgba(0, 0, 0, 0.05);
  779. padding: .4rem;
  780. font-weight: bold;
  781. }
  782. .markdown-body details a {
  783. margin-left: -2rem;
  784. }
  785. .markdown-body table {
  786. border-spacing: 0;
  787. border-radius: .1rem;
  788. width: 100%;
  789. border-collapse: separate;
  790. border: .1rem solid rgba(0, 0, 0, 0.05);
  791. }
  792. .markdown-body table td,
  793. .markdown-body table th {
  794. border-right: .1rem solid rgba(0, 0, 0, 0.05);
  795. }
  796. .markdown-body table td,
  797. .markdown-body table th {
  798. border-top: .1rem solid rgba(0, 0, 0, 0.05);
  799. }
  800. .markdown-body table td:last-child,
  801. .markdown-body th:last-child {
  802. border-right-color: transparent;
  803. }
  804. .markdown-body table thead tr:first-child>* {
  805. border-top: .1rem solid transparent;
  806. }
  807. /* 加载进度条 */
  808. .loading-bar {
  809. position: fixed;
  810. top: 0;
  811. left: 0;
  812. z-index: 2;
  813. opacity: 0;
  814. transition: opacity .4s linear;
  815. .progress {
  816. position: fixed;
  817. top: 0;
  818. left: 0;
  819. width: 0;
  820. height: 4px;
  821. background-color: #2E7D32;
  822. box-shadow: 0 0 10px rgba(102, 187, 106, .7);
  823. }
  824. &.loading {
  825. opacity: 1;
  826. transition: none;
  827. .progress {
  828. transition: width .4s ease;
  829. }
  830. }
  831. }
  832. /* 翻译按钮相关 */
  833. .translateSelectLanguage {
  834. background-color: #616161;
  835. color: inherit;
  836. z-index: 3;
  837. width: 17%;
  838. height: 100%;
  839. opacity: 0;
  840. cursor: pointer;
  841. position: absolute;
  842. top: 0;
  843. left: 50%;
  844. }
  845. /* 深色模式相关 */
  846. .darkmode--activated .markdown-body img,
  847. .darkmode--activated .markdown-body svg {
  848. isolation: isolate;
  849. mix-blend-mode: difference
  850. }
  851. .darkmode-layer,
  852. .darkmode-toggle {
  853. z-index: 3
  854. }
  855. /* 小屏响应式优化 */
  856. @media only screen and (max-width: 950px) {
  857. html,
  858. body,
  859. .markdown-body {
  860. height: 100%;
  861. }
  862. .markdown-body {
  863. overflow-y: scroll;
  864. min-width: 256px;
  865. top: 0;
  866. margin: 0 0;
  867. }
  868. }
  869. </style>
  870. <link href="https://lf26-cdn-tos.bytecdntp.com/cdn/expire-1-y/highlight.js/11.4.0/styles/default.min.css"
  871. type="text/css" rel="stylesheet" />
  872. <link rel="stylesheet"
  873. href="https://chinese-fonts-cdn.deno.dev/packages/maple-mono-cn/dist/MapleMono-CN-Regular/result.css" />
  874. </head>
  875. <body>
  876. <!-- 导航栏 -->
  877. <nav>
  878. <a title="返回" class="back-btn">
  879. <svg xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24" fill="none"
  880. stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"
  881. class="icon icon-tabler icons-tabler-outline icon-tabler-arrow-back">
  882. <path stroke="none" d="M0 0h24v24H0z" fill="none" />
  883. <path d="M9 11l-4 4l4 4m-4 -4h11a4 4 0 0 0 0 -8h-1" />
  884. </svg>
  885. </a>
  886. <a title="刷新" class="refresh-btn" href="">
  887. <svg xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24" fill="none"
  888. stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"
  889. class="icon icon-tabler icons-tabler-outline icon-tabler-refresh">
  890. <path stroke="none" d="M0 0h24v24H0z" fill="none" />
  891. <path d="M20 11a8.1 8.1 0 0 0 -15.5 -2m-.5 -4v4h4" />
  892. <path d="M4 13a8.1 8.1 0 0 0 15.5 2m.5 4v-4h-4" />
  893. </svg>
  894. </a>
  895. <a title="主页" class="to-home">
  896. <svg xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24" fill="none"
  897. stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"
  898. class="icon icon-tabler icons-tabler-outline icon-tabler-home">
  899. <path stroke="none" d="M0 0h24v24H0z" fill="none" />
  900. <path d="M5 12l-2 0l9 -9l9 9l-2 0" />
  901. <path d="M5 12v7a2 2 0 0 0 2 2h10a2 2 0 0 0 2 -2v-7" />
  902. <path d="M9 21v-6a2 2 0 0 1 2 -2h2a2 2 0 0 1 2 2v6" />
  903. </svg>
  904. </a>
  905. <a title="更改语言" id="translate">
  906. <svg xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24" fill="none"
  907. stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"
  908. class="icon icon-tabler icons-tabler-outline icon-tabler-language">
  909. <path stroke="none" d="M0 0h24v24H0z" fill="none" />
  910. <path d="M4 5h7" />
  911. <path d="M9 3v2c0 4.418 -2.239 8 -5 8" />
  912. <path d="M5 9c0 2.144 2.952 3.908 6.7 4" />
  913. <path d="M12 20l4 -9l4 9" />
  914. <path d="M19.1 18h-6.2" />
  915. </svg>
  916. </a>
  917. <a title="切换显示模式" class="dark-btn" href="javascript:;">
  918. <svg xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24" fill="none"
  919. stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"
  920. class="icon icon-tabler icons-tabler-outline icon-tabler-brightness">
  921. <path stroke="none" d="M0 0h24v24H0z" fill="none" />
  922. <path d="M12 12m-9 0a9 9 0 1 0 18 0a9 9 0 1 0 -18 0" />
  923. <path d="M12 3l0 18" />
  924. <path d="M12 9l4.65 -4.65" />
  925. <path d="M12 14.3l7.37 -7.37" />
  926. <path d="M12 19.6l8.85 -8.85" />
  927. </svg>
  928. </a>
  929. <a title="关于" class="about-btn">
  930. <svg xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24" fill="none"
  931. stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"
  932. class="icon icon-tabler icons-tabler-outline icon-tabler-info-circle">
  933. <path stroke="none" d="M0 0h24v24H0z" fill="none" />
  934. <path d="M3 12a9 9 0 1 0 18 0a9 9 0 0 0 -18 0" />
  935. <path d="M12 9h.01" />
  936. <path d="M11 12h1v4h1" />
  937. </svg>
  938. </a>
  939. </nav>
  940. <!-- 内容主体 -->
  941. <div class="markdown-body">
  942. <div class="content">
  943. <h1>加载中……</h1>
  944. <p>请稍等。</p>
  945. </div>
  946. </div>
  947. <!-- 加载动画 -->
  948. <div class="loading-bar">
  949. <div class="progress"></div>
  950. </div>
  951. <div class="background"></div>
  952. <script src="https://lf6-cdn-tos.bytecdntp.com/cdn/expire-1-y/instant.page/5.1.0/instantpage.min.js"
  953. type="text/javascript"></script>
  954. </body>
  955. </html>