1
0

index.html 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475
  1. <!doctype html>
  2. <html lang="zh-Hans">
  3. <!-- 作者:PJ568 -->
  4. <!-- 项目地址:https://github.com/PJ-568/markdown.html/ -->
  5. <!-- v 0.0.6 -->
  6. <head>
  7. <meta charset="utf-8">
  8. <meta name="description" content="HTML read markdown">
  9. <meta name="viewport" content="width=256, initial-scale=1">
  10. <script defer src="//lib.baomitu.com/marked/4.0.2/marked.min.js" type="text/javascript"></script>
  11. <script src="//lib.baomitu.com/pjax/0.2.8/pjax.min.js" type="text/javascript"></script>
  12. <script src="//lib.baomitu.com/translate.js/3.7.2/translate.js" type="text/javascript"></script>
  13. <script src="//lib.baomitu.com/Darkmode.js/1.5.7/darkmode-js.min.js" type="text/javascript"></script>
  14. <link rel="stylesheet" href="https://lf6-cdn-tos.bytecdntp.com/cdn/expire-1-y/font-awesome/6.0.0/css/all.min.css">
  15. <script>
  16. // 使用 IIFE 包裹以避免全局变量污染
  17. (function () {
  18. // 加载动画相关
  19. var loadingBar = document.querySelector('.loading-bar');
  20. var progress = document.querySelector('.loading-bar .progress');
  21. var timer = null;
  22. // 文档解析功能相关
  23. var url = new URL(window.location.href);
  24. var params = new URLSearchParams(url.search);
  25. var pValue = params.get('p');
  26. // Pjax 相关
  27. let pjax;
  28. // 初始化加载动画相关元素
  29. function initAni() {
  30. loadingBar = document.querySelector('.loading-bar');
  31. progress = document.querySelector('.loading-bar .progress');
  32. }
  33. // 停止加载动画
  34. function endLoad() {
  35. clearInterval(timer);
  36. progress.style.width = '100%';
  37. loadingBar.classList.remove('loading');
  38. setTimeout(function () {
  39. progress.style.width = 0;
  40. }, 400);
  41. }
  42. // 初始化 PJAX
  43. function initPjax() {
  44. try {
  45. const Pjax = window.Pjax || function () { };
  46. pjax = new Pjax({
  47. selectors: [
  48. 'head meta',
  49. 'head title',
  50. '#c',
  51. '.pjax-reload'
  52. ],
  53. cacheBust: false
  54. });
  55. } catch (e) {
  56. console.error('PJAX 初始化出错:' + e);
  57. }
  58. }
  59. // translate-js 翻译系统
  60. //// 初始化翻译系统
  61. function initTranslate() {
  62. try {
  63. translate.service.use('client.edge');
  64. translate.listener.start();
  65. translate.setAutoDiscriminateLocalLanguage();
  66. translate.language.setUrlParamControl();
  67. translate.ignore.class.push('notTranslate');
  68. translate.execute();
  69. } catch (e) {
  70. console.error('翻译系统出错:' + e);
  71. }
  72. }
  73. // Darkmode.js 深色模式
  74. function initDarkmode() {
  75. try {
  76. const Darkmode = window.Darkmode || function () {
  77. console.warn('Darkmode.js 不存在?');
  78. return;
  79. };
  80. var darkmode = new Darkmode();
  81. document.body.addEventListener('click', function (event) {
  82. if (event.target.closest('#dark_b')) {
  83. try {
  84. darkmode.toggle();
  85. } catch (e) {
  86. console.error('Darkmode.js 出错:' + e);
  87. }
  88. }
  89. });
  90. } catch (e) {
  91. console.error('初始化 Darkmode.js 出错:' + e);
  92. }
  93. }
  94. // 更新 p 值
  95. function updatePValue() {
  96. url = new URL(window.location.href);
  97. params = new URLSearchParams(url.search);
  98. pValue = params.get('p');
  99. }
  100. // 获取文件所在目录
  101. function getDirectory(path) {
  102. path = new URL(path, 'http://example.com').pathname;
  103. const lastSlashIndex = path.lastIndexOf('/');
  104. return path.substring(0, lastSlashIndex) + '/';
  105. }
  106. // 获取 markdown.html 的当前路径
  107. function getPath() {
  108. return getDirectory(window.location);
  109. }
  110. // 判断路径是否为相对路径
  111. const isRelativePath = (path) => !path.startsWith('/');
  112. // 根据指定文件读取并解析
  113. const showMarkdown = async (path) => {
  114. try {
  115. const absolutePath = new URL(path, 'http://example.com').pathname;
  116. const response = await fetch(absolutePath);
  117. if (response.ok) {
  118. const markdown = await response.text();
  119. var content = marked.parse(markdown);
  120. content = `<blockquote><code>${decodeURIComponent(absolutePath)}</code></blockquote>${content}`;
  121. const parser = new DOMParser();
  122. const doc = parser.parseFromString(content, 'text/html');
  123. const links = doc.querySelectorAll('a[href$=".md"]');
  124. links.forEach(link => {
  125. var href = link.getAttribute('href');
  126. /*
  127. 如果 href 是相对路径,则在其前加上当前读取的文档的绝对路径;
  128. 如果不存在当前读取的文档(默认读取的 index.md 或 README.md),
  129. 则在其前加上 markdown.html 的绝对路径。
  130. */
  131. if (isRelativePath(href)) {
  132. if (pValue) {
  133. href = getDirectory(absolutePath) + href;
  134. } else {
  135. href = getPath() + href;
  136. }
  137. }
  138. const absoluteHref = new URL(href, 'http://example.com').pathname;
  139. link.setAttribute('href', `${window.location.pathname}?p=${absoluteHref}`);
  140. });
  141. content = doc.body.innerHTML;
  142. document.getElementById('c').innerHTML = content;
  143. const firstH1 = document.querySelector('h1');
  144. document.title = firstH1 ? firstH1.textContent : absolutePath;
  145. } else {
  146. throw new Error('请求失败');
  147. }
  148. } catch (error) {
  149. document.getElementById('c').innerHTML = '<h1>加载失败</h1><p>可<a href="">重试</a>。</p>';
  150. console.error(error);
  151. }
  152. };
  153. // 初始化内容
  154. const initContent = async () => {
  155. try {
  156. if (pValue) {
  157. await showMarkdown(pValue);
  158. } else {
  159. const response = await fetch(getPath() + 'index.md');
  160. if (response.ok) {
  161. await showMarkdown(getPath() + 'index.md');
  162. } else {
  163. await showMarkdown(getPath() + 'README.md');
  164. }
  165. }
  166. } catch (error) {
  167. console.error(error);
  168. }
  169. };
  170. // 初始化返回首页按钮
  171. function initHomeBtn() {
  172. const homeBtn = document.getElementById('to-home');
  173. homeBtn.href = window.location.pathname;
  174. }
  175. // 初始化
  176. function initialize() {
  177. initContent(); //// 初始化内容
  178. initHomeBtn(); //// 初始化返回首页按钮
  179. initDarkmode(); //// 初始化深色模式
  180. initTranslate(); //// 初始化页面翻译
  181. initPjax(); //// 初始化 Pjax
  182. initAni(); //// 初始化加载动画
  183. }
  184. // 触发器
  185. //// 网页加载完毕后触发
  186. window.addEventListener('DOMContentLoaded', () => initialize());
  187. //// 翻译执行完成后触发
  188. // translate.listener.renderTaskFinish = function (_task) {
  189. // }
  190. //// Pjax 开始时执行的函数
  191. document.addEventListener('pjax:send', function () {
  192. var loadingBarWidth = 20;
  193. var MAX_LOADING_WIDTH = 95;
  194. loadingBar.classList.add('loading');
  195. progress.style.width = loadingBarWidth + '%';
  196. clearInterval(timer);
  197. timer = setInterval(function () {
  198. loadingBarWidth += 3;
  199. if (loadingBarWidth > MAX_LOADING_WIDTH) {
  200. loadingBarWidth = MAX_LOADING_WIDTH;
  201. }
  202. progress.style.width = loadingBarWidth + '%';
  203. }, 500);
  204. });
  205. //// 监听 Pjax 完成后,重新加载
  206. document.addEventListener('pjax:complete', function () {
  207. updatePValue();
  208. initContent();
  209. translate.execute(); // 手动触发翻译,但是理论上 translate-js 会监听页面变化
  210. endLoad();
  211. })
  212. })();
  213. </script>
  214. <title>加载中</title>
  215. <style>
  216. /* 全局 */
  217. * {
  218. font-family: 'Exo', sans-serif;
  219. text-decoration: none;
  220. }
  221. ::selection {
  222. background-color: #DCEDC8;
  223. }
  224. body {
  225. margin: 0;
  226. }
  227. a {
  228. color: #66BB6A;
  229. cursor: pointer;
  230. text-decoration: none;
  231. }
  232. a:hover {
  233. color: #2E7D32;
  234. }
  235. /* 背景 */
  236. .background {
  237. position: fixed;
  238. left: 0;
  239. top: 0;
  240. height: 100%;
  241. width: 100%;
  242. z-index: -1;
  243. background-color: #e6e6e6;
  244. }
  245. /* 导航栏 */
  246. nav {
  247. position: fixed;
  248. top: 0;
  249. right: 0;
  250. width: min-content;
  251. background-color: #616161;
  252. display: flex;
  253. justify-content: space-between;
  254. align-items: center;
  255. }
  256. nav a {
  257. color: #FAFAFA;
  258. padding: .5rem;
  259. transition:
  260. color .2s,
  261. background-color .2s;
  262. }
  263. nav a:hover {
  264. color: #FFF;
  265. background-color: #212121;
  266. }
  267. /* 主要内容 */
  268. #c {
  269. margin: 3rem auto;
  270. width: 100%;
  271. max-width: 950px;
  272. min-height: 300px;
  273. background-color: #FAFAFA;
  274. color: #212121;
  275. box-sizing: border-box;
  276. overflow-x: hidden;
  277. padding: 30px;
  278. box-shadow: 0 2px 6px rgba(100, 100, 100, .3);
  279. }
  280. /* Markdown 样式 */
  281. #c a {
  282. transition:
  283. color .2s,
  284. background-color .2s;
  285. }
  286. #c a:hover {
  287. background-color: rgba(255, 255, 255, 0.1);
  288. }
  289. #c pre {
  290. background-color: rgba(0, 0, 0, 0.05);
  291. padding: .4rem;
  292. border-radius: .5rem;
  293. overflow-y: scroll;
  294. }
  295. #c code {
  296. font-family: "cousine nerd font mono", monospace;
  297. }
  298. #c p code {
  299. background-color: rgba(0, 0, 0, 0.05);
  300. font-size: small;
  301. padding: .2rem;
  302. border-radius: .1rem;
  303. }
  304. #c blockquote {
  305. background-color: rgba(0, 0, 0, 0.05);
  306. font-size: small;
  307. padding: .4rem;
  308. border-left: 5px solid rgba(0, 0, 0, 0.1);
  309. }
  310. #c table {
  311. border-spacing: 0;
  312. border-radius: .1rem;
  313. width: 100%;
  314. border-collapse: separate;
  315. border: .1rem solid rgba(0, 0, 0, 0.05);
  316. }
  317. #c table td,
  318. #c table th {
  319. border-right: .1rem solid rgba(0, 0, 0, 0.05);
  320. }
  321. #c table td,
  322. #c table th {
  323. border-top: .1rem solid rgba(0, 0, 0, 0.05);
  324. }
  325. #c table td:last-child,
  326. #c th:last-child {
  327. border-right-color: transparent;
  328. }
  329. #c table thead tr:first-child>* {
  330. border-top: .1rem solid transparent;
  331. }
  332. /* 加载进度条 */
  333. .loading-bar {
  334. position: fixed;
  335. top: 0;
  336. left: 0;
  337. z-index: 99999;
  338. opacity: 0;
  339. transition: opacity .4s linear;
  340. .progress {
  341. position: fixed;
  342. top: 0;
  343. left: 0;
  344. width: 0;
  345. height: 4px;
  346. background-color: #FFF;
  347. box-shadow: 0 0 10px rgba(102, 187, 106, .7);
  348. }
  349. &.loading {
  350. opacity: 1;
  351. transition: none;
  352. .progress {
  353. transition: width .4s ease;
  354. }
  355. }
  356. }
  357. /* 翻译按钮相关 */
  358. .translateSelectLanguage {
  359. background-color: #616161;
  360. color: inherit;
  361. z-index: 10;
  362. width: 25%;
  363. height: 100%;
  364. opacity: 0;
  365. cursor: pointer;
  366. position: absolute;
  367. top: 0;
  368. left: 25%;
  369. }
  370. /* 深色模式相关 */
  371. .darkmode--activated img,
  372. .darkmode--activated svg {
  373. isolation: isolate;
  374. mix-blend-mode: difference
  375. }
  376. .darkmode-layer,
  377. .darkmode-toggle {
  378. z-index: 2
  379. }
  380. /* 小屏响应式优化 */
  381. @media only screen and (max-width: 950px) {
  382. html,
  383. body,
  384. #c {
  385. height: 100%;
  386. }
  387. #c {
  388. overflow-y: scroll;
  389. min-width: 256px;
  390. top: 0;
  391. margin: 0 0;
  392. }
  393. }
  394. </style>
  395. </head>
  396. <body>
  397. <div class="background"></div>
  398. <!-- 导航栏 -->
  399. <nav>
  400. <a title="主页" id="to-home"><i class="fa fa-home"></i></a>
  401. <a title="更改语言" id="translate"><i class="fa fa-language"></i></a>
  402. <a title="切换显示模式" id="dark_b" href="javascript:;"><i class="fa fa-adjust"></i></a>
  403. <a title="关于" href="https://github.com/PJ-568/markdown.html/"><i class="fa fa-info-circle"></i></a>
  404. </nav>
  405. <!-- 内容主体 -->
  406. <div id="c">
  407. <h1>加载中……</h1>
  408. <p>请稍等。</p>
  409. </div>
  410. <!-- 加载动画 -->
  411. <div class="loading-bar">
  412. <div class="progress"></div>
  413. </div>
  414. <script src="//lf6-cdn-tos.bytecdntp.com/cdn/expire-1-y/instant.page/5.1.0/instantpage.min.js"
  415. type="text/javascript"></script>
  416. </body>
  417. </html>