index.html 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437
  1. <!doctype html>
  2. <html lang="zh-Hans">
  3. <!-- 作者:PJ568 -->
  4. <!-- 项目地址:https://github.com/PJ-568/markdown.html/ -->
  5. <head>
  6. <meta charset="utf-8">
  7. <meta name="description" content="HTML read markdown">
  8. <meta name="viewport" content="width=256, initial-scale=1">
  9. <script defer src="//lib.baomitu.com/marked/4.0.2/marked.min.js" type="text/javascript"></script>
  10. <script src="//lib.baomitu.com/pjax/0.2.8/pjax.min.js" type="text/javascript"></script>
  11. <script src="//lib.baomitu.com/translate.js/3.7.2/translate.js" type="text/javascript"></script>
  12. <script src="//lib.baomitu.com/Darkmode.js/1.5.7/darkmode-js.min.js" type="text/javascript"></script>
  13. <link rel="stylesheet" href="https://lf6-cdn-tos.bytecdntp.com/cdn/expire-1-y/font-awesome/6.0.0/css/all.min.css">
  14. <script>
  15. // 使用 IIFE 包裹以避免全局变量污染
  16. (function () {
  17. // 加载动画相关
  18. var loadingBar = document.querySelector('.loading-bar');
  19. var progress = document.querySelector('.loading-bar .progress');
  20. var timer = null;
  21. // 文档解析功能相关
  22. var url = new URL(window.location.href);
  23. var params = new URLSearchParams(url.search);
  24. var pValue = params.get('p');
  25. // Pjax 相关
  26. let pjax;
  27. // 初始化加载动画相关元素
  28. function initAni() {
  29. loadingBar = document.querySelector('.loading-bar');
  30. progress = document.querySelector('.loading-bar .progress');
  31. }
  32. // 停止加载动画
  33. function endLoad() {
  34. clearInterval(timer);
  35. progress.style.width = '100%';
  36. loadingBar.classList.remove('loading');
  37. setTimeout(function () {
  38. progress.style.width = 0;
  39. }, 400);
  40. }
  41. // 初始化 PJAX
  42. function initPjax() {
  43. try {
  44. const Pjax = window.Pjax || function () { };
  45. pjax = new Pjax({
  46. selectors: [
  47. 'head meta',
  48. 'head title',
  49. '#c',
  50. '.pjax-reload'
  51. ],
  52. cacheBust: false
  53. });
  54. } catch (e) {
  55. console.error('PJAX 初始化出错:' + e);
  56. }
  57. }
  58. // translate-js 翻译系统
  59. //// 初始化翻译系统
  60. function initTranslate() {
  61. try {
  62. translate.service.use('client.edge');
  63. translate.listener.start();
  64. translate.setAutoDiscriminateLocalLanguage();
  65. translate.language.setUrlParamControl();
  66. translate.ignore.class.push('notTranslate');
  67. translate.execute();
  68. } catch (e) {
  69. console.error('翻译系统出错:' + e);
  70. }
  71. }
  72. // Darkmode.js 深色模式
  73. function initDarkmode() {
  74. try {
  75. const Darkmode = window.Darkmode || function () {
  76. console.warn('Darkmode.js 不存在?');
  77. return;
  78. };
  79. var darkmode = new Darkmode();
  80. document.body.addEventListener('click', function (event) {
  81. if (event.target.id === 'dark_b') {
  82. try {
  83. darkmode.toggle();
  84. } catch (e) {
  85. console.error('Darkmode.js 出错:' + e);
  86. }
  87. }
  88. });
  89. } catch (e) {
  90. console.error('初始化 Darkmode.js 出错:' + e);
  91. }
  92. }
  93. // 更新 p 值
  94. function updatePValue() {
  95. url = new URL(window.location.href);
  96. params = new URLSearchParams(url.search);
  97. pValue = params.get('p');
  98. }
  99. // 获取文件所在目录
  100. function getDirectory(path) {
  101. path = new URL(path, 'http://example.com').pathname;
  102. const lastSlashIndex = path.lastIndexOf('/');
  103. return path.substring(0, lastSlashIndex) + '/';
  104. }
  105. // 获取 markdown.html 的当前路径
  106. function getPath() {
  107. return getDirectory(window.location);
  108. }
  109. // 判断路径是否为相对路径
  110. const isRelativePath = (path) => !path.startsWith('/');
  111. // 根据指定文件读取并解析
  112. const showMarkdown = async (path) => {
  113. try {
  114. const absolutePath = new URL(path, 'http://example.com').pathname;
  115. const response = await fetch(absolutePath);
  116. if (response.ok) {
  117. const markdown = await response.text();
  118. var content = marked.parse(markdown);
  119. content = `<blockquote><code>${absolutePath}</code></blockquote>${content}`;
  120. const parser = new DOMParser();
  121. const doc = parser.parseFromString(content, 'text/html');
  122. const links = doc.querySelectorAll('a[href$=".md"]');
  123. links.forEach(link => {
  124. var href = link.getAttribute('href');
  125. /*
  126. 如果 href 是相对路径,则在其前加上当前读取的文档的绝对路径;
  127. 如果不存在当前读取的文档(默认读取的 index.md 或 README.md),
  128. 则在其前加上 markdown.html 的绝对路径。
  129. */
  130. if (isRelativePath(href)) {
  131. if (pValue) {
  132. href = getDirectory(absolutePath) + href;
  133. } else {
  134. href = getPath() + href;
  135. }
  136. }
  137. const absoluteHref = new URL(href, 'http://example.com').pathname;
  138. link.setAttribute('href', `${window.location.pathname}?p=${absoluteHref}`);
  139. });
  140. content = doc.body.innerHTML;
  141. document.getElementById('c').innerHTML = content;
  142. const firstH1 = document.querySelector('h1');
  143. document.title = firstH1 ? firstH1.textContent : absolutePath;
  144. } else {
  145. throw new Error('请求失败');
  146. }
  147. } catch (error) {
  148. document.getElementById('c').innerHTML = '<h1>加载失败</h1><p>可<a href="">重试</a>。</p>';
  149. console.error(error);
  150. }
  151. };
  152. // 初始化内容
  153. const initContent = async () => {
  154. try {
  155. if (pValue) {
  156. await showMarkdown(pValue);
  157. } else {
  158. const response = await fetch(getPath() + 'index.md');
  159. if (response.ok) {
  160. await showMarkdown(getPath() + 'index.md');
  161. } else {
  162. await showMarkdown(getPath() + 'README.md');
  163. }
  164. }
  165. } catch (error) {
  166. console.error(error);
  167. }
  168. };
  169. // 初始化返回首页按钮
  170. function initHomeBtn() {
  171. const homeBtn = document.getElementById('to-home');
  172. homeBtn.href = window.location.pathname;
  173. }
  174. // 初始化
  175. function initialize() {
  176. initContent(); //// 初始化内容
  177. initHomeBtn(); //// 初始化返回首页按钮
  178. initDarkmode(); //// 初始化深色模式
  179. initTranslate(); //// 初始化页面翻译
  180. initPjax(); //// 初始化 Pjax
  181. initAni(); //// 初始化加载动画
  182. }
  183. // 触发器
  184. //// 网页加载完毕后触发
  185. window.addEventListener('DOMContentLoaded', () => initialize());
  186. //// 翻译执行完成后触发
  187. // translate.listener.renderTaskFinish = function (_task) {
  188. // }
  189. //// Pjax 开始时执行的函数
  190. document.addEventListener('pjax:send', function () {
  191. var loadingBarWidth = 20;
  192. var MAX_LOADING_WIDTH = 95;
  193. loadingBar.classList.add('loading');
  194. progress.style.width = loadingBarWidth + '%';
  195. clearInterval(timer);
  196. timer = setInterval(function () {
  197. loadingBarWidth += 3;
  198. if (loadingBarWidth > MAX_LOADING_WIDTH) {
  199. loadingBarWidth = MAX_LOADING_WIDTH;
  200. }
  201. progress.style.width = loadingBarWidth + '%';
  202. }, 500);
  203. });
  204. //// 监听 Pjax 完成后,重新加载
  205. document.addEventListener('pjax:complete', function () {
  206. updatePValue();
  207. initContent();
  208. // translate.execute(); // 手动触发翻译,但是理论上 translate-js 会监听页面变化,所以注释。
  209. endLoad();
  210. })
  211. })();
  212. </script>
  213. <title>加载中</title>
  214. <style>
  215. /* 全局 */
  216. * {
  217. font-family: 'Exo', sans-serif;
  218. text-decoration: none;
  219. }
  220. ::selection {
  221. background-color: #DCEDC8;
  222. }
  223. html {
  224. background-color: #e6e6e6;
  225. }
  226. body {
  227. margin: 0;
  228. }
  229. a {
  230. color: #66BB6A;
  231. cursor: pointer;
  232. text-decoration: none;
  233. }
  234. a:hover {
  235. color: #2E7D32;
  236. }
  237. /* 导航栏 */
  238. nav {
  239. position: fixed;
  240. top: 0;
  241. right: 0;
  242. width: min-content;
  243. background-color: #616161;
  244. display: flex;
  245. justify-content: space-between;
  246. align-items: center;
  247. }
  248. nav a {
  249. color: #FAFAFA;
  250. padding: .5rem;
  251. transition:
  252. color .2s,
  253. background-color .2s;
  254. }
  255. nav a:hover {
  256. color: #FFF;
  257. background-color: #212121;
  258. }
  259. /* 主要内容 */
  260. #c {
  261. margin: 3rem auto;
  262. width: 100%;
  263. max-width: 950px;
  264. min-height: 300px;
  265. background-color: #FAFAFA;
  266. color: #212121;
  267. box-sizing: border-box;
  268. overflow-x: hidden;
  269. padding: 30px;
  270. box-shadow: 0 2px 6px rgba(100, 100, 100, .3);
  271. }
  272. /* Markdown 样式 */
  273. #c a {
  274. transition:
  275. color .2s,
  276. background-color .2s;
  277. }
  278. #c a:hover {
  279. background-color: rgba(255, 255, 255, 0.1);
  280. }
  281. #c pre {
  282. background-color: rgba(0, 0, 0, 0.05);
  283. padding: .4rem;
  284. border-radius: .5rem;
  285. overflow-y: scroll;
  286. }
  287. #c code {
  288. font-family: "cousine nerd font mono", monospace;
  289. }
  290. #c p code {
  291. background-color: rgba(0, 0, 0, 0.05);
  292. font-size: small;
  293. padding: .2rem;
  294. border-radius: .1rem;
  295. }
  296. #c blockquote {
  297. background-color: rgba(0, 0, 0, 0.05);
  298. font-size: small;
  299. padding: .4rem;
  300. border-left: 5px solid rgba(0, 0, 0, 0.1);
  301. }
  302. #c table {
  303. border-spacing: 0;
  304. border-radius: .1rem;
  305. width: 100%;
  306. border-collapse: separate;
  307. border: .1rem solid rgba(0, 0, 0, 0.05);
  308. }
  309. #c table td,
  310. #c table th {
  311. border-right: .1rem solid rgba(0, 0, 0, 0.05);
  312. }
  313. #c table td,
  314. #c table th {
  315. border-top: .1rem solid rgba(0, 0, 0, 0.05);
  316. }
  317. #c table td:last-child,
  318. #c th:last-child {
  319. border-right-color: transparent;
  320. }
  321. #c table thead tr:first-child>* {
  322. border-top: .1rem solid transparent;
  323. }
  324. /* 翻译按钮相关 */
  325. .translateSelectLanguage {
  326. background-color: #616161;
  327. color: inherit;
  328. z-index: 10;
  329. width: 25%;
  330. height: 100%;
  331. opacity: 0;
  332. cursor: pointer;
  333. position: absolute;
  334. top: 0;
  335. left: 25%;
  336. }
  337. /* 深色模式相关 */
  338. .darkmode--activated img,
  339. .darkmode--activated svg {
  340. isolation: isolate;
  341. mix-blend-mode: difference
  342. }
  343. .darkmode-layer,
  344. .darkmode-toggle {
  345. z-index: 2
  346. }
  347. /* 小屏响应式优化 */
  348. @media only screen and (max-width: 950px) {
  349. html,
  350. body,
  351. #c {
  352. height: 100%;
  353. }
  354. #c {
  355. overflow-y: scroll;
  356. min-width: 256px;
  357. top: 0;
  358. margin: 0 0;
  359. }
  360. }
  361. </style>
  362. </head>
  363. <body>
  364. <!-- 导航栏 -->
  365. <nav>
  366. <a title="主页" id="to-home"><i class="fa fa-home"></i></a>
  367. <a title="更改语言" id="translate"><i class="fa fa-language"></i></a>
  368. <a title="切换显示模式" id="dark_b" href="javascript:;"><i class="fa fa-adjust"></i></a>
  369. <a title="关于" href="https://github.com/PJ-568/markdown.html/"><i class="fa fa-info-circle"></i></a>
  370. </nav>
  371. <!-- 内容主体 -->
  372. <div id="c">
  373. <h1>加载中……</h1>
  374. <p>请稍等。</p>
  375. </div>
  376. <!-- 加载动画 -->
  377. <div class="loading-bar">
  378. <div class="progress"></div>
  379. </div>
  380. <script src="//lf6-cdn-tos.bytecdntp.com/cdn/expire-1-y/instant.page/5.1.0/instantpage.min.js"
  381. type="text/javascript"></script>
  382. </body>
  383. </html>