1
0

index.html 14 KB

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