|
| 1 | +// https://giscus.app |
| 2 | + |
| 3 | +const data_repo = "chai2010/advanced-go-programming-book"; |
| 4 | +const data_repo_id = "MDEwOlJlcG9zaXRvcnkxMTU4NTc5NTQ="; |
| 5 | +const data_category = "General"; |
| 6 | +const data_category_id = "DIC_kwDOBufaIs4CAwFi"; |
| 7 | + |
| 8 | +var initAll = function () { |
| 9 | + var path = window.location.pathname; |
| 10 | + if (path.endsWith("/print.html")) { |
| 11 | + return; |
| 12 | + } |
| 13 | + |
| 14 | + var images = document.querySelectorAll("main img") |
| 15 | + Array.prototype.forEach.call(images, function (img) { |
| 16 | + img.addEventListener("click", function () { |
| 17 | + BigPicture({ |
| 18 | + el: img, |
| 19 | + }); |
| 20 | + }); |
| 21 | + }); |
| 22 | + |
| 23 | + // Un-active everything when you click it |
| 24 | + Array.prototype.forEach.call(document.getElementsByClassName("pagetoc")[0].children, function (el) { |
| 25 | + el.addEventHandler("click", function () { |
| 26 | + Array.prototype.forEach.call(document.getElementsByClassName("pagetoc")[0].children, function (el) { |
| 27 | + el.classList.remove("active"); |
| 28 | + }); |
| 29 | + el.classList.add("active"); |
| 30 | + }); |
| 31 | + }); |
| 32 | + |
| 33 | + var updateFunction = function () { |
| 34 | + var id = null; |
| 35 | + var elements = document.getElementsByClassName("header"); |
| 36 | + Array.prototype.forEach.call(elements, function (el) { |
| 37 | + if (window.pageYOffset >= el.offsetTop) { |
| 38 | + id = el; |
| 39 | + } |
| 40 | + }); |
| 41 | + |
| 42 | + Array.prototype.forEach.call(document.getElementsByClassName("pagetoc")[0].children, function (el) { |
| 43 | + el.classList.remove("active"); |
| 44 | + }); |
| 45 | + |
| 46 | + Array.prototype.forEach.call(document.getElementsByClassName("pagetoc")[0].children, function (el) { |
| 47 | + if (id == null) { |
| 48 | + return; |
| 49 | + } |
| 50 | + if (id.href.localeCompare(el.href) == 0) { |
| 51 | + el.classList.add("active"); |
| 52 | + } |
| 53 | + }); |
| 54 | + }; |
| 55 | + |
| 56 | + var pagetoc = document.getElementsByClassName("pagetoc")[0]; |
| 57 | + var elements = document.getElementsByClassName("header"); |
| 58 | + Array.prototype.forEach.call(elements, function (el) { |
| 59 | + var link = document.createElement("a"); |
| 60 | + |
| 61 | + // Indent shows hierarchy |
| 62 | + var indent = ""; |
| 63 | + switch (el.parentElement.tagName) { |
| 64 | + case "H1": |
| 65 | + return; |
| 66 | + case "H3": |
| 67 | + indent = "20px"; |
| 68 | + break; |
| 69 | + case "H4": |
| 70 | + indent = "40px"; |
| 71 | + break; |
| 72 | + default: |
| 73 | + break; |
| 74 | + } |
| 75 | + |
| 76 | + link.appendChild(document.createTextNode(el.text)); |
| 77 | + link.style.paddingLeft = indent; |
| 78 | + link.href = el.href; |
| 79 | + pagetoc.appendChild(link); |
| 80 | + }); |
| 81 | + updateFunction.call(); |
| 82 | + |
| 83 | + // Handle active elements on scroll |
| 84 | + window.addEventListener("scroll", updateFunction); |
| 85 | + |
| 86 | + document.getElementById("theme-list").addEventListener("click", function (e) { |
| 87 | + var iframe = document.querySelector('.giscus-frame'); |
| 88 | + if (!iframe) return; |
| 89 | + var theme; |
| 90 | + if (e.target.className === "theme") { |
| 91 | + theme = e.target.id; |
| 92 | + } else { |
| 93 | + return; |
| 94 | + } |
| 95 | + |
| 96 | + // 若当前 mdbook 主题不是 Light 或 Rust ,则将 giscuz 主题设置为 transparent_dark |
| 97 | + var giscusTheme = "light" |
| 98 | + if (theme != "light" && theme != "rust") { |
| 99 | + giscusTheme = "transparent_dark"; |
| 100 | + } |
| 101 | + |
| 102 | + var msg = { |
| 103 | + setConfig: { |
| 104 | + theme: giscusTheme |
| 105 | + } |
| 106 | + }; |
| 107 | + iframe.contentWindow.postMessage({ giscus: msg }, 'https://giscus.app'); |
| 108 | + }); |
| 109 | + |
| 110 | + pagePath = pagePath.replace("index.md", ""); |
| 111 | + pagePath = pagePath.replace(".md", ""); |
| 112 | + if (pagePath.length > 0) { |
| 113 | + if (pagePath.charAt(pagePath.length-1) == "/"){ |
| 114 | + pagePath = pagePath.substring(0, pagePath.length-1); |
| 115 | + } |
| 116 | + }else { |
| 117 | + pagePath = "index"; |
| 118 | + } |
| 119 | + |
| 120 | + var giscusTheme = "light"; |
| 121 | + const themeClass = document.getElementsByTagName("html")[0].className; |
| 122 | + if (themeClass.indexOf("light") == -1 && themeClass.indexOf("rust") == -1) { |
| 123 | + giscusTheme = "transparent_dark"; |
| 124 | + } |
| 125 | + |
| 126 | + var script = document.createElement("script"); |
| 127 | + script.type = "text/javascript"; |
| 128 | + script.src = "https://giscus.app/client.js"; |
| 129 | + script.async = true; |
| 130 | + script.crossOrigin = "anonymous"; |
| 131 | + script.setAttribute("data-repo", data_repo); |
| 132 | + script.setAttribute("data-repo-id", data_repo_id); |
| 133 | + script.setAttribute("data-category", data_category); |
| 134 | + script.setAttribute("data-category-id", data_category_id); |
| 135 | + script.setAttribute("data-mapping", "specific"); |
| 136 | + script.setAttribute("data-term", pagePath); |
| 137 | + script.setAttribute("data-reactions-enabled", "1"); |
| 138 | + script.setAttribute("data-emit-metadata", "0"); |
| 139 | + script.setAttribute("data-input-position", "top"); |
| 140 | + script.setAttribute("data-theme", giscusTheme); |
| 141 | + script.setAttribute("data-lang", "zh-CN"); |
| 142 | + script.setAttribute("data-loading", "lazy"); |
| 143 | + document.getElementById("giscus-container").appendChild(script); |
| 144 | +}; |
| 145 | + |
| 146 | +window.addEventListener('load', initAll); |
0 commit comments