index.html 16 KB

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