1
0

index.html 16 KB

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