index.html 16 KB

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