index.html 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516
  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.2.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. // 更新 p 值
  110. function updatePValue() {
  111. url = new URL(window.location.href);
  112. params = new URLSearchParams(url.search);
  113. pValue = params.get('p');
  114. }
  115. // 获取文件所在目录
  116. function getDirectory(path) {
  117. path = new URL(path, 'http://example.com').pathname;
  118. const lastSlashIndex = path.lastIndexOf('/');
  119. return path.substring(0, lastSlashIndex) + '/';
  120. }
  121. // 获取 markdown.html 的当前路径
  122. function getPath() {
  123. return getDirectory(window.location);
  124. }
  125. // 将 ?p= 后面的值转换为绝对路径
  126. function decodePath(path) {
  127. const url = new URL(path);
  128. const params = new URLSearchParams(url.search);
  129. return params.get('p');
  130. }
  131. // 判断路径是否为相对路径
  132. const isRelativePath = (path) => !path.startsWith('/');
  133. // 判断路径是否在本域下
  134. const _isSameDomain = (url) => {
  135. const currentDomain = window.location.origin;
  136. const targetDomain = new URL(url).origin;
  137. return currentDomain === targetDomain;
  138. }
  139. // 根据指定文件读取并解析
  140. const showMarkdown = async (path, response = null) => {
  141. try {
  142. const absolutePath = new URL(path, 'http://example.com').pathname;
  143. if (!response) {
  144. response = await fetch(absolutePath);
  145. if (!response.ok) {
  146. throw new Error('请求失败');
  147. }
  148. }
  149. const markdown = await response.text();
  150. var content = marked.parse(markdown);
  151. content = `<blockquote><code>${decodeURIComponent(absolutePath)}</code></blockquote>${content}`;
  152. const parser = new DOMParser();
  153. const doc = parser.parseFromString(content, 'text/html');
  154. const links = doc.querySelectorAll('a[href$=".md"]');
  155. links.forEach(link => {
  156. var href = link.getAttribute('href');
  157. /*
  158. 如果 href 是相对路径,则在其前加上当前读取的文档的绝对路径;
  159. 如果不存在当前读取的文档(默认读取的 index.md 或 README.md),
  160. 则在其前加上 markdown.html 的绝对路径。
  161. */
  162. if (isRelativePath(href)) {
  163. if (pValue) {
  164. href = getDirectory(absolutePath) + href;
  165. } else {
  166. href = getPath() + href;
  167. }
  168. const absoluteHref = new URL(href, 'http://example.com').pathname;
  169. link.setAttribute('href', `${window.location.pathname}?p=${absoluteHref}`);
  170. } else if (href.startsWith('/')) {
  171. link.setAttribute('href', `${window.location.pathname}?p=${href}`);
  172. }
  173. });
  174. content = doc.body.innerHTML;
  175. document.getElementById('c').innerHTML = content;
  176. const firstH1 = document.querySelector('h1');
  177. document.title = firstH1 ? firstH1.textContent : absolutePath;
  178. } catch (error) {
  179. document.getElementById('c').innerHTML = '<h1>加载失败</h1><p><a href="">重试</a></p>';
  180. console.error(error);
  181. }
  182. };
  183. // 初始化自定义链接 Pjax
  184. function _initContentPjax() {
  185. document.querySelector('#c').addEventListener('click', function (event) {
  186. const link = event.target.closest('a[href$=".md"]');
  187. if (link) {
  188. try {
  189. event.preventDefault();
  190. startLoad();
  191. showMarkdown(decodePath(link.href));
  192. history.pushState(null, null, link.href);
  193. endLoad();
  194. } catch (e) {
  195. console.error('自定义 Pjax 出错:' + e);
  196. }
  197. }
  198. });
  199. }
  200. // 初始化内容
  201. const initContent = async () => {
  202. try {
  203. if (pValue) {
  204. await showMarkdown(pValue);
  205. } else {
  206. const response = await fetch(getPath() + 'index.md');
  207. if (response.ok) {
  208. await showMarkdown(getPath() + 'index.md', response);
  209. } else {
  210. await showMarkdown(getPath() + 'README.md');
  211. }
  212. }
  213. } catch (error) {
  214. console.error(error);
  215. }
  216. };
  217. // 初始化返回首页按钮
  218. function initHomeBtn() {
  219. const homeBtn = document.getElementById('to-home');
  220. homeBtn.href = window.location.pathname;
  221. }
  222. // 初始化
  223. function initialize() {
  224. initDarkmode(); //// 初始化深色模式
  225. // initContentPjax(); //// 初始化自定义链接 Pjax
  226. initHomeBtn(); //// 初始化返回首页按钮
  227. initContent(); //// 初始化内容
  228. initTranslate(); //// 初始化页面翻译
  229. initPjax(); //// 初始化 Pjax
  230. initAni(); //// 初始化加载动画
  231. }
  232. // 触发器
  233. //// 网页加载完毕后触发
  234. window.addEventListener('DOMContentLoaded', () => initialize());
  235. //// 翻译执行完成后触发
  236. // translate.listener.renderTaskFinish = function (_task) {
  237. // }
  238. //// Pjax 开始时执行的函数
  239. document.addEventListener('pjax:send', function () {
  240. startLoad();
  241. });
  242. //// 监听 Pjax 完成后,重新加载
  243. document.addEventListener('pjax:complete', function () {
  244. updatePValue();
  245. initContent();
  246. translate.execute(); // 手动触发翻译,但是理论上 translate-js 会监听页面变化
  247. endLoad();
  248. })
  249. })();
  250. </script>
  251. <title>加载中</title>
  252. <style>
  253. /* 全局 */
  254. * {
  255. font-family: 'Exo', sans-serif;
  256. text-decoration: none;
  257. }
  258. ::selection {
  259. background-color: #DCEDC8;
  260. }
  261. body {
  262. margin: 0;
  263. }
  264. a {
  265. color: #66BB6A;
  266. cursor: pointer;
  267. text-decoration: none;
  268. }
  269. a:hover {
  270. color: #2E7D32;
  271. }
  272. /* 背景 */
  273. .background {
  274. position: fixed;
  275. left: 0;
  276. top: 0;
  277. height: 100%;
  278. width: 100%;
  279. z-index: -1;
  280. background-color: #e6e6e6;
  281. }
  282. /* 导航栏 */
  283. nav {
  284. position: fixed;
  285. top: 0;
  286. right: 0;
  287. width: min-content;
  288. background-color: #616161;
  289. display: flex;
  290. justify-content: space-between;
  291. align-items: center;
  292. }
  293. nav a {
  294. color: #FAFAFA;
  295. padding: .5rem;
  296. transition:
  297. color .2s,
  298. background-color .2s;
  299. }
  300. nav a:hover {
  301. color: #FFF;
  302. background-color: #212121;
  303. }
  304. /* 主要内容 */
  305. #c {
  306. margin: 3rem auto;
  307. width: 100%;
  308. max-width: 950px;
  309. min-height: 300px;
  310. background-color: #FAFAFA;
  311. color: #212121;
  312. box-sizing: border-box;
  313. overflow-x: hidden;
  314. padding: 30px;
  315. box-shadow: 0 2px 6px rgba(100, 100, 100, .3);
  316. }
  317. /* Markdown 样式 */
  318. #c a {
  319. transition:
  320. color .2s,
  321. background-color .2s;
  322. }
  323. #c a:hover {
  324. background-color: rgba(255, 255, 255, 0.1);
  325. }
  326. #c pre {
  327. background-color: rgba(0, 0, 0, 0.05);
  328. padding: .4rem;
  329. border-radius: .5rem;
  330. overflow-y: scroll;
  331. }
  332. #c code {
  333. font-family: "cousine nerd font mono", monospace;
  334. }
  335. #c p code {
  336. background-color: rgba(0, 0, 0, 0.05);
  337. font-size: small;
  338. padding: .2rem;
  339. border-radius: .1rem;
  340. }
  341. #c blockquote {
  342. background-color: rgba(0, 0, 0, 0.05);
  343. font-size: small;
  344. padding: .4rem;
  345. border-left: 5px solid rgba(0, 0, 0, 0.1);
  346. }
  347. #c table {
  348. border-spacing: 0;
  349. border-radius: .1rem;
  350. width: 100%;
  351. border-collapse: separate;
  352. border: .1rem solid rgba(0, 0, 0, 0.05);
  353. }
  354. #c table td,
  355. #c table th {
  356. border-right: .1rem solid rgba(0, 0, 0, 0.05);
  357. }
  358. #c table td,
  359. #c table th {
  360. border-top: .1rem solid rgba(0, 0, 0, 0.05);
  361. }
  362. #c table td:last-child,
  363. #c th:last-child {
  364. border-right-color: transparent;
  365. }
  366. #c table thead tr:first-child>* {
  367. border-top: .1rem solid transparent;
  368. }
  369. /* 加载进度条 */
  370. .loading-bar {
  371. position: fixed;
  372. top: 0;
  373. left: 0;
  374. z-index: 99999;
  375. opacity: 0;
  376. transition: opacity .4s linear;
  377. .progress {
  378. position: fixed;
  379. top: 0;
  380. left: 0;
  381. width: 0;
  382. height: 4px;
  383. background-color: #FFF;
  384. box-shadow: 0 0 10px rgba(102, 187, 106, .7);
  385. }
  386. &.loading {
  387. opacity: 1;
  388. transition: none;
  389. .progress {
  390. transition: width .4s ease;
  391. }
  392. }
  393. }
  394. /* 翻译按钮相关 */
  395. .translateSelectLanguage {
  396. background-color: #616161;
  397. color: inherit;
  398. z-index: 10;
  399. width: 25%;
  400. height: 100%;
  401. opacity: 0;
  402. cursor: pointer;
  403. position: absolute;
  404. top: 0;
  405. left: 25%;
  406. }
  407. /* 深色模式相关 */
  408. .darkmode--activated img,
  409. .darkmode--activated svg {
  410. isolation: isolate;
  411. mix-blend-mode: difference
  412. }
  413. .darkmode-layer,
  414. .darkmode-toggle {
  415. z-index: 2
  416. }
  417. /* 小屏响应式优化 */
  418. @media only screen and (max-width: 950px) {
  419. html,
  420. body,
  421. #c {
  422. height: 100%;
  423. }
  424. #c {
  425. overflow-y: scroll;
  426. min-width: 256px;
  427. top: 0;
  428. margin: 0 0;
  429. }
  430. }
  431. </style>
  432. </head>
  433. <body>
  434. <div class="background"></div>
  435. <!-- 导航栏 -->
  436. <nav>
  437. <a title="主页" id="to-home"><i class="fa fa-home"></i></a>
  438. <a title="更改语言" id="translate"><i class="fa fa-language"></i></a>
  439. <a title="切换显示模式" id="dark_b" href="javascript:;"><i class="fa fa-adjust"></i></a>
  440. <a title="关于" href="https://github.com/PJ-568/markdown.html/"><i class="fa fa-info-circle"></i></a>
  441. </nav>
  442. <!-- 内容主体 -->
  443. <div id="c">
  444. <h1>加载中……</h1>
  445. <p>请稍等。</p>
  446. </div>
  447. <!-- 加载动画 -->
  448. <div class="loading-bar">
  449. <div class="progress"></div>
  450. </div>
  451. <script src="//lf6-cdn-tos.bytecdntp.com/cdn/expire-1-y/instant.page/5.1.0/instantpage.min.js"
  452. type="text/javascript"></script>
  453. </body>
  454. </html>