Skip to content

Commit 39158b4

Browse files
committed
feat: enhance utterances support with auto theme adaptation
1 parent a0ed2fc commit 39158b4

File tree

7 files changed

+31
-10
lines changed

7 files changed

+31
-10
lines changed

README.en.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -545,14 +545,15 @@ disqus:
545545
shortname: "your shortname"
546546
count: true # Whether to enable comment count statistics
547547
```
548+
548549
If using utterance [utterances](https://utteranc.es/)
549550
Please set `utterances.enable` to `true `in your theme's `_config.yml` and fill in your own `repo`
550551
```yml
551552
utterances:
552553
enable: true
553554
repo: owner/repo # Change this to "Your GitHub Username/The Repository Name" used for storing blog comments
554555
issue_term: title
555-
theme: github-light
556+
theme: github-light # You can use auto to automatically adapt to dark and light themes
556557
```
557558

558559
</details>

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -551,14 +551,15 @@ disqus:
551551
shortname: "your shortname"
552552
count: true # 是否启用评论数量统计
553553
```
554+
554555
若基于 [utterances](https://utteranc.es/)
555556
请在内层 `_config.yml` 中将 `utterances.enable` 改为 `true`,并填入自己的 `repo`
556557
```yml
557558
utterances:
558559
enable: true
559560
repo: owner/repo # 这里需要修改为 你的 GitHub 用户名/刚刚创建的,用户保存博客评论的 GitHub 仓库名
560561
issue_term: title
561-
theme: github-light
562+
theme: github-light # 你可以使用 auto 来自动适配深色和浅色主题
562563
```
563564
</details>
564565
<details>

_config.yml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -398,7 +398,8 @@ utterances:
398398
enable: false
399399
repo: owner/repo # Change this to "Your GitHub Username/The Repository Name" used for storing blog comments
400400
issue_term: title
401-
theme: github-light
401+
theme: auto # auto means to automatically adapt to dark and light themes, you can also use specific themes like github-light, github-dark, preferred-color-scheme, etc.
402+
402403
disqus:
403404
enable: false
404405
shortname: # your disqus shortname

layout/_partial/article.ejs

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -60,11 +60,6 @@
6060
<%= __('comment') %>
6161
</a>
6262
<%_ } _%>
63-
<%_ if (post.comments !== false && theme.utterances.enable && theme.utterances.repo) { _%>
64-
<a data-aos="<%= theme.animation.options.article.comment %>" href="<%- url_for_lang(post.path) %>#comments" class="article-comment-link">
65-
<%= __('comment') %>
66-
</a>
67-
<%_ } _%>
6863
<%_ if (post.comments !== false && theme.disqus.enable && theme.disqus.shortname && theme.disqus.count) { _%>
6964
<a data-aos="<%= theme.animation.options.article.comment %>" href="<%- full_url_for_lang(post.path) %>#disqus_thread" class="article-comment-link" id="disqus-count">
7065
</a>

layout/_partial/post/comment.ejs

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -190,18 +190,33 @@
190190
if (!container) return;
191191
container.style.display = 'block';
192192
193+
let theme = '<%= theme.utterances.theme || "github-light" %>';
194+
const parsedTheme = theme === 'auto' ? (document.documentElement.getAttribute('data-theme') === 'dark' ? 'github-dark' : 'github-light') : theme;
195+
193196
// 创建 utterances 脚本
194197
const script = document.createElement('script');
195198
script.src = 'https://utteranc.es/client.js';
196199
script.setAttribute('repo', '<%= theme.utterances.repo %>');
197200
script.setAttribute('issue-term', '<%= theme.utterances.issue_term || "pathname" %>');
198-
script.setAttribute('theme', '<%= theme.utterances.theme || "github-light" %>');
201+
script.setAttribute('theme', parsedTheme);
199202
script.setAttribute('crossorigin', 'anonymous');
200203
script.async = true;
201204
202205
// 清空容器(防止重复加载)并插入脚本
203206
container.innerHTML = '';
204207
container.appendChild(script);
208+
if (theme === 'auto') {
209+
document.body.addEventListener('light-theme-set', () => {
210+
const iframe = container.querySelector('iframe.utterances-frame');
211+
if (!iframe) return;
212+
iframe.contentWindow.postMessage({ type: 'set-theme', theme: 'github-light' }, 'https://utteranc.es');
213+
});
214+
document.body.addEventListener('dark-theme-set', () => {
215+
const iframe = container.querySelector('iframe.utterances-frame');
216+
if (!iframe) return;
217+
iframe.contentWindow.postMessage({ type: 'set-theme', theme: 'github-dark' }, 'https://utteranc.es');
218+
});
219+
}
205220
}
206221
},
207222
<%_ } _%>

scripts/filter/stylus.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,7 @@ hexo.extend.filter.register("stylus:renderer", (style) => {
9999
themeConfig.gitalk?.clientID &&
100100
themeConfig.gitalk?.clientSecret;
101101
const hasGiscus = themeConfig.giscus?.enable;
102+
const hasUtterances = themeConfig.utterances?.enable && themeConfig.utterances?.repo;
102103
style
103104
.define(
104105
"article-families",
@@ -237,5 +238,6 @@ hexo.extend.filter.register("stylus:renderer", (style) => {
237238
.define("has-valine", hasValine)
238239
.define("has-waline", hasWaline)
239240
.define("has-gitalk", hasGitalk)
240-
.define("has-giscus", hasGiscus);
241+
.define("has-giscus", hasGiscus)
242+
.define("has-utterances", hasUtterances);
241243
});

source/css/_partial/comment.styl

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -173,6 +173,12 @@ if (has-gitalk) {
173173
}
174174
}
175175

176+
if (has-utterances) {
177+
.utterances {
178+
max-width: none !important;
179+
}
180+
}
181+
176182
$base-comment-style {
177183
color: var(--color-default) !important;
178184
@extend $base-style;

0 commit comments

Comments
 (0)