Skip to content

Commit 6fa4115

Browse files
committed
Migrate comment component from utterances to giscus.
1 parent 30d5e4e commit 6fa4115

File tree

3 files changed

+18
-12
lines changed

3 files changed

+18
-12
lines changed

config.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
baseURL: https://ramsayleung.github.io
2-
title: "In Pursuit of Hubris"
2+
title: "In Pursuit of Simplicity"
33
paginate: 5
44
theme: PaperMod
55

content/zh/post/2024/hugo评论组件自适应博客暗黑主题.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ function getCurrentTheme() {
8787

8888
```js
8989
function loadUtterances(darkMode=false) {
90-
const commentContainer = document.getElementById("comments-utteranc");
90+
const commentContainer = document.getElementById("comments-giscus");
9191
if (commentContainer !== null) {
9292
commentContainer.innerHTML = ''
9393
const commentScript = document.createElement("script");

layouts/partials/comments.html

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<section>
22
<h2>Comments</h2>
3-
<div id="comments-utteranc"></div>
3+
<div id="comments-giscus"></div>
44
</section>
55

66
<script type="text/javascript">
@@ -9,35 +9,41 @@ <h2>Comments</h2>
99
return document.documentElement.getAttribute('data-theme') || document.body.classList.contains('dark') ? 'dark' : 'light';
1010
}
1111

12-
function loadUtterances(darkMode=false) {
13-
const commentContainer = document.getElementById("comments-utteranc");
12+
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";
16+
const commentContainer = document.getElementById("comments-giscus");
1417
if (commentContainer !== null) {
1518
commentContainer.innerHTML = ''
1619
const commentScript = document.createElement("script");
17-
commentScript.setAttribute("id", "utteranc");
18-
commentScript.setAttribute("src", "https://utteranc.es/client.js");
20+
commentScript.setAttribute("src", "https://giscus.app/client.js");
1921
commentScript.setAttribute("data-repo", "ramsayleung/comment");
20-
commentScript.setAttribute("data-category", "Announcements");
22+
commentScript.setAttribute("data-repo-id", "MDEwOlJlcG9zaXRvcnkzMDk2NjQ1NDk=");
23+
commentScript.setAttribute("data-category", "General");
24+
commentScript.setAttribute("data-category-id", "DIC_kwDOEnUbJc4Cltnz");
2125
commentScript.setAttribute("data-mapping", "pathname");
26+
commentScript.setAttribute("data-strict", "0");
2227
commentScript.setAttribute("data-reactions-enabled", "1");
2328
commentScript.setAttribute("data-emit-metadata", "0");
24-
commentScript.setAttribute("data-theme", darkMode ? "github-dark" : "github-light");
25-
commentScript.setAttribute("data-issue-term", "title");
29+
commentScript.setAttribute("data-input-position", "bottom");
30+
commentScript.setAttribute("data-theme", darkMode ? "dark" : "light");
2631
commentScript.setAttribute("crossorigin", "anonymous");
32+
commentScript.setAttribute("data-lang", isChinese ? "zh-CN": "en");
2733
commentScript.setAttribute("async", "true");
2834
commentContainer.appendChild(commentScript);
2935
}
3036
}
3137

3238
const isDarkMode = getCurrentTheme() === 'dark';
33-
loadUtterances(isDarkMode);
39+
loadComment(isDarkMode);
3440

3541
// Watch for theme changes
3642
const themeObserver = new MutationObserver((mutations) => {
3743
mutations.forEach((mutation) => {
3844
if (mutation.type === 'attributes' && mutation.attributeName === 'class') {
3945
const isDarkMode = getCurrentTheme() === 'dark';
40-
loadUtterances(isDarkMode);
46+
loadComment(isDarkMode);
4147
console.log(`changing theme`);
4248
}
4349
});

0 commit comments

Comments
 (0)