1
0

index.html 17 KB

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