replacing gitalk with giscuz for commenting

pull/639/head
sunface 3 years ago
parent 20230a1361
commit f384fedd7e

@ -1,12 +1,12 @@
(function() { var initAll = function () {
var path = window.location.pathname; var path = window.location.pathname;
if (path.endsWith("/print.html")) { if (path.endsWith("/print.html")) {
return; return;
} }
var images = document.querySelectorAll("main img") var images = document.querySelectorAll("main img")
Array.prototype.forEach.call(images, function(img) { Array.prototype.forEach.call(images, function (img) {
img.addEventListener("click", function() { img.addEventListener("click", function () {
BigPicture({ BigPicture({
el: img, el: img,
}); });
@ -14,41 +14,41 @@
}); });
// Un-active everything when you click it // Un-active everything when you click it
Array.prototype.forEach.call(document.getElementsByClassName("pagetoc")[0].children, function(el) { Array.prototype.forEach.call(document.getElementsByClassName("pagetoc")[0].children, function (el) {
el.addEventHandler("click", function() { el.addEventHandler("click", function () {
Array.prototype.forEach.call(document.getElementsByClassName("pagetoc")[0].children, function(el) { Array.prototype.forEach.call(document.getElementsByClassName("pagetoc")[0].children, function (el) {
el.classList.remove("active"); el.classList.remove("active");
}); });
el.classList.add("active"); el.classList.add("active");
}); });
}); });
var updateFunction = function() { var updateFunction = function () {
var id = null;
var id;
var elements = document.getElementsByClassName("header"); var elements = document.getElementsByClassName("header");
Array.prototype.forEach.call(elements, function(el) { Array.prototype.forEach.call(elements, function (el) {
if (window.pageYOffset >= el.offsetTop) { if (window.pageYOffset >= el.offsetTop) {
id = el; id = el;
} }
}); });
Array.prototype.forEach.call(document.getElementsByClassName("pagetoc")[0].children, function(el) { Array.prototype.forEach.call(document.getElementsByClassName("pagetoc")[0].children, function (el) {
el.classList.remove("active"); el.classList.remove("active");
}); });
Array.prototype.forEach.call(document.getElementsByClassName("pagetoc")[0].children, function(el) { Array.prototype.forEach.call(document.getElementsByClassName("pagetoc")[0].children, function (el) {
if (id == null) {
return;
}
if (id.href.localeCompare(el.href) == 0) { if (id.href.localeCompare(el.href) == 0) {
el.classList.add("active"); el.classList.add("active");
} }
}); });
}; };
// Populate sidebar on load
window.addEventListener('load', function() {
var pagetoc = document.getElementsByClassName("pagetoc")[0]; var pagetoc = document.getElementsByClassName("pagetoc")[0];
var elements = document.getElementsByClassName("header"); var elements = document.getElementsByClassName("header");
Array.prototype.forEach.call(elements, function(el) { Array.prototype.forEach.call(elements, function (el) {
var link = document.createElement("a"); var link = document.createElement("a");
// Indent shows hierarchy // Indent shows hierarchy
@ -56,9 +56,6 @@
switch (el.parentElement.tagName) { switch (el.parentElement.tagName) {
case "H1": case "H1":
return; return;
// case "H2":
// indent = "20px";
// break;
case "H3": case "H3":
indent = "20px"; indent = "20px";
break; break;
@ -75,30 +72,48 @@
pagetoc.appendChild(link); pagetoc.appendChild(link);
}); });
updateFunction.call(); updateFunction.call();
});
// Handle active elements on scroll // Handle active elements on scroll
window.addEventListener("scroll", updateFunction); window.addEventListener("scroll", updateFunction);
var p = path.replace("index.html", ""); pagePath = pagePath.replace("index.md", "");
p = p.replace(".html", ""); pagePath = pagePath.replace(".md", "");
var strs = p.split("/"); if (pagePath.length > 0) {
if (strs[strs.length-1] == ""){ if (pagePath.charAt(pagePath.length-1) == "/"){
strs.pop() pagePath = pagePath.substring(0, pagePath.length-1)
} }
var str = strs[strs.length-1]; }else {
var title = document.querySelector("main>h1,h2>a").textContent pagePath = "index"
var gitalk = new Gitalk({ }
clientID: '07ea0357feefeb9de502',
clientSecret: '2f362f6990633ad63f7455b06fc00f4e45c5847d', // 选取浏览器默认使用的语言
repo: 'rust-course-comments', const lang = navigator.language || navigator.userLanguage
owner: 'sunface',
admin: ["sunface"], // 若当前 mdbook 主题为 Light 或 Rust ,则将 giscuz 主题设置为 light
labels: ['comments'], var theme = "transparent_dark";
title: title, const themeClass = document.getElementsByTagName("html")[0].className;
createIssueManually: false, if (themeClass.indexOf("light") != -1 || themeClass.indexOf("rust") != -1) {
id: str, theme = "light"
distractionFreeMode: true }
});
gitalk.render('gitalk-container'); var script = document.createElement("script")
})(); script.type = "text/javascript";
script.src = "https://giscus.app/client.js";
script.async = true;
script.crossOrigin = "anonymous";
script.setAttribute("data-repo", "sunface/rust-course");
script.setAttribute("data-repo-id", "MDEwOlJlcG9zaXRvcnkxNDM4MjIwNjk=");
script.setAttribute("data-category", "章节评论区");
script.setAttribute("data-category-id", "DIC_kwDOCJKM9c4COQcP");
script.setAttribute("data-mapping", "specific");
script.setAttribute("data-term", pagePath);
script.setAttribute("data-reactions-enabled", "1");
script.setAttribute("data-emit-metadata", "0");
script.setAttribute("data-input-position", "top");
script.setAttribute("data-theme", theme);
script.setAttribute("data-lang", lang);
script.setAttribute("data-loading", "lazy");
document.getElementById("giscus-container").appendChild(script);
};
window.addEventListener('load', initAll);

@ -5,7 +5,7 @@ title = "Rust语言圣经(Rust教程 Rust Course)"
src = "src" src = "src"
[output.html] [output.html]
additional-css = ["theme/style3.css"] additional-css = ["theme/style4.css"]
additional-js = ["assets/custom0.js", "assets/bigPicture.js"] additional-js = ["assets/custom0.js", "assets/bigPicture.js"]
git-repository-url = "https://github.com/sunface/rust-course" git-repository-url = "https://github.com/sunface/rust-course"
edit-url-template = "https://github.com/sunface/rust-course/edit/main/{path}" edit-url-template = "https://github.com/sunface/rust-course/edit/main/{path}"

@ -54,8 +54,6 @@
<script async type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/mathjax/2.7.1/MathJax.js?config=TeX-AMS-MML_HTMLorMML"></script> <script async type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/mathjax/2.7.1/MathJax.js?config=TeX-AMS-MML_HTMLorMML"></script>
{{/if}} {{/if}}
<link rel="stylesheet" href="https://unpkg.com/gitalk/dist/gitalk.css">
<script src="https://unpkg.com/gitalk/dist/gitalk.min.js"></script>
</head> </head>
<body> <body>
<!-- Provide site root to javascript --> <!-- Provide site root to javascript -->
@ -69,11 +67,9 @@
try { try {
var theme = localStorage.getItem('mdbook-theme'); var theme = localStorage.getItem('mdbook-theme');
var sidebar = localStorage.getItem('mdbook-sidebar'); var sidebar = localStorage.getItem('mdbook-sidebar');
if (theme.startsWith('"') && theme.endsWith('"')) { if (theme.startsWith('"') && theme.endsWith('"')) {
localStorage.setItem('mdbook-theme', theme.slice(1, theme.length - 1)); localStorage.setItem('mdbook-theme', theme.slice(1, theme.length - 1));
} }
if (sidebar.startsWith('"') && sidebar.endsWith('"')) { if (sidebar.startsWith('"') && sidebar.endsWith('"')) {
localStorage.setItem('mdbook-sidebar', sidebar.slice(1, sidebar.length - 1)); localStorage.setItem('mdbook-sidebar', sidebar.slice(1, sidebar.length - 1));
} }
@ -187,7 +183,7 @@
<div class="sidetoc"><nav class="pagetoc"></nav></div> <div class="sidetoc"><nav class="pagetoc"></nav></div>
<main> <main>
{{{ content }}} {{{ content }}}
<div id="gitalk-container"></div> <div id="giscus-container"></div>
</main> </main>
<nav class="nav-wrapper" aria-label="Page navigation"> <nav class="nav-wrapper" aria-label="Page navigation">
@ -235,7 +231,6 @@
location.reload(); location.reload();
} }
}; };
window.onbeforeunload = function() { window.onbeforeunload = function() {
socket.close(); socket.close();
} }
@ -246,7 +241,6 @@
<!-- Google Analytics Tag --> <!-- Google Analytics Tag -->
<script type="text/javascript"> <script type="text/javascript">
var localAddrs = ["localhost", "127.0.0.1", ""]; var localAddrs = ["localhost", "127.0.0.1", ""];
// make sure we don't activate google analytics if the developer is // make sure we don't activate google analytics if the developer is
// inspecting the book locally... // inspecting the book locally...
if (localAddrs.indexOf(document.location.hostname) === -1) { if (localAddrs.indexOf(document.location.hostname) === -1) {
@ -254,7 +248,6 @@
(i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o), (i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),
m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m) m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m)
})(window,document,'script','https://www.google-analytics.com/analytics.js','ga'); })(window,document,'script','https://www.google-analytics.com/analytics.js','ga');
ga('create', '{{google_analytics}}', 'auto'); ga('create', '{{google_analytics}}', 'auto');
ga('send', 'pageview'); ga('send', 'pageview');
} }
@ -290,6 +283,10 @@
<script src="{{ path_to_root }}clipboard.min.js" type="text/javascript" charset="utf-8"></script> <script src="{{ path_to_root }}clipboard.min.js" type="text/javascript" charset="utf-8"></script>
<script src="{{ path_to_root }}highlight.js" type="text/javascript" charset="utf-8"></script> <script src="{{ path_to_root }}highlight.js" type="text/javascript" charset="utf-8"></script>
<script src="{{ path_to_root }}book.js" type="text/javascript" charset="utf-8"></script> <script src="{{ path_to_root }}book.js" type="text/javascript" charset="utf-8"></script>
<script type="text/javascript" charset="utf-8">
var pagePath = "{{ path }}"
</script>
<!-- Custom JS scripts --> <!-- Custom JS scripts -->
{{#each additional_js}} {{#each additional_js}}

@ -57,7 +57,7 @@
.chapter li.chapter-item { .chapter li.chapter-item {
/* 没有文件时的文字颜色 */ /* 没有文件时的文字颜色 */
color: #939da3; color: #939da3;
margin-top: 1.2rem; margin-top: 1rem;
} }
/* 修改滚动条宽度 */ /* 修改滚动条宽度 */
@ -69,3 +69,4 @@
table { table {
margin-left: 0 !important; margin-left: 0 !important;
} }
Loading…
Cancel
Save