index.html 17 KB

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