index.html 18 KB

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