Skip to content

Commit ac1e75e

Browse files
committed
feat: 添加 giscus 作为评论系统
1 parent 385c1bc commit ac1e75e

File tree

4 files changed

+68
-1
lines changed

4 files changed

+68
-1
lines changed

.github/workflows/ci.yml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ jobs:
3939
pip install \
4040
mkdocs-materialx \
4141
mkdocs-git-revision-date-localized-plugin \
42-
mkdocs-git-committers-plugin-2
42+
mkdocs-git-committers-plugin-2 \
43+
mkdocs-minify-plugin
4344
4445
- run: mkdocs gh-deploy --force

docs/.meta.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
comments: true

mkdocs.yml

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@ theme:
1313
- search.suggest
1414
- search.share
1515

16+
custom_dir: overrides
17+
1618
palette:
1719
- media: "(prefers-color-scheme)"
1820
toggle:
@@ -45,3 +47,11 @@ plugins:
4547
lang: zh
4648

4749
- optimize
50+
- meta
51+
- minify:
52+
minify_html: true
53+
minify_js: true
54+
minify_css: true
55+
htmlmin_opts:
56+
remove_comments: true
57+
cache_safe: true

overrides/partials/comments.html

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
{% if page.meta.comments %}
2+
<h2 id="__comments">{{ lang.t("meta.comments") }}</h2>
3+
<script
4+
src="https://giscus.app/client.js"
5+
data-repo="mumu-lhl/t1d-survival-guide"
6+
data-repo-id="R_kgDORQE96Q"
7+
data-category="Announcements"
8+
data-category-id="DIC_kwDORQE96c4C22EB"
9+
data-mapping="pathname"
10+
data-strict="0"
11+
data-reactions-enabled="1"
12+
data-emit-metadata="0"
13+
data-input-position="bottom"
14+
data-theme="preferred_color_scheme"
15+
data-lang="zh-CN"
16+
crossorigin="anonymous"
17+
async
18+
></script>
19+
20+
<!-- Synchronize Giscus theme with palette -->
21+
<script>
22+
var giscus = document.querySelector("script[src*=giscus]");
23+
24+
// Set palette on initial load
25+
var palette = __md_get("__palette");
26+
if (palette && typeof palette.color === "object") {
27+
var theme =
28+
palette.color.scheme === "slate" ? "transparent_dark" : "light";
29+
30+
// Instruct Giscus to set theme
31+
giscus.setAttribute("data-theme", theme);
32+
}
33+
34+
// Register event handlers after documented loaded
35+
document.addEventListener("DOMContentLoaded", function () {
36+
var ref = document.querySelector("[data-md-component=palette]");
37+
ref.addEventListener("change", function () {
38+
var palette = __md_get("__palette");
39+
if (palette && typeof palette.color === "object") {
40+
var theme =
41+
palette.color.scheme === "slate"
42+
? "transparent_dark"
43+
: "light";
44+
45+
// Instruct Giscus to change theme
46+
var frame = document.querySelector(".giscus-frame");
47+
frame.contentWindow.postMessage(
48+
{ giscus: { setConfig: { theme } } },
49+
"https://giscus.app",
50+
);
51+
}
52+
});
53+
});
54+
</script>
55+
{% endif %}

0 commit comments

Comments
 (0)