index.html 18 KB

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