1
0

index.html 18 KB

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