| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386 |
- <!doctype html>
- <html>
- <markdown-html
- version="1.20.11"
- 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,PD94bWwgdmVyc2lvbj0iMS4wIiBlbmNvZGluZz0iVVRGLTgiPz4KPHN2ZyB3aWR0aD0iNDgiIGhlaWdodD0iNDgiIHZlcnNpb249IjEuMSIgdmlld0JveD0iMCAwIDQ4IDQ4IiB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciPgo8cmVjdCB4PSI2IiB5PSI2IiB3aWR0aD0iMzYiIGhlaWdodD0iMzYiIHJ4PSIyLjI1IiBmaWxsPSIjMmU3ZDMyIiBzdHJva2U9IiMxYjVlMjAiIHN0cm9rZS13aWR0aD0iLjc1Ii8+CjxnIGZpbGw9IiNlOGY1ZTkiIHN0cm9rZS13aWR0aD0iLjA5Mzc1Ij4KPHJlY3QgeD0iOSIgeT0iMTIiIHdpZHRoPSIzMCIgaGVpZ2h0PSIxLjUiIHJ4PSIuMzc1Ii8+CjxyZWN0IHg9IjkiIHk9IjE1Ljc1IiB3aWR0aD0iMjYuMjUiIGhlaWdodD0iMS41IiByeD0iLjM3NSIvPgo8cmVjdCB4PSI5IiB5PSIxOS41IiB3aWR0aD0iMjIuNSIgaGVpZ2h0PSIxLjUiIHJ4PSIuMzc1Ii8+CjxyZWN0IHg9IjkiIHk9IjIzLjI1IiB3aWR0aD0iMzAiIGhlaWdodD0iMS41IiByeD0iLjM3NSIvPgo8cmVjdCB4PSI5IiB5PSIyNyIgd2lkdGg9IjI2LjI1IiBoZWlnaHQ9IjEuNSIgcng9Ii4zNzUiLz4KPHJlY3QgeD0iOSIgeT0iMzAuNzUiIHdpZHRoPSIyMi41IiBoZWlnaHQ9IjEuNSIgcng9Ii4zNzUiLz4KPHRleHQgeD0iMjQiIHk9IjM5LjM3NSIgZm9udC1mYW1pbHk9IidTYXJhc2EgVGVybSBTQyBOZXJkXFxcXFxcXFxcXFxcXFxcJycsICdNYXBsZSBNb25vIENOJywgVWJ1bnR1LCBSb2JvdG8sICdPcGVuIFNhbnMnLCAnTWljcm9zb2Z0IFlhSGVpJywgQXJpYWwsIHNhbnMtc2VyaWYiIGZvbnQtc2l6ZT0iNC41cHgiIGZvbnQtd2VpZ2h0PSJib2xkIiB0ZXh0LWFuY2hvcj0ibWlkZGxlIj4mbHQ7TUgmZ3Q7PC90ZXh0Pgo8L2c+Cjwvc3ZnPgo="
- />
- <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;
- }
- // 判断是否为其它协议(是 HTTP(S) 协议)
- function isProtocol(url) {
- //// 排除特殊协议
- const excludedProtocols = [
- "#",
- "javascript:",
- "mailto:",
- "tel:",
- "data:",
- "ftp:",
- "about:",
- ];
- if (excludedProtocols.some((proto) => url.startsWith(proto))) {
- return false;
- } else {
- return true;
- }
- }
- // 判断链接是否为外链
- function isExternalLink(url) {
- if (!url || typeof url !== "string") return false;
- if (!isProtocol(url)) {
- 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) && isProtocol(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 &&
- !src.startsWith("data:") &&
- !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) {
- const imgOrSvg = event.target.closest(".content img, .content svg");
- if (imgOrSvg) {
- const url =
- imgOrSvg.getAttribute("src") ||
- imgOrSvg.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();
- }
- return;
- }
- 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">
- <license content="Copyright (c) 2018-2025 Tabler"></license>
- <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="">
- <license content="Copyright (c) 2018-2025 Tabler"></license>
- <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">
- <license content="Copyright (c) 2018-2025 Tabler"></license>
- <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">
- <license content="Copyright (c) 2018-2025 Tabler"></license>
- <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:;">
- <license content="Copyright (c) 2018-2025 Tabler"></license>
- <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">
- <license content="Copyright (c) 2018-2025 Tabler"></license>
- <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>
|