1
0

index.html 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642
  1. <!doctype html>
  2. <html>
  3. <!-- 作者:PJ568 -->
  4. <!-- 项目地址:https://github.com/PJ-568/markdown.html/ -->
  5. <!-- 本项目遵循 CC BY-SA 4.0 International 许可协议 -->
  6. <!-- v 0.7.1 -->
  7. <head>
  8. <meta charset="utf-8">
  9. <meta name="description" content="HTML read markdown">
  10. <meta name="viewport" content="width=256, initial-scale=1">
  11. <script defer src="//lib.baomitu.com/marked/4.0.2/marked.min.js" type="text/javascript"></script>
  12. <script src="//lib.baomitu.com/pjax/0.2.8/pjax.min.js" type="text/javascript"></script>
  13. <script src="//lib.baomitu.com/translate.js/3.7.2/translate.js" type="text/javascript"></script>
  14. <script src="//lib.baomitu.com/Darkmode.js/1.5.7/darkmode-js.min.js" type="text/javascript"></script>
  15. <link rel="stylesheet" href="https://lf6-cdn-tos.bytecdntp.com/cdn/expire-1-y/font-awesome/6.0.0/css/all.min.css">
  16. <script>
  17. // 使用 IIFE 包裹以避免全局变量污染
  18. (function () {
  19. // 加载动画相关
  20. var loadingBar = document.querySelector('.loading-bar');
  21. var progress = document.querySelector('.loading-bar .progress');
  22. var timer = null;
  23. // 文档解析功能相关
  24. var existIndexPage = true; // 是否存在索引页
  25. var url = new URL(window.location.href);
  26. var params = new URLSearchParams(url.search);
  27. var pValue = formatPath(params.get('p'));
  28. // Pjax 相关
  29. let pjax;
  30. // 自定义 PJAX 相关
  31. const existPushState = (history && history.pushState);
  32. // 初始化加载动画相关元素
  33. function initAni() {
  34. loadingBar = document.querySelector('.loading-bar');
  35. progress = document.querySelector('.loading-bar .progress');
  36. }
  37. // 开始加载动画
  38. function startLoad() {
  39. var loadingBarWidth = 20;
  40. var MAX_LOADING_WIDTH = 95;
  41. loadingBar.classList.add('loading');
  42. progress.style.width = loadingBarWidth + '%';
  43. clearInterval(timer);
  44. timer = setInterval(function () {
  45. loadingBarWidth += 3;
  46. if (loadingBarWidth > MAX_LOADING_WIDTH) {
  47. loadingBarWidth = MAX_LOADING_WIDTH;
  48. }
  49. progress.style.width = loadingBarWidth + '%';
  50. }, 500);
  51. }
  52. // 停止加载动画
  53. function endLoad() {
  54. clearInterval(timer);
  55. progress.style.width = '100%';
  56. loadingBar.classList.remove('loading');
  57. setTimeout(function () {
  58. progress.style.width = 0;
  59. }, 400);
  60. }
  61. // 初始化 PJAX
  62. function initPjax() {
  63. try {
  64. const Pjax = window.Pjax || function () { };
  65. pjax = new Pjax({
  66. selectors: [
  67. 'head title',
  68. '#c',
  69. '.pjax-reload'
  70. ],
  71. cacheBust: false
  72. });
  73. } catch (e) {
  74. console.error('PJAX 初始化出错:' + e);
  75. }
  76. }
  77. // translate-js 翻译系统
  78. //// 初始化翻译系统
  79. function initTranslate() {
  80. try {
  81. translate.service.use('client.edge');
  82. translate.request.listener.start();
  83. translate.setAutoDiscriminateLocalLanguage();
  84. translate.language.setUrlParamControl();
  85. translate.ignore.class.push('notTranslate');
  86. translate.execute();
  87. } catch (e) {
  88. console.error('翻译系统出错:' + e);
  89. }
  90. }
  91. // 初始化返回按钮
  92. function initBackBtn() {
  93. try {
  94. document.querySelector('nav').addEventListener('click', function (event) {
  95. if (event.target.closest('#back-btn')) {
  96. try {
  97. window.history.back();
  98. } catch (e) {
  99. console.error('返回按钮出错:' + e);
  100. }
  101. }
  102. });
  103. } catch (e) {
  104. console.error('初始化返回按钮出错:' + e);
  105. }
  106. }
  107. // Darkmode.js 深色模式
  108. function initDarkmode() {
  109. try {
  110. const Darkmode = window.Darkmode || function () {
  111. console.warn('Darkmode.js 不存在?');
  112. return;
  113. };
  114. var darkmode = new Darkmode();
  115. document.querySelector('nav').addEventListener('click', function (event) {
  116. if (event.target.closest('#dark_b')) {
  117. try {
  118. darkmode.toggle();
  119. } catch (e) {
  120. console.error('Darkmode.js 出错:' + e);
  121. }
  122. }
  123. });
  124. } catch (e) {
  125. console.error('初始化 Darkmode.js 出错:' + e);
  126. }
  127. }
  128. //// 检查并提示翻译
  129. function infoTranslate() {
  130. try {
  131. var currentLang = translate.language.getCurrent();
  132. var originalLang = translate.language.getLocal();
  133. if (currentLang !== originalLang) {
  134. const translateInfo = document.querySelectorAll('#translate-info');
  135. if (translateInfo) {
  136. translateInfo.forEach(function (item) {
  137. item.remove();
  138. })
  139. }
  140. // 获取 #c 元素
  141. var mainElements = document.querySelector('#c');
  142. var bq = document.createElement('blockquote');
  143. bq.id = 'translate-info';
  144. var infoBody = document.createElement('p');
  145. infoBody.innerText = '当前页面为翻译版本。';
  146. var msgBody = document.createElement('p');
  147. var langIcon = document.createElement('i');
  148. langIcon.classList.add('fa');
  149. langIcon.classList.add('fa-language');
  150. var switchButton = document.createElement('a');
  151. switchButton.href = 'javascript:;';
  152. switchButton.title = '切换到原语言';
  153. switchButton.innerText = ' 切换到文章原本的语言';
  154. msgBody.appendChild(langIcon);
  155. msgBody.appendChild(switchButton);
  156. bq.appendChild(infoBody);
  157. bq.appendChild(msgBody);
  158. mainElements.insertBefore(bq, mainElements.children[1]);
  159. switchButton.addEventListener('click', function () {
  160. translate.changeLanguage(originalLang);
  161. });
  162. }
  163. } catch (e) {
  164. console.error('检查并提示翻译出错:' + e);
  165. }
  166. }
  167. //// 刷新翻译
  168. function refreshTranslate() {
  169. try {
  170. translate.selectLanguageTag.refreshRender();
  171. } catch (e) {
  172. console.error('刷新翻译出错:' + e);
  173. }
  174. }
  175. // 更新 p 值
  176. function updatePValue() {
  177. url = new URL(window.location.href);
  178. params = new URLSearchParams(url.search);
  179. pValue = formatPath(params.get('p'));
  180. }
  181. // 获取文件所在目录
  182. function getDirectory(path) {
  183. path = new URL(path, 'http://example.com').pathname;
  184. const lastSlashIndex = path.lastIndexOf('/');
  185. return path.substring(0, lastSlashIndex) + '/';
  186. }
  187. // 获取 markdown.html 的当前路径
  188. function getPath() {
  189. return getDirectory(window.location);
  190. }
  191. // 将 ?p= 后面的值转换为绝对路径
  192. function decodePath(path) {
  193. const url = new URL(path);
  194. const params = new URLSearchParams(url.search);
  195. return params.get('p');
  196. }
  197. // 判断路径是否为相对路径
  198. const isRelativePath = (path) => !path.startsWith('/');
  199. // 判断路径是否在本域下
  200. const _isSameDomain = (url) => {
  201. const currentDomain = window.location.origin;
  202. const targetDomain = new URL(url).origin;
  203. return currentDomain === targetDomain;
  204. }
  205. // 根据指定文件读取并解析
  206. const showMarkdown = async (path, response = null) => {
  207. try {
  208. if (!response) {
  209. response = await fetch(path);
  210. if (!response.ok) {
  211. throw new Error('请求失败');
  212. }
  213. }
  214. const markdown = await response.text();
  215. var content = marked.parse(markdown);
  216. content = `<blockquote><code>${decodeURIComponent(path)}</code></blockquote>${content}`;
  217. const parser = new DOMParser();
  218. const doc = parser.parseFromString(content, 'text/html');
  219. const links = doc.querySelectorAll('a[href$=".md"]');
  220. links.forEach(link => {
  221. var href = link.getAttribute('href');
  222. /*
  223. 如果 href 是相对路径,则在其前加上当前读取的文档的绝对路径;
  224. 如果不存在当前读取的文档(默认读取的 index.md 或 README.md),
  225. 则在其前加上 markdown.html 的绝对路径。
  226. */
  227. if (isRelativePath(href)) {
  228. if (pValue) {
  229. href = getDirectory(path) + href;
  230. } else {
  231. href = getPath() + href;
  232. }
  233. const absoluteHref = new URL(href, 'http://example.com').pathname;
  234. link.setAttribute('href', `${window.location.pathname}?p=${absoluteHref}`);
  235. } else if (href.startsWith('/')) {
  236. link.setAttribute('href', `${window.location.pathname}?p=${href}`);
  237. }
  238. });
  239. content = doc.body.innerHTML;
  240. document.getElementById('c').innerHTML = content;
  241. const firstH1 = document.querySelector('h1');
  242. document.title = firstH1 ? firstH1.textContent : path;
  243. showMarkdownEnded();
  244. } catch (error) {
  245. document.title = '加载失败'
  246. document.getElementById('c').innerHTML = `<h1>加载失败</h1><p>文档<code>${path}</code>可能不存在。您可以尝试<a href="">重试</a>。</p>`;
  247. console.error(error);
  248. }
  249. };
  250. // 格式化路径
  251. function formatPath(path) {
  252. if (path) {
  253. path = new URL(path, 'http://example.com').pathname;
  254. if (!path.endsWith('.md')) {
  255. if (!path.endsWith('/')) {
  256. path += '/';
  257. }
  258. path += 'index.md';
  259. }
  260. }
  261. return path;
  262. }
  263. // 初始化自定义 PJAX
  264. function initCostumedPjax() {
  265. if (existPushState) {
  266. document.querySelector('#c').addEventListener('click', function (event) {
  267. const link = event.target.closest('a[href$=".md"]');
  268. if (link) {
  269. try {
  270. event.preventDefault();
  271. startLoad();
  272. showMarkdown(decodePath(link.href));
  273. history.pushState({ url: link.href, title: document.title, uid: link.href }, null, link.href);
  274. } catch (e) {
  275. console.error('自定义 PJAX 出错:' + e);
  276. }
  277. }
  278. });
  279. }
  280. }
  281. // 初始化内容
  282. const initContent = async () => {
  283. try {
  284. if (pValue) {
  285. await showMarkdown(pValue);
  286. } else {
  287. if (existIndexPage) {
  288. const response = await fetch(getPath() + 'index.md');
  289. if (response.ok) {
  290. await showMarkdown(getPath() + 'index.md', response);
  291. } else {
  292. existIndexPage = false;
  293. await showMarkdown(getPath() + 'README.md');
  294. }
  295. } else {
  296. await showMarkdown(getPath() + 'README.md');
  297. }
  298. }
  299. } catch (error) {
  300. console.error(error);
  301. }
  302. };
  303. // 初始化返回首页按钮
  304. function initHomeBtn() {
  305. const homeBtn = document.getElementById('to-home');
  306. homeBtn.href = window.location.pathname;
  307. }
  308. // 初始化
  309. function initialize() {
  310. initDarkmode(); //// 初始化深色模式
  311. initBackBtn(); //// 初始化返回按钮
  312. initHomeBtn(); //// 初始化返回首页按钮
  313. initContent(); //// 初始化内容
  314. initTranslate(); //// 初始化页面翻译
  315. initPjax(); //// 初始化 Pjax
  316. initCostumedPjax(); //// 初始化自定义 PJAX
  317. initAni(); //// 初始化加载动画
  318. if (existPushState) {
  319. history.pushState({ url: window.location.href, title: document.title, uid: window.location.href }, null, window.location.href);
  320. }
  321. }
  322. // 触发器
  323. //// 网页加载完毕后触发
  324. window.addEventListener('DOMContentLoaded', () => initialize());
  325. //// 翻译执行完成后触发
  326. // translate.listener.renderTaskFinish = function (_task) {
  327. // }
  328. //// Pjax 开始时执行的函数
  329. document.addEventListener('pjax:send', function () {
  330. startLoad();
  331. });
  332. //// 监听 Pjax 完成后,重新加载
  333. document.addEventListener('pjax:complete', function () {
  334. updatePValue();
  335. initContent();
  336. translate.execute(); // 手动触发翻译,但是理论上 translate-js 会监听页面变化
  337. initCostumedPjax(); //// 初始化自定义 PJAX
  338. })
  339. //// 文档内容加载完毕时触发
  340. function showMarkdownEnded() {
  341. refreshTranslate();
  342. infoTranslate(); //// 检查并提示翻译
  343. translate.execute(); // 手动触发翻译,但是理论上 translate-js 会监听页面变化
  344. endLoad();
  345. }
  346. })();
  347. </script>
  348. <title>加载中</title>
  349. <style>
  350. /* 全局 */
  351. * {
  352. font-family: 'Exo', sans-serif;
  353. text-decoration: none;
  354. }
  355. :root {
  356. background-color: #e6e6e6;
  357. }
  358. ::selection {
  359. background-color: #DCEDC8;
  360. }
  361. body {
  362. margin: 0;
  363. }
  364. a {
  365. color: #66BB6A;
  366. cursor: pointer;
  367. text-decoration: none;
  368. }
  369. a:hover {
  370. color: #2E7D32;
  371. }
  372. /* 背景 */
  373. .background {
  374. position: fixed;
  375. left: 0;
  376. top: 0;
  377. height: 100%;
  378. width: 100%;
  379. z-index: -1;
  380. background-color: #e6e6e6;
  381. }
  382. /* 导航栏 */
  383. nav {
  384. position: fixed;
  385. top: 0;
  386. right: 0;
  387. width: min-content;
  388. background-color: #616161;
  389. display: flex;
  390. justify-content: space-between;
  391. align-items: center;
  392. }
  393. nav a {
  394. color: #FAFAFA;
  395. padding: .5rem;
  396. transition:
  397. color .2s,
  398. background-color .2s;
  399. }
  400. nav a:hover {
  401. color: #FFF;
  402. background-color: #212121;
  403. }
  404. /* 主要内容 */
  405. #c {
  406. margin: 3rem auto;
  407. width: 100%;
  408. max-width: 950px;
  409. min-height: 300px;
  410. background-color: #FAFAFA;
  411. color: #212121;
  412. box-sizing: border-box;
  413. overflow-x: hidden;
  414. padding: 30px;
  415. box-shadow: 0 2px 6px rgba(100, 100, 100, .3);
  416. }
  417. #c img,
  418. #c video,
  419. #c iframe,
  420. #c object,
  421. #c embed,
  422. #c svg,
  423. #c canvas,
  424. #c audio {
  425. max-width: 100%;
  426. }
  427. /* Markdown 样式 */
  428. #c a {
  429. transition:
  430. color .2s,
  431. background-color .2s;
  432. }
  433. #c a:hover {
  434. background-color: rgba(255, 255, 255, 0.1);
  435. }
  436. #c pre {
  437. background-color: rgba(0, 0, 0, 0.05);
  438. padding: .4rem;
  439. border-radius: .5rem;
  440. overflow-y: scroll;
  441. }
  442. #c code {
  443. font-family: "cousine nerd font mono", monospace;
  444. }
  445. #c p code {
  446. background-color: rgba(0, 0, 0, 0.05);
  447. font-size: small;
  448. padding: .2rem;
  449. border-radius: .1rem;
  450. }
  451. #c blockquote {
  452. background-color: rgba(0, 0, 0, 0.05);
  453. font-size: small;
  454. padding: .4rem;
  455. border-left: 5px solid rgba(0, 0, 0, 0.1);
  456. }
  457. #c table {
  458. border-spacing: 0;
  459. border-radius: .1rem;
  460. width: 100%;
  461. border-collapse: separate;
  462. border: .1rem solid rgba(0, 0, 0, 0.05);
  463. }
  464. #c table td,
  465. #c table th {
  466. border-right: .1rem solid rgba(0, 0, 0, 0.05);
  467. }
  468. #c table td,
  469. #c table th {
  470. border-top: .1rem solid rgba(0, 0, 0, 0.05);
  471. }
  472. #c table td:last-child,
  473. #c th:last-child {
  474. border-right-color: transparent;
  475. }
  476. #c table thead tr:first-child>* {
  477. border-top: .1rem solid transparent;
  478. }
  479. /* 加载进度条 */
  480. .loading-bar {
  481. position: fixed;
  482. top: 0;
  483. left: 0;
  484. z-index: 99999;
  485. opacity: 0;
  486. transition: opacity .4s linear;
  487. .progress {
  488. position: fixed;
  489. top: 0;
  490. left: 0;
  491. width: 0;
  492. height: 4px;
  493. background-color: #FFF;
  494. box-shadow: 0 0 10px rgba(102, 187, 106, .7);
  495. }
  496. &.loading {
  497. opacity: 1;
  498. transition: none;
  499. .progress {
  500. transition: width .4s ease;
  501. }
  502. }
  503. }
  504. /* 翻译按钮相关 */
  505. .translateSelectLanguage {
  506. background-color: #616161;
  507. color: inherit;
  508. z-index: 10;
  509. width: 17%;
  510. height: 100%;
  511. opacity: 0;
  512. cursor: pointer;
  513. position: absolute;
  514. top: 0;
  515. left: 50%;
  516. }
  517. /* 深色模式相关 */
  518. .darkmode--activated img,
  519. .darkmode--activated svg {
  520. isolation: isolate;
  521. mix-blend-mode: difference
  522. }
  523. .darkmode-layer,
  524. .darkmode-toggle {
  525. z-index: 2
  526. }
  527. /* 小屏响应式优化 */
  528. @media only screen and (max-width: 950px) {
  529. html,
  530. body,
  531. #c {
  532. height: 100%;
  533. }
  534. #c {
  535. overflow-y: scroll;
  536. min-width: 256px;
  537. top: 0;
  538. margin: 0 0;
  539. }
  540. }
  541. </style>
  542. </head>
  543. <body>
  544. <!-- 导航栏 -->
  545. <nav>
  546. <a title="返回" id="back-btn"><i class="fa fa-backward"></i></a>
  547. <a title="刷新" href=""><i class="fa fa-arrows-rotate"></i></a>
  548. <a title="主页" id="to-home"><i class="fa fa-home"></i></a>
  549. <a title="更改语言" id="translate"><i class="fa fa-language"></i></a>
  550. <a title="切换显示模式" id="dark_b" href="javascript:;"><i class="fa fa-adjust"></i></a>
  551. <a title="关于" href="https://github.com/PJ-568/markdown.html/"><i class="fa fa-info-circle"></i></a>
  552. </nav>
  553. <!-- 内容主体 -->
  554. <div id="c">
  555. <h1>加载中……</h1>
  556. <p>请稍等。</p>
  557. </div>
  558. <!-- 加载动画 -->
  559. <div class="loading-bar">
  560. <div class="progress"></div>
  561. </div>
  562. <div class="background"></div>
  563. <script src="//lf6-cdn-tos.bytecdntp.com/cdn/expire-1-y/instant.page/5.1.0/instantpage.min.js"
  564. type="text/javascript"></script>
  565. </body>
  566. </html>