1
0

index.html 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628
  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.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 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. path += 'index.md';
  254. }
  255. }
  256. return path;
  257. }
  258. // 初始化自定义 PJAX
  259. function initCostumedPjax() {
  260. if (existPushState) {
  261. document.querySelector('#c').addEventListener('click', function (event) {
  262. const link = event.target.closest('a[href$=".md"]');
  263. if (link) {
  264. try {
  265. event.preventDefault();
  266. startLoad();
  267. showMarkdown(decodePath(link.href));
  268. history.pushState({ url: link.href, title: document.title, uid: link.href }, null, link.href);
  269. } catch (e) {
  270. console.error('自定义 PJAX 出错:' + e);
  271. }
  272. }
  273. });
  274. }
  275. }
  276. // 初始化内容
  277. const initContent = async () => {
  278. try {
  279. if (pValue) {
  280. await showMarkdown(pValue);
  281. } else {
  282. const response = await fetch(getPath() + 'index.md');
  283. if (response.ok) {
  284. await showMarkdown(getPath() + 'index.md', response);
  285. } else {
  286. await showMarkdown(getPath() + 'README.md');
  287. }
  288. }
  289. } catch (error) {
  290. console.error(error);
  291. }
  292. };
  293. // 初始化返回首页按钮
  294. function initHomeBtn() {
  295. const homeBtn = document.getElementById('to-home');
  296. homeBtn.href = window.location.pathname;
  297. }
  298. // 初始化
  299. function initialize() {
  300. initDarkmode(); //// 初始化深色模式
  301. initBackBtn(); //// 初始化返回按钮
  302. initHomeBtn(); //// 初始化返回首页按钮
  303. initContent(); //// 初始化内容
  304. initTranslate(); //// 初始化页面翻译
  305. initPjax(); //// 初始化 Pjax
  306. initCostumedPjax(); //// 初始化自定义 PJAX
  307. initAni(); //// 初始化加载动画
  308. if (existPushState) {
  309. history.pushState({ url: window.location.href, title: document.title, uid: window.location.href }, null, window.location.href);
  310. }
  311. }
  312. // 触发器
  313. //// 网页加载完毕后触发
  314. window.addEventListener('DOMContentLoaded', () => initialize());
  315. //// 翻译执行完成后触发
  316. // translate.listener.renderTaskFinish = function (_task) {
  317. // }
  318. //// Pjax 开始时执行的函数
  319. document.addEventListener('pjax:send', function () {
  320. startLoad();
  321. });
  322. //// 监听 Pjax 完成后,重新加载
  323. document.addEventListener('pjax:complete', function () {
  324. updatePValue();
  325. initContent();
  326. translate.execute(); // 手动触发翻译,但是理论上 translate-js 会监听页面变化
  327. initCostumedPjax(); //// 初始化自定义 PJAX
  328. })
  329. //// 文档内容加载完毕时触发
  330. function showMarkdownEnded() {
  331. refreshTranslate();
  332. infoTranslate(); //// 检查并提示翻译
  333. translate.execute(); // 手动触发翻译,但是理论上 translate-js 会监听页面变化
  334. endLoad();
  335. }
  336. })();
  337. </script>
  338. <title>加载中</title>
  339. <style>
  340. /* 全局 */
  341. * {
  342. font-family: 'Exo', sans-serif;
  343. text-decoration: none;
  344. }
  345. ::selection {
  346. background-color: #DCEDC8;
  347. }
  348. body {
  349. margin: 0;
  350. }
  351. a {
  352. color: #66BB6A;
  353. cursor: pointer;
  354. text-decoration: none;
  355. }
  356. a:hover {
  357. color: #2E7D32;
  358. }
  359. /* 背景 */
  360. .background {
  361. position: fixed;
  362. left: 0;
  363. top: 0;
  364. height: 100%;
  365. width: 100%;
  366. z-index: -1;
  367. background-color: #e6e6e6;
  368. }
  369. /* 导航栏 */
  370. nav {
  371. position: fixed;
  372. top: 0;
  373. right: 0;
  374. width: min-content;
  375. background-color: #616161;
  376. display: flex;
  377. justify-content: space-between;
  378. align-items: center;
  379. }
  380. nav a {
  381. color: #FAFAFA;
  382. padding: .5rem;
  383. transition:
  384. color .2s,
  385. background-color .2s;
  386. }
  387. nav a:hover {
  388. color: #FFF;
  389. background-color: #212121;
  390. }
  391. /* 主要内容 */
  392. #c {
  393. margin: 3rem auto;
  394. width: 100%;
  395. max-width: 950px;
  396. min-height: 300px;
  397. background-color: #FAFAFA;
  398. color: #212121;
  399. box-sizing: border-box;
  400. overflow-x: hidden;
  401. padding: 30px;
  402. box-shadow: 0 2px 6px rgba(100, 100, 100, .3);
  403. }
  404. #c img,
  405. #c video,
  406. #c iframe,
  407. #c object,
  408. #c embed,
  409. #c svg,
  410. #c canvas,
  411. #c audio {
  412. max-width: 100%;
  413. }
  414. /* Markdown 样式 */
  415. #c a {
  416. transition:
  417. color .2s,
  418. background-color .2s;
  419. }
  420. #c a:hover {
  421. background-color: rgba(255, 255, 255, 0.1);
  422. }
  423. #c pre {
  424. background-color: rgba(0, 0, 0, 0.05);
  425. padding: .4rem;
  426. border-radius: .5rem;
  427. overflow-y: scroll;
  428. }
  429. #c code {
  430. font-family: "cousine nerd font mono", monospace;
  431. }
  432. #c p code {
  433. background-color: rgba(0, 0, 0, 0.05);
  434. font-size: small;
  435. padding: .2rem;
  436. border-radius: .1rem;
  437. }
  438. #c blockquote {
  439. background-color: rgba(0, 0, 0, 0.05);
  440. font-size: small;
  441. padding: .4rem;
  442. border-left: 5px solid rgba(0, 0, 0, 0.1);
  443. }
  444. #c table {
  445. border-spacing: 0;
  446. border-radius: .1rem;
  447. width: 100%;
  448. border-collapse: separate;
  449. border: .1rem solid rgba(0, 0, 0, 0.05);
  450. }
  451. #c table td,
  452. #c table th {
  453. border-right: .1rem solid rgba(0, 0, 0, 0.05);
  454. }
  455. #c table td,
  456. #c table th {
  457. border-top: .1rem solid rgba(0, 0, 0, 0.05);
  458. }
  459. #c table td:last-child,
  460. #c th:last-child {
  461. border-right-color: transparent;
  462. }
  463. #c table thead tr:first-child>* {
  464. border-top: .1rem solid transparent;
  465. }
  466. /* 加载进度条 */
  467. .loading-bar {
  468. position: fixed;
  469. top: 0;
  470. left: 0;
  471. z-index: 99999;
  472. opacity: 0;
  473. transition: opacity .4s linear;
  474. .progress {
  475. position: fixed;
  476. top: 0;
  477. left: 0;
  478. width: 0;
  479. height: 4px;
  480. background-color: #FFF;
  481. box-shadow: 0 0 10px rgba(102, 187, 106, .7);
  482. }
  483. &.loading {
  484. opacity: 1;
  485. transition: none;
  486. .progress {
  487. transition: width .4s ease;
  488. }
  489. }
  490. }
  491. /* 翻译按钮相关 */
  492. .translateSelectLanguage {
  493. background-color: #616161;
  494. color: inherit;
  495. z-index: 10;
  496. width: 17%;
  497. height: 100%;
  498. opacity: 0;
  499. cursor: pointer;
  500. position: absolute;
  501. top: 0;
  502. left: 50%;
  503. }
  504. /* 深色模式相关 */
  505. .darkmode--activated img,
  506. .darkmode--activated svg {
  507. isolation: isolate;
  508. mix-blend-mode: difference
  509. }
  510. .darkmode-layer,
  511. .darkmode-toggle {
  512. z-index: 2
  513. }
  514. /* 小屏响应式优化 */
  515. @media only screen and (max-width: 950px) {
  516. html,
  517. body,
  518. #c {
  519. height: 100%;
  520. }
  521. #c {
  522. overflow-y: scroll;
  523. min-width: 256px;
  524. top: 0;
  525. margin: 0 0;
  526. }
  527. }
  528. </style>
  529. </head>
  530. <body>
  531. <div class="background"></div>
  532. <!-- 导航栏 -->
  533. <nav>
  534. <a title="返回" id="back-btn"><i class="fa fa-backward"></i></a>
  535. <a title="刷新" href=""><i class="fa fa-arrows-rotate"></i></a>
  536. <a title="主页" id="to-home"><i class="fa fa-home"></i></a>
  537. <a title="更改语言" id="translate"><i class="fa fa-language"></i></a>
  538. <a title="切换显示模式" id="dark_b" href="javascript:;"><i class="fa fa-adjust"></i></a>
  539. <a title="关于" href="https://github.com/PJ-568/markdown.html/"><i class="fa fa-info-circle"></i></a>
  540. </nav>
  541. <!-- 内容主体 -->
  542. <div id="c">
  543. <h1>加载中……</h1>
  544. <p>请稍等。</p>
  545. </div>
  546. <!-- 加载动画 -->
  547. <div class="loading-bar">
  548. <div class="progress"></div>
  549. </div>
  550. <script src="//lf6-cdn-tos.bytecdntp.com/cdn/expire-1-y/instant.page/5.1.0/instantpage.min.js"
  551. type="text/javascript"></script>
  552. </body>
  553. </html>