index.html 17 KB

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