| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342 |
- <!DOCTYPE html>
- <html>
- <markdown-html
- version="1.20.4"
- author="PJ568"
- repo="https://github.com/PJ-568/markdown.html"
- license="CC BY-SA 4.0 International"
- ></markdown-html>
- <head>
- <meta charset="utf-8" />
- <meta name="description" content="HTML read markdown" />
- <meta
- name="keywords"
- content="markdown, html, renderer, low-dependency, one-click deployment"
- />
- <meta name="author" content="PJ568" />
- <link
- rel="icon"
- type="image/svg+xml"
- href="data:image/svg+xml;charset=utf-8;base64,PHN2ZyB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciIHdpZHRoPSI1MTIiIGhlaWdodD0iNTEyIiB2aWV3Qm94PSIwIDAgNTEyIDUxMiI+CiAgPCEtLSDog4zmma/mlrnlnZfku6PooajmlofmoaMgLS0+CiAgPHJlY3QgeD0iNjQiIHk9IjY0IiB3aWR0aD0iMzg0IiBoZWlnaHQ9IjM4NCIgcng9IjI0IiBmaWxsPSIjMkU3RDMyIiBzdHJva2U9IiMxQjVFMjAiIHN0cm9rZS13aWR0aD0iOCIvPgogIAogIDwhLS0g5paH5qGj5YaF5a6557q/5p2hIC0tPgogIDxyZWN0IHg9Ijk2IiB5PSIxMjgiIHdpZHRoPSIzMjAiIGhlaWdodD0iMTYiIHJ4PSI0IiBmaWxsPSIjRThGNUU5Ii8+CiAgPHJlY3QgeD0iOTYiIHk9IjE2OCIgd2lkdGg9IjI4MCIgaGVpZ2h0PSIxNiIgcng9IjQiIGZpbGw9IiNFOEY1RTkiLz4KICA8cmVjdCB4PSI5NiIgeT0iMjA4IiB3aWR0aD0iMjQwIiBoZWlnaHQ9IjE2IiByeD0iNCIgZmlsbD0iI0U4RjVFOSIvPgogIDxyZWN0IHg9Ijk2IiB5PSIyNDgiIHdpZHRoPSIzMjAiIGhlaWdodD0iMTYiIHJ4PSI0IiBmaWxsPSIjRThGNUU5Ii8+CiAgPHJlY3QgeD0iOTYiIHk9IjI4OCIgd2lkdGg9IjI4MCIgaGVpZ2h0PSIxNiIgcng9IjQiIGZpbGw9IiNFOEY1RTkiLz4KICA8cmVjdCB4PSI5NiIgeT0iMzI4IiB3aWR0aD0iMjQwIiBoZWlnaHQ9IjE2IiByeD0iNCIgZmlsbD0iI0U4RjVFOSIvPgogIAogIDwhLS0g6aG555uu5ZCN56ew57yp5YaZIC0tPgogIDx0ZXh0IHg9IjI1NiIgeT0iNDIwIiBmb250LWZhbWlseT0iU2FyYXNhIFRlcm0gU0MgTmVyZCcsICdNYXBsZSBNb25vIENOJywgVWJ1bnR1LCBSb2JvdG8sICdPcGVuIFNhbnMnLCAnTWljcm9zb2Z0IFlhSGVpJywgQXJpYWwsIHNhbnMtc2VyaWYiIGZvbnQtc2l6ZT0iNDgiIGZvbnQtd2VpZ2h0PSJib2xkIiB0ZXh0LWFuY2hvcj0ibWlkZGxlIiBmaWxsPSIjRThGNUU5Ij4mbHQ7TUgmZ3Q7PC90ZXh0Pgo8L3N2Zz4="
- />
- <meta name="viewport" content="width=256, initial-scale=1" />
- <script
- defer
- src="https://lib.baomitu.com/marked/4.0.2/marked.min.js"
- type="application/javascript"
- ></script>
- <script
- src="https://lib.baomitu.com/pjax/0.2.8/pjax.min.js"
- type="application/javascript"
- ></script>
- <script
- src="https://lib.baomitu.com/translate.js/3.17.0/translate.js"
- type="application/javascript"
- ></script>
- <script
- src="https://lib.baomitu.com/Darkmode.js/1.5.7/darkmode-js.min.js"
- type="application/javascript"
- ></script>
- <script
- src="https://lib.baomitu.com/mermaid/8.14.0/mermaid.min.js"
- type="application/javascript"
- ></script>
- <script
- src="https://lib.baomitu.com/highlight.js/11.4.0/highlight.min.js"
- type="application/javascript"
- ></script>
- <script>
- // 使用 IIFE 包裹以避免全局变量污染
- (function () {
- function getVersion() {
- const markdownElement = document.querySelector("markdown-html");
- if (markdownElement) {
- const version = markdownElement.getAttribute("version");
- return version;
- } else {
- return "未知";
- }
- }
- function getAbout() {
- return `
- # 关于
- ## MARKDOWN.HTML
- 版本:${getVersion()}
- 旨在提供低依赖、一键部署的 Markdown 网页渲染方案。
- ### 特性
- - **依赖少**:仅需一个 HTML 文件,即可完成 Markdown 网页渲染。
- - **Markdown 支持**:将 Markdown 文件渲染为 HTML。
- - **Mermaid 支持**:将 Markdown 中的 Mermaid 语法渲染为图表。
- - **多语言支持**:支持多种语言间的机器互译。
- - **深色模式**:支持浅色、深色、跟随系统。
- - **Pjax 支持**:支持无刷新页面跳转。
- 详情请访问 [GitHub](https://github.com/PJ-568/MARKDOWN.HTML) 或 [Gitee](https://gitee.com/PJ-568/MARKDOWN.HTML) 仓库。
- `;
- }
- // 加载动画相关
- var loadingState = {
- countLoading: 0,
- };
- Object.defineProperty(loadingState, "countLoading", {
- get: function () {
- return this._countLoading;
- },
- set: function (newValue) {
- if (newValue < 0) {
- newValue = 0;
- }
- const loadingBar = document.querySelector(".loading-bar");
- const progress = document.querySelector(".loading-bar .progress");
- if (this.countLoading === 0 && newValue > 0) {
- var loadingBarWidth = 20;
- var MAX_LOADING_WIDTH = 95;
- loadingBar.classList.add("loading");
- progress.style.width = loadingBarWidth + "%";
- clearInterval(timer);
- timer = setInterval(function () {
- loadingBarWidth += 3;
- if (loadingBarWidth > MAX_LOADING_WIDTH) {
- loadingBarWidth = MAX_LOADING_WIDTH;
- }
- progress.style.width = loadingBarWidth + "%";
- }, 500);
- } else if (this.countLoading > 0 && newValue === 0) {
- clearInterval(timer);
- progress.style.width = "100%";
- loadingBar.classList.remove("loading");
- setTimeout(function () {
- progress.style.width = 0;
- }, 400);
- }
- this._countLoading = newValue;
- },
- });
- loadingState.countLoading = 0;
- function startLoad() {
- loadingState.countLoading = loadingState.countLoading + 1;
- }
- function endLoad() {
- loadingState.countLoading = loadingState.countLoading - 1;
- }
- var timer = null;
- // 文档解析功能相关
- var notExistIndexPage = null; // 是否不存在索引页
- var pValue = null;
- async function getPValue() {
- if (pValue) {
- return pValue;
- } else {
- return await decodeMDPath(window.location.href);
- }
- }
- // PJAX 相关
- let PJAX;
- // 深色模式相关
- let darkmode;
- // 自定义 PJAX 相关
- const existPushState = history && history.pushState;
- var level = 1; // 当前页面的层级
- var lastLevel = 0;
- // 初始化 PJAX
- function initPJAX() {
- try {
- const Pjax =
- window.Pjax ||
- function () {
- console.warn("Pjax 不存在?");
- };
- PJAX = new Pjax({
- selectors: ["head title", ".markdown-body", ".pjax-reload"],
- switches: {
- ".markdown-body": Pjax.switches.sideBySide,
- },
- switchesOptions: {
- ".markdown-body": {
- classNames: {
- remove:
- "Animated Animated-remove Animated--reverse Animate--fast Animate--noDelay",
- add: "Animated",
- backward: "Animate--slideInRight",
- forward: "Animate--slideInLeft",
- },
- },
- },
- cacheBust: false,
- });
- } catch (e) {
- console.error("PJAX 初始化出错:" + e);
- }
- }
- // translate-js 翻译系统
- //// 初始化翻译系统
- function initTranslate() {
- try {
- translate.service.use("client.edge");
- translate.request.listener.start();
- translate.setAutoDiscriminateLocalLanguage();
- translate.language.setUrlParamControl();
- translate.ignore.class.push("notTranslate");
- translate.execute();
- } catch (e) {
- console.error("翻译系统出错:" + e);
- }
- }
- // Darkmode.js 深色模式
- function initDarkmode() {
- try {
- const Darkmode =
- window.Darkmode ||
- function () {
- console.warn("Darkmode.js 不存在?");
- };
- darkmode = new Darkmode();
- } catch (e) {
- console.error("初始化 Darkmode.js 出错:" + e);
- }
- }
- //// 检查并提示翻译
- function infoTranslate() {
- try {
- const currentLang = translate.language.getCurrent();
- const originalLang = translate.language.getLocal();
- if (currentLang !== originalLang) {
- const translateInfo =
- document.querySelectorAll(".translate-info");
- if (translateInfo) {
- translateInfo.forEach(function (item) {
- item.remove();
- });
- }
- // 获取 .markdown-body 元素
- var bq = document.createElement("blockquote");
- bq.classList.add("translate-info");
- var infoBody = document.createElement("p");
- infoBody.innerText = "当前页面已翻译。您可以:";
- var msgBody = document.createElement("p");
- var switchButton = document.createElement("a");
- switchButton.classList.add("translate-switch");
- switchButton.href = "javascript:;";
- switchButton.title = "切换到原语言";
- switchButton.innerText = "切换到文章原本的语言";
- bq.appendChild(infoBody);
- bq.appendChild(switchButton);
- const mainElement = document.querySelector(
- ".markdown-body .content"
- );
- mainElement.insertBefore(bq, mainElement.children[1]);
- }
- } catch (e) {
- console.error("检查并提示翻译出错:" + e);
- }
- }
- //// 刷新翻译
- function refreshTranslate() {
- try {
- translate.selectLanguageTag.refreshRender();
- } catch (e) {
- console.error("刷新翻译出错:" + e);
- }
- }
- // 获取文件所在目录
- function getDirectory(path) {
- path = new URL(path, "http://example.com").pathname;
- const lastSlashIndex = path.lastIndexOf("/");
- return path.substring(0, lastSlashIndex + 1);
- }
- // 获取 markdown.html 当前所加载的 md 文件的所在路径
- function getPath() {
- return getDirectory(window.location.href);
- }
- // 将 ?p= 后面的值转换为绝对路径
- /*
- 输出的值是 /path/to/file.md
- 当其为 null 时,根据是否存在 index.md 文件来决定返回的值
- */
- async function decodeMDPath(path) {
- const url = new URL(path);
- const params = new URLSearchParams(url.search);
- path = formatMDPath(params.get("p"));
- return path;
- }
- // 格式化路径
- /*
- 如果 mdPath 为 null,则返回 null,如果 mdPath 是路径,则返回该路径下 /index.md
- return: 返回格式化后的文件路径,一定是绝对路径,或者 null
- */
- function formatMDPath(mdPath) {
- if (mdPath) {
- if (isRelativePath(mdPath)) {
- mdPath = getPath() + mdPath;
- }
- mdPath = new URL(mdPath, "http://example.com").pathname;
- if (!mdPath.endsWith(".md")) {
- if (!mdPath.endsWith("/")) {
- mdPath += "/";
- }
- mdPath += "index.md";
- }
- }
- return mdPath;
- }
- // 判断链接是否为外链
- function isExternalLink(url) {
- if (!url || typeof url !== "string") return false;
- //// 排除特殊协议
- const excludedProtocols = [
- "#",
- "javascript:",
- "mailto:",
- "tel:",
- "data:",
- ];
- if (excludedProtocols.some((proto) => url.startsWith(proto))) {
- return false;
- }
- try {
- const currentUrl = new URL(window.location.href);
- const targetUrl = new URL(url, currentUrl.origin);
- //// 排除非 HTTP(S) 协议
- if (!["http:", "https:"].includes(targetUrl.protocol)) return false;
- //// 域名统一转小写比较
- const currentHost = currentUrl.hostname.toLowerCase();
- const targetHost = targetUrl.hostname.toLowerCase();
- return currentHost !== targetHost;
- } catch (e) {
- return false;
- }
- }
- // 判断路径是否为相对路径
- function isRelativePath(path) {
- return !path.match(/^\//); //// 不以 / 开头
- }
- // 处理错误
- function updateContent(article, title = null, mdPath = pValue) {
- try {
- document.querySelector(
- ".markdown-body"
- ).innerHTML = `<div class="old-content Animated Animated-remove Animated--reverse Animate--fast Animate--noDelay Animate--slideInLeft">${
- document.querySelector(".markdown-body .content").innerHTML
- }</div><div class="content Animated Animate--slideInRight">${article}</div>`;
- firstH1 = document.querySelector(".markdown-body .content h1");
- document.title = title
- ? title
- : firstH1
- ? firstH1.textContent
- : mdPath;
- } catch (error) {
- throw error;
- }
- }
- // 加载 CSS
- function loadCSS(url) {
- return new Promise((resolve, reject) => {
- //// 检查是否已加载
- const existingLinks = document.querySelectorAll(
- `link[href="${url}"]`
- );
- if (existingLinks.length > 0) {
- return resolve(url);
- }
- //// 创建新的 link 元素
- const link = document.createElement("link");
- link.rel = "stylesheet";
- link.type = "text/css";
- link.href = url;
- //// 成功加载处理
- link.onload = () => {
- link.onload = null;
- link.onerror = null;
- resolve(url);
- };
- //// 加载失败处理
- link.onerror = () => {
- link.onerror = null;
- link.onload = null;
- document.head.removeChild(link);
- reject(new Error(`无法加载CSS文件: ${url}`));
- };
- //// 添加到文档头部
- document.head.appendChild(link);
- });
- }
- // 等待动画结束
- function waitForAnimationsEnd(parentElement, selector, animationNames) {
- return new Promise((resolve) => {
- var completedCount = 0;
- const elements = parentElement.querySelectorAll(selector);
- const totalElements = elements.length;
- const handleAnimationEnd = function (event) {
- if (
- animationNames.includes(event.animationName) &&
- Array.from(elements).includes(event.target)
- ) {
- completedCount++;
- if (completedCount === totalElements) {
- parentElement.removeEventListener(
- "animationend",
- handleAnimationEnd
- );
- resolve();
- }
- }
- };
- parentElement.addEventListener("animationend", handleAnimationEnd);
- });
- }
- // 处理错误
- function handleError(mdPath, errorMessage) {
- updateContent(
- `<h1>加载失败</h1><p>加载文档<code>${mdPath}</code>时出错:<code>${errorMessage}</code>。您可以尝试<a class="refresh-btn" href="">重新加载</a>或<a class="back-btn">返回</a>。</p>`,
- "加载失败"
- );
- waitForAnimationsEnd(
- document.querySelector(".markdown-body"),
- ".content, .old-content",
- ["Animation-slideInRight", "Animation-slideInLeft"]
- ).then(() => {
- customPushState(
- window.location.origin +
- window.location.pathname +
- (mdPath ? "?p=" + mdPath : "")
- );
- });
- endLoad();
- }
- // 更新页面的 markdown 内容
- /*
- mdPath: 要加载的 markdown 文件路径,最好是带主机名的绝对路径(要用于 fetch())
- response: fetch() 的返回值,用于防止二次访问
- */
- const showMarkdown = async (mdPath, response = null) => {
- if (mdPath === null) {
- var path = null;
- const currentPath = getPath();
- // 如果存在 index.md 文件,则返回 index.md,否则返回 README.md
- if (notExistIndexPage) {
- path = currentPath + "README.md";
- } else if (notExistIndexPage === false) {
- path = currentPath + "index.md";
- } else {
- response = await fetch(getPath() + "index.md");
- if (
- response.ok &&
- response.headers.get("content-type").startsWith("text/markdown")
- ) {
- notExistIndexPage = false;
- path = currentPath + "index.md";
- } else {
- response = null;
- notExistIndexPage = true;
- path = currentPath + "README.md";
- }
- }
- } else if (mdPath.endsWith("about.md")) {
- // 特殊处理 about.md
- response = await fetch(mdPath);
- const about = getAbout();
- if (
- response.ok &&
- response.headers.get("content-type").startsWith("text/markdown")
- ) {
- var r_text = await response.text();
- const content = about + (await r_text.replace(/^# .*/m, ""));
- response = {
- ok: true,
- status: 200,
- statusText: "自生成。",
- text: async () => {
- return content;
- },
- };
- } else {
- response = {
- ok: true,
- status: 200,
- statusText: "自生成。",
- text: async () => {
- return about + "*无更多详细信息*";
- },
- };
- }
- }
- try {
- pValue = mdPath ? mdPath : path;
- updateContent(await getMarkdown(mdPath ? mdPath : path, response));
- document.querySelector("html").classList.add("loaded"); //// 防止缓存的页面被自定义 PJAX 中覆写的处理响应的函数捕获
- waitForAnimationsEnd(
- document.querySelector(".markdown-body"),
- ".content, .old-content",
- ["Animation-slideInRight", "Animation-slideInLeft"]
- ).then(async () => {
- await showMarkdownEnded();
- customPushState(
- window.location.origin +
- window.location.pathname +
- (mdPath ? "?p=" + mdPath : "")
- );
- });
- } catch (error) {
- handleError(mdPath ? mdPath : path, error);
- }
- };
- // 读取并解析指定文件
- const getMarkdown = async (mdPath, response = null) => {
- //// 生成目录
- function generateOutline(doc) {
- const headings = Array.from(
- doc.querySelectorAll("h1, h2, h3, h4, h5, h6")
- );
- if (headings.length === 0) return "<i>无目录</i>";
- var outline = "<ul>";
- var stack = [];
- headings.forEach((heading) => {
- const level = parseInt(heading.tagName.charAt(1));
- const id = heading.textContent
- .toLowerCase()
- .replace(/ /g, "-") //// 将空格替换为连字符
- .replace(/[^\w\u4e00-\u9fa5-]/g, ""); //// 保留字母、数字、下划线、中文字符和连字符
- //// 设置 id 属性以便链接跳转
- heading.setAttribute("id", id);
- while (
- stack.length > 0 &&
- stack[stack.length - 1].level >= level
- ) {
- stack.pop();
- outline += "</ul></li>";
- }
- if (stack.length === 0 || stack[stack.length - 1].level < level) {
- outline += "<li><ul>";
- } else {
- outline += "</li><li>";
- }
- outline += `<a href="#${id}">${heading.textContent}</a>`;
- stack.push({ level: level });
- });
- while (stack.length > 0) {
- outline += "</ul></li>";
- stack.pop();
- }
- outline += "</ul>";
- return outline;
- }
- if (!response) {
- response = await fetch(mdPath);
- if (
- !response.ok &&
- response.headers.get("content-type").startsWith("text/markdown")
- ) {
- throw new Error(
- "请求失败:" +
- mdPath +
- "," +
- response.headers.get("content-type") +
- "," +
- response.status +
- "," +
- response.statusText
- );
- }
- }
- var markdown_content = await response.text();
- var content = marked.parse(markdown_content);
- //// 因为是在本页面渲染一个其它页面的文档:
- //// - 从 content 中获取所有 href 以 .md 结尾的 a 标签;
- //// - 遍历这些 a 标签,如果 href 不是外链,则判断 href 是否为相对路径……最后转换为 ?p=<到该文件的绝对路径>
- //// - 从 content 中获取所有 img 标签;
- //// - 遍历这些 img 标签,如果 src 不是外链,则判断 src 是否为相对路径……最后转换为 <到该文件的绝对路径>
- const linkParser = new DOMParser();
- const linkDoc = linkParser.parseFromString(
- `<div>${content}</div>`,
- "text/html"
- );
- //// 处理所有 a 链接
- const mdLinks = linkDoc.querySelectorAll('a[href]:not([href=""])');
- mdLinks.forEach((link) => {
- const href = link.getAttribute("href");
- if (!isExternalLink(href) && isRelativePath(href)) {
- var absolutePath = pValue
- ? getDirectory(mdPath) + href
- : getPath() + href;
- absolutePath = new URL(absolutePath, "http://example.com")
- .pathname;
- if (absolutePath.endsWith(".md")) {
- link.setAttribute("href", `?p=${absolutePath}`);
- } else {
- link.setAttribute("href", absolutePath);
- }
- }
- });
- //// 处理所有图片
- const images = linkDoc.querySelectorAll("img");
- images.forEach((img) => {
- const src = img.getAttribute("src");
- img.setAttribute("loading", "lazy");
- if (src && !isExternalLink(src) && isRelativePath(src)) {
- var absolutePath = pValue
- ? getDirectory(mdPath) + src
- : getPath() + src;
- absolutePath = new URL(absolutePath, "http://example.com")
- .pathname;
- img.setAttribute("src", absolutePath);
- }
- });
- //// 更新处理后的 markdown 内容
- content = linkDoc.body.innerHTML.replace(/^<div>|<\/div>$/g, "");
- //// 空文件
- if (content === "") {
- content = "<i>空文件</i>";
- }
- //// 添加目录
- content = `<details class="outline"><summary>${decodeURIComponent(
- mdPath
- )}</summary></details><hr>${content}`;
- const parser = new DOMParser();
- const doc = parser.parseFromString(content, "text/html");
- const outline = generateOutline(doc);
- doc.querySelector(".outline").innerHTML += outline;
- return doc.body.innerHTML;
- };
- // 初始化自定义 PJAX 响应
- function initCustomPJAXResponse() {
- //// 覆写 PJAX 处理响应的函数,在内容插入页面之前实现 markdown 的渲染
- PJAX._handleResponse = PJAX.handleResponse;
- PJAX.handleResponse = async function (
- responseText,
- request,
- href,
- options
- ) {
- if (
- /^<!doctype html>[\s\S]*?<html>[\s\S]*?<markdown-html/i.test(
- request.responseText
- )
- ) {
- await showMarkdown(await decodeMDPath(href));
- } else {
- PJAX._handleResponse(responseText, request, href, options);
- }
- };
- }
- // 初始化自定义 PJAX 事件监听器
- function initCustomPJAXEventListener() {
- // 监听自定义链接的点击事件
- if (existPushState) {
- document
- .querySelector(".markdown-body")
- .addEventListener("click", async function (event) {
- const link = event.target.closest('a[href$=".md"]');
- if (link) {
- try {
- event.preventDefault();
- startLoad();
- await showMarkdown(await decodeMDPath(link.href));
- } catch (e) {
- console.error("自定义 PJAX 出错:" + e);
- }
- }
- });
- }
- }
- // 自定义推送状态
- function customPushState(url = window.location.href) {
- if (existPushState) {
- if (level > lastLevel) {
- history.pushState(
- { url: url, title: document.title, uid: level },
- null,
- url
- );
- lastLevel = level++;
- } else {
- lastLevel = level - 1;
- }
- }
- }
- // 初始化内容
- const initContent = async () => {
- try {
- await showMarkdown(await getPValue());
- } catch (error) {
- handleError(await getPValue(), "初始化内容时出错:" + error);
- }
- };
- // 初始化返回首页按钮
- function initHomeBtn() {
- const homeBtn = document.querySelector(".to-home");
- homeBtn.href = window.location.pathname;
- }
- // 初始化返回首页按钮
- function initAboutBtn() {
- const aboutBtn = document.querySelector(".about-btn");
- aboutBtn.href = `?p=${getDirectory(
- window.location.pathname
- )}about.md`;
- }
- // 初始化
- function initialize() {
- startLoad(); //// 开始加载动画
- initDarkmode(); //// 初始化深色模式
- initHomeBtn(); //// 初始化返回首页按钮
- initAboutBtn(); //// 初始化关于按钮
- initContent(); //// 初始化内容
- initTranslate(); //// 初始化页面翻译
- initPJAX(); //// 初始化 PJAX
- initCustomPJAXResponse(); //// 初始化自定义 PJAX 响应
- initCustomPJAXEventListener(); //// 初始化自定义 PJAX 事件监听器
- // loadCSS(
- // "https://chinese-fonts-cdn.deno.dev/packages/maple-mono-cn/dist/MapleMono-CN-Regular/result.css"
- // ); //// 加载字体 CSS,会造成渲染阻塞,暂时禁用
- endLoad(); //// 结束加载动画
- }
- // 触发器
- //// 网页加载完毕后触发初始化
- window.addEventListener("DOMContentLoaded", () => initialize());
- //// 翻译执行完成后触发
- translate.listener.renderTaskFinish = function (_task) {
- endLoad();
- };
- //// PJAX 开始时执行的函数
- document.addEventListener("pjax:send", function () {
- startLoad();
- });
- //// 监听 PJAX 完成后,重新加载
- document.addEventListener("pjax:complete", async function () {
- await showMarkdown(await getPValue());
- initCustomPJAXEventListener(); //// 重新初始化自定义 PJAX 事件监听器
- });
- //// 监听 animationend 事件
- document.addEventListener(
- "animationend",
- function handleAnimationEnd(event) {
- if (event.animationName === "Animation-slideInRight") {
- const element = document.querySelector(".markdown-body .content");
- element.classList.remove("Animated");
- element.classList.remove("Animate--slideInRight");
- } else if (event.animationName === "Animation-slideInLeft") {
- document.querySelector(".markdown-body .old-content").remove();
- }
- }
- );
- //// 点击事件委托
- document.addEventListener("click", function (event) {
- if (event.target.closest(".back-btn")) {
- window.history.back();
- } else if (event.target.closest(".refresh-btn")) {
- window.location.reload();
- } else if (event.target.closest(".dark-btn")) {
- darkmode.toggle();
- } else if (event.target.closest(".translate-switch")) {
- translate.changeLanguage(translate.language.getLocal());
- /* 跳转。禁用原因是有些带有特殊字符的链接会出错或无法跳转
- } else if (event.target.closest('.content a[href^="#"]')) {
- // 目录平滑移动
- const href = event.target.getAttribute('href');
- if (href !== '#') {
- event.preventDefault();
- const targetElement = document.querySelector(href);
- if (targetElement) {
- targetElement.scrollIntoView({
- behavior: 'smooth'
- });
- } else {
- console.error(`ID 为 ${targetId} 的元素不存在。`);
- }
- }
- */
- } else if (event.target.closest('.content a[href]:not([href=""])')) {
- const href = event.target.getAttribute("href");
- if (isExternalLink(href)) {
- window.open(href, "_blank");
- event.preventDefault();
- }
- } else if (event.target.closest(".content img, .content svg")) {
- const url =
- event.target.getAttribute("src") ||
- event.target.getAttribute("xlink:href");
- if (url) {
- window.open(url, "_blank");
- event.preventDefault();
- } else {
- const newWindow = window.open("", "_blank");
- newWindow.document.write(imgOrSvg.outerHTML);
- newWindow.document.close();
- event.preventDefault();
- }
- }
- });
- //// 文档内容加载完毕时触发
- async function showMarkdownEnded() {
- refreshTranslate();
- infoTranslate(); //// 检查并提示翻译
- await mermaid.init({ noteMargin: 10 }, ".language-mermaid"); //// 初始化 mermaid
- translate.execute(); //// 手动触发翻译,但是理论上 translate-js 会监听页面变化
- hljs.highlightAll(); //// 触发代码块语法高亮
- endLoad();
- window.scrollTo({
- top: 0,
- behavior: "smooth",
- });
- }
- //// 浏览器返回时触发
- window.onpopstate = function (event) {
- if (level > 1) {
- --level;
- }
- };
- })();
- </script>
- <title>加载中</title>
- <style>
- /* 全局 */
- :root,
- html,
- body,
- button,
- input,
- select,
- textarea,
- code,
- pre {
- font-family: "Sarasa Term SC Nerd", "Maple Mono CN", Ubuntu, Roboto,
- "Open Sans", "Microsoft YaHei", Arial, sans-serif;
- text-decoration: none;
- }
- :root,
- html,
- body {
- background-color: #e6e6e6;
- }
- ::selection {
- background-color: #dcedc8;
- }
- body {
- margin: 0;
- }
- a {
- color: #66bb6a;
- cursor: pointer;
- text-decoration: none;
- }
- a:hover {
- color: #2e7d32;
- }
- /* 背景 */
- .background {
- position: fixed;
- left: 0;
- top: 0;
- height: 100%;
- width: 100%;
- z-index: -1;
- background-color: #e6e6e6;
- }
- /* 导航栏 */
- nav {
- position: fixed;
- top: 0;
- right: 0;
- width: min-content;
- background-color: #616161;
- display: flex;
- justify-content: space-between;
- align-items: center;
- z-index: 1;
- }
- nav a {
- color: #fafafa;
- padding: 0.3rem;
- transition: color 0.2s, background-color 0.2s;
- }
- nav a:hover {
- color: #fff;
- background-color: #212121;
- }
- /* 动画 */
- .Animated {
- animation-fill-mode: both;
- animation-duration: 1s;
- }
- .Animated-remove {
- position: absolute;
- }
- .Animated--reverse {
- animation-direction: reverse;
- }
- .Animate--fast {
- animation-duration: 0.5s;
- }
- .Animate--noDelay {
- animation-delay: 0s !important;
- }
- .Animate--slideInRight {
- animation-name: Animation-slideInRight;
- }
- @keyframes Animation-slideInRight {
- 0% {
- transform: translateX(200%);
- }
- 100% {
- transform: translateX(0);
- }
- }
- .Animate--slideInLeft {
- animation-name: Animation-slideInLeft;
- }
- @keyframes Animation-slideInLeft {
- 0% {
- transform: translateX(-100%);
- }
- 100% {
- transform: translateX(0);
- }
- }
- /* 主要内容 */
- .markdown-body {
- position: relative;
- margin: 3rem auto;
- width: 100%;
- max-width: 950px;
- min-height: 300px;
- background-color: #fafafa;
- color: #212121;
- box-sizing: border-box;
- overflow-x: hidden;
- box-shadow: 0 2px 6px rgba(100, 100, 100, 0.3);
- }
- .markdown-body .content,
- .markdown-body .old-content {
- margin: 2rem;
- }
- .markdown-body img,
- .markdown-body video,
- .markdown-body iframe,
- .markdown-body object,
- .markdown-body embed,
- .markdown-body svg,
- .markdown-body canvas,
- .markdown-body audio {
- max-width: 100%;
- background-color: rgba(0, 0, 0, 0.05);
- }
- /* Markdown 样式 */
- .markdown-body a {
- transition: color 0.2s, background-color 0.2s;
- }
- .markdown-body a:hover {
- background-color: rgba(255, 255, 255, 0.1);
- }
- .markdown-body ul,
- .markdown-body ol {
- padding-left: 2rem;
- }
- .markdown-body pre {
- background-color: rgba(0, 0, 0, 0.05);
- padding: 0.4rem;
- border-radius: 0.1rem;
- overflow-y: scroll;
- }
- .markdown-body code {
- font-family: "cousine nerd font mono", monospace;
- background-color: rgba(0, 0, 0, 0.05);
- font-size: small;
- padding: 0.2rem;
- border-radius: 0.1rem;
- }
- .markdown-body pre code {
- background-color: unset;
- font-size: unset;
- padding: unset;
- border-radius: unset;
- }
- .markdown-body blockquote {
- background-color: rgba(0, 0, 0, 0.05);
- font-size: small;
- padding: 0.4rem;
- margin: 0.5rem 2rem;
- border-left: 5px solid rgba(0, 0, 0, 0.1);
- }
- .markdown-body details {
- border: 1px solid rgba(0, 0, 0, 0.1);
- border-radius: 0.1rem;
- }
- .markdown-body summary {
- font-size: small;
- cursor: pointer;
- background-color: rgba(0, 0, 0, 0.05);
- padding: 0.4rem;
- font-weight: bold;
- }
- .markdown-body details a {
- margin-left: -2rem;
- }
- .markdown-body table {
- border-spacing: 0;
- border-radius: 0.1rem;
- width: 100%;
- border-collapse: separate;
- border: 0.1rem solid rgba(0, 0, 0, 0.05);
- }
- .markdown-body table > * {
- overflow-y: scroll;
- }
- .markdown-body table td,
- .markdown-body table th {
- border-right: 0.1rem solid rgba(0, 0, 0, 0.05);
- border-top: 0.1rem solid rgba(0, 0, 0, 0.05);
- padding: 0.4rem;
- }
- .markdown-body table td:last-child,
- .markdown-body th:last-child {
- border-right-color: transparent;
- }
- .markdown-body table thead tr:first-child > * {
- border-top: 0.1rem solid transparent;
- }
- /* 加载进度条 */
- .loading-bar {
- position: fixed;
- top: 0;
- left: 0;
- z-index: 2;
- opacity: 0;
- transition: opacity 0.4s linear;
- .progress {
- position: fixed;
- top: 0;
- left: 0;
- width: 0;
- height: 4px;
- background-color: #2e7d32;
- box-shadow: 0 0 10px rgba(102, 187, 106, 0.7);
- }
- &.loading {
- opacity: 1;
- transition: none;
- .progress {
- transition: width 0.4s ease;
- }
- }
- }
- /* 翻译按钮相关 */
- .translateSelectLanguage {
- background-color: #616161;
- color: inherit;
- z-index: 3;
- width: 17%;
- height: 100%;
- opacity: 0;
- cursor: pointer;
- position: absolute;
- top: 0;
- left: 50%;
- }
- /* 深色模式相关 */
- /* 防止背景图片、svg、canvas 和视频等混色 */
- .darkmode--activated .markdown-body img,
- .darkmode--activated .markdown-body svg,
- .darkmode--activated .markdown-body canvas,
- .darkmode--activated .markdown-body video {
- isolation: isolate;
- mix-blend-mode: difference;
- }
- .darkmode-layer,
- .darkmode-toggle {
- z-index: 3;
- }
- /* 小屏响应式优化 */
- @media only screen and (max-width: 950px) {
- :root,
- html,
- body {
- background-color: #616161;
- }
- :root,
- html,
- body,
- .markdown-body {
- height: 100%;
- }
- nav {
- position: relative;
- margin-left: auto;
- }
- .markdown-body {
- overflow-y: scroll;
- min-width: 256px;
- top: 0;
- margin: 0 0;
- }
- }
- </style>
- <link
- href="https://lib.baomitu.com/highlight.js/11.4.0/styles/default.min.css"
- type="text/css"
- rel="stylesheet"
- />
- </head>
- <body>
- <!-- 导航栏 -->
- <nav>
- <a title="返回" class="back-btn">
- <svg
- xmlns="http://www.w3.org/2000/svg"
- width="24"
- height="24"
- viewBox="0 0 24 24"
- fill="none"
- stroke="currentColor"
- stroke-width="2"
- stroke-linecap="round"
- stroke-linejoin="round"
- class="icon icon-tabler icons-tabler-outline icon-tabler-arrow-back"
- >
- <path stroke="none" d="M0 0h24v24H0z" fill="none" />
- <path d="M9 11l-4 4l4 4m-4 -4h11a4 4 0 0 0 0 -8h-1" />
- </svg>
- </a>
- <a title="刷新" class="refresh-btn" href="">
- <svg
- xmlns="http://www.w3.org/2000/svg"
- width="24"
- height="24"
- viewBox="0 0 24 24"
- fill="none"
- stroke="currentColor"
- stroke-width="2"
- stroke-linecap="round"
- stroke-linejoin="round"
- class="icon icon-tabler icons-tabler-outline icon-tabler-refresh"
- >
- <path stroke="none" d="M0 0h24v24H0z" fill="none" />
- <path d="M20 11a8.1 8.1 0 0 0 -15.5 -2m-.5 -4v4h4" />
- <path d="M4 13a8.1 8.1 0 0 0 15.5 2m.5 4v-4h-4" />
- </svg>
- </a>
- <a title="主页" class="to-home">
- <svg
- xmlns="http://www.w3.org/2000/svg"
- width="24"
- height="24"
- viewBox="0 0 24 24"
- fill="none"
- stroke="currentColor"
- stroke-width="2"
- stroke-linecap="round"
- stroke-linejoin="round"
- class="icon icon-tabler icons-tabler-outline icon-tabler-home"
- >
- <path stroke="none" d="M0 0h24v24H0z" fill="none" />
- <path d="M5 12l-2 0l9 -9l9 9l-2 0" />
- <path d="M5 12v7a2 2 0 0 0 2 2h10a2 2 0 0 0 2 -2v-7" />
- <path d="M9 21v-6a2 2 0 0 1 2 -2h2a2 2 0 0 1 2 2v6" />
- </svg>
- </a>
- <a title="更改语言" id="translate">
- <svg
- xmlns="http://www.w3.org/2000/svg"
- width="24"
- height="24"
- viewBox="0 0 24 24"
- fill="none"
- stroke="currentColor"
- stroke-width="2"
- stroke-linecap="round"
- stroke-linejoin="round"
- class="icon icon-tabler icons-tabler-outline icon-tabler-language"
- >
- <path stroke="none" d="M0 0h24v24H0z" fill="none" />
- <path d="M4 5h7" />
- <path d="M9 3v2c0 4.418 -2.239 8 -5 8" />
- <path d="M5 9c0 2.144 2.952 3.908 6.7 4" />
- <path d="M12 20l4 -9l4 9" />
- <path d="M19.1 18h-6.2" />
- </svg>
- </a>
- <a title="切换显示模式" class="dark-btn" href="javascript:;">
- <svg
- xmlns="http://www.w3.org/2000/svg"
- width="24"
- height="24"
- viewBox="0 0 24 24"
- fill="none"
- stroke="currentColor"
- stroke-width="2"
- stroke-linecap="round"
- stroke-linejoin="round"
- class="icon icon-tabler icons-tabler-outline icon-tabler-brightness"
- >
- <path stroke="none" d="M0 0h24v24H0z" fill="none" />
- <path d="M12 12m-9 0a9 9 0 1 0 18 0a9 9 0 1 0 -18 0" />
- <path d="M12 3l0 18" />
- <path d="M12 9l4.65 -4.65" />
- <path d="M12 14.3l7.37 -7.37" />
- <path d="M12 19.6l8.85 -8.85" />
- </svg>
- </a>
- <a title="关于" class="about-btn">
- <svg
- xmlns="http://www.w3.org/2000/svg"
- width="24"
- height="24"
- viewBox="0 0 24 24"
- fill="none"
- stroke="currentColor"
- stroke-width="2"
- stroke-linecap="round"
- stroke-linejoin="round"
- class="icon icon-tabler icons-tabler-outline icon-tabler-info-circle"
- >
- <path stroke="none" d="M0 0h24v24H0z" fill="none" />
- <path d="M3 12a9 9 0 1 0 18 0a9 9 0 0 0 -18 0" />
- <path d="M12 9h.01" />
- <path d="M11 12h1v4h1" />
- </svg>
- </a>
- </nav>
- <!-- 内容主体 -->
- <div class="markdown-body">
- <div class="content">
- <h1>加载中……</h1>
- <p>请稍等。</p>
- </div>
- </div>
- <!-- 加载动画 -->
- <div class="loading-bar">
- <div class="progress"></div>
- </div>
- <div class="background"></div>
- <script
- src="https://lib.baomitu.com/instant.page/5.1.0/instantpage.min.js"
- type="text/javascript"
- ></script>
- </body>
- </html>
|