index.html 17 KB

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