Skip to content

Commit 9a5d023

Browse files
committed
Optimize the way to set Giscus theme by sending message.
1 parent d8e2d0c commit 9a5d023

File tree

3 files changed

+43
-30
lines changed

3 files changed

+43
-30
lines changed

README.org

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,4 +6,4 @@
66
* Description
77
The blog is migrated from the original self-hosted [[https://github.com/ramsayleung/blog][blog]] which was interesting and delightful to build something from scratch.
88

9-
There is Ramsay personal blog, where he shares a bit of what he has learned along the way.
9+
There is Ramsay's personal blog, where he shares a bit of what he has learned along the way.

config.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ params:
8181
homeInfoParams:
8282
Title: "Hi there \U0001F44B"
8383
Content: >
84-
There is Ramsay' personal blog, where he shares a bit of what he has learned along the way. \
84+
There is Ramsay's personal blog, where he shares a bit of what he has learned along the way. \
8585
8686
_-- **Ramsay Leung**_
8787

layouts/partials/comments.html

Lines changed: 41 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -2,49 +2,63 @@
22
<h2>Comments</h2>
33
<div id="comments-giscus"></div>
44
</section>
5-
65
<script type="text/javascript">
7-
86
function getCurrentTheme() {
97
return document.documentElement.getAttribute('data-theme') || document.body.classList.contains('dark') ? 'dark' : 'light';
108
}
119

10+
function setGiscusTheme(darkMode=false) {
11+
const theme = darkMode ? "dark" : "light";
12+
var iframe = document.querySelector('.giscus-frame');
13+
14+
if (iframe) {
15+
var url = new URL(iframe.src);
16+
url.searchParams.set('theme', theme);
17+
iframe.src = url.toString();
18+
}
19+
}
20+
1221
function loadComment(darkMode=false) {
13-
const currentUrl = URL.parse(window.location.href);
14-
const paths = currentUrl.pathname.split('/').filter(x => x!= '');
15-
const isChinese = paths.length > 0 && paths[0] == "zh";
22+
const isChinese = "{{.Lang}}" == "zh";
1623
const commentContainer = document.getElementById("comments-giscus");
24+
25+
// Only proceed if container exists and visibility is not hidden
1726
if (commentContainer !== null) {
18-
commentContainer.innerHTML = ''
19-
const commentScript = document.createElement("script");
20-
commentScript.setAttribute("src", "https://giscus.app/client.js");
21-
commentScript.setAttribute("data-repo", "ramsayleung/comment");
22-
commentScript.setAttribute("data-repo-id", "MDEwOlJlcG9zaXRvcnkzMDk2NjQ1NDk=");
23-
commentScript.setAttribute("data-category", "General");
24-
commentScript.setAttribute("data-category-id", "DIC_kwDOEnUbJc4Cltnz");
25-
commentScript.setAttribute("data-mapping", "pathname");
26-
commentScript.setAttribute("data-strict", "0");
27-
commentScript.setAttribute("data-reactions-enabled", "1");
28-
commentScript.setAttribute("data-emit-metadata", "0");
29-
commentScript.setAttribute("data-input-position", "bottom");
30-
commentScript.setAttribute("data-theme", darkMode ? "dark" : "light");
31-
commentScript.setAttribute("crossorigin", "anonymous");
32-
commentScript.setAttribute("data-lang", isChinese ? "zh-CN": "en");
33-
commentScript.setAttribute("async", "true");
34-
commentContainer.appendChild(commentScript);
27+
if (!commentContainer.hasAttribute('data-giscus-loaded')) {
28+
console.log('Initial giscus load');
29+
const commentScript = document.createElement("script");
30+
commentScript.setAttribute("src", "https://giscus.app/client.js");
31+
commentScript.setAttribute("data-repo", "ramsayleung/comment");
32+
commentScript.setAttribute("data-repo-id", "MDEwOlJlcG9zaXRvcnkzMDk2NjQ1NDk=");
33+
commentScript.setAttribute("data-category", "General");
34+
commentScript.setAttribute("data-category-id", "DIC_kwDOEnUbJc4Cltnz");
35+
commentScript.setAttribute("data-mapping", "pathname");
36+
commentScript.setAttribute("data-strict", "0");
37+
commentScript.setAttribute("data-reactions-enabled", "1");
38+
commentScript.setAttribute("data-emit-metadata", "0");
39+
commentScript.setAttribute("data-input-position", "bottom");
40+
commentScript.setAttribute("data-theme", darkMode ? "dark" : "light");
41+
commentScript.setAttribute("crossorigin", "anonymous");
42+
commentScript.setAttribute("data-lang", isChinese ? "zh-CN": "en");
43+
commentScript.setAttribute("async", "true");
44+
commentContainer.appendChild(commentScript);
45+
commentContainer.setAttribute('data-giscus-loaded', 'true');
46+
}
3547
}
3648
}
3749

38-
const isDarkMode = getCurrentTheme() === 'dark';
39-
loadComment(isDarkMode);
50+
let currentTheme = getCurrentTheme();
51+
loadComment(currentTheme === 'dark');
4052

4153
// Watch for theme changes
4254
const themeObserver = new MutationObserver((mutations) => {
4355
mutations.forEach((mutation) => {
4456
if (mutation.type === 'attributes' && mutation.attributeName === 'class') {
45-
const isDarkMode = getCurrentTheme() === 'dark';
46-
loadComment(isDarkMode);
47-
console.log(`changing theme`);
57+
const newTheme = getCurrentTheme();
58+
if (newTheme !== currentTheme) {
59+
currentTheme = newTheme;
60+
setGiscusTheme(currentTheme === 'dark');
61+
}
4862
}
4963
});
5064
});
@@ -54,5 +68,4 @@ <h2>Comments</h2>
5468
attributes: true,
5569
attributeFilter: ['class']
5670
});
57-
5871
</script>

0 commit comments

Comments
 (0)