Skip to content

Commit cf8e02f

Browse files
Make Giscus light and dark themes configurable via _config.yml (alshedivat#3270)
Currently, the `site.giscus.theme` option is ignored because `giscus.liquid` always computes the theme as light/dark based on the current site theme. This PR allows users to configure separate light and dark giscus themes in `_config.yml`, which will support dynamic updates when switching between light, dark, or system themes. Fixes alshedivat#3269
1 parent 109d394 commit cf8e02f

File tree

3 files changed

+60
-27
lines changed

3 files changed

+60
-27
lines changed

_config.yml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,8 @@ giscus:
110110
strict: 1 # use strict identification mode
111111
reactions_enabled: 1 # enable (1) or disable (0) emoji reactions
112112
input_position: bottom # whether to display input form below (bottom) or above (top) the comments
113-
theme: preferred_color_scheme # name of the color scheme (preferred works well with al-folio light/dark mode)
113+
dark_theme: dark # name of the dark color scheme (preferred works well with al-folio dark mode)
114+
light_theme: light # name of the light color scheme (preferred works well with al-folio light mode)
114115
emit_metadata: 0
115116
lang: en
116117

_includes/giscus.liquid

Lines changed: 10 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -9,33 +9,17 @@
99
{% endif %}
1010

1111
{% if site.giscus.repo %}
12-
<script>
13-
let giscusTheme = determineComputedTheme();
14-
let giscusAttributes = {
15-
src: 'https://giscus.app/client.js',
16-
'data-repo': '{{ site.giscus.repo }}',
17-
'data-repo-id': '{{ site.giscus.repo_id }}',
18-
'data-category': '{{ site.giscus.category }}',
19-
'data-category-id': '{{ site.giscus.category_id }}',
20-
'data-mapping': '{{ site.giscus.mapping }}',
21-
'data-strict': '{{ site.giscus.strict }}',
22-
'data-reactions-enabled': '{{ site.giscus.reactions_enabled }}',
23-
'data-emit-metadata': '{{ site.giscus.emit_metadata }}',
24-
'data-input-position': '{{ site.giscus.input_position }}',
25-
'data-theme': giscusTheme,
26-
'data-lang': '{{ site.giscus.lang }}',
27-
crossorigin: 'anonymous',
28-
async: '',
29-
};
30-
31-
let giscusScript = document.createElement('script');
32-
Object.entries(giscusAttributes).forEach(([key, value]) => giscusScript.setAttribute(key, value));
33-
document.getElementById('giscus_thread').appendChild(giscusScript);
34-
</script>
35-
<noscript>Please enable JavaScript to view the <a href="http://giscus.app/?ref_noscript">comments powered by giscus.</a></noscript>
12+
<script defer src="{{ '/assets/js/giscus-setup.js' | relative_url }}"></script>
13+
<noscript>
14+
Please enable JavaScript to view the
15+
<a href="http://giscus.app/?ref_noscript">comments powered by giscus.</a>
16+
</noscript>
3617
{% else %}
37-
{% capture giscus_warning %} > ##### giscus comments misconfigured > Please follow instructions at
38-
[http://giscus.app](http://giscus.app) and update your giscus configuration. {: .block-danger } {% endcapture %}
18+
{% capture giscus_warning %}
19+
> ##### giscus comments misconfigured
20+
> Please follow instructions at [http://giscus.app](http://giscus.app) and update your giscus configuration.
21+
{: .block-danger }
22+
{% endcapture %}
3923
{{ giscus_warning | markdownify }}
4024
{% endif %}
4125
</div>

_scripts/giscus-setup.js

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
---
2+
permalink: /assets/js/giscus-setup.js
3+
---
4+
5+
function determineGiscusTheme() {
6+
{% if site.enable_darkmode %}
7+
let theme =
8+
localStorage.getItem("theme") ||
9+
document.documentElement.getAttribute("data-theme") ||
10+
"system";
11+
12+
if (theme === "dark") return "{{ site.giscus.dark_theme }}";
13+
if (theme === "light") return "{{ site.giscus.light_theme }}";
14+
15+
const prefersDark = window.matchMedia("(prefers-color-scheme: dark)").matches;
16+
return prefersDark ? "{{ site.giscus.dark_theme }}" : "{{ site.giscus.light_theme }}";
17+
{% else %}
18+
return "{{ site.giscus.light_theme }}";
19+
{% endif %}
20+
}
21+
22+
(function setupGiscus() {
23+
let giscusTheme = determineGiscusTheme();
24+
25+
let giscusAttributes = {
26+
src: "https://giscus.app/client.js",
27+
"data-repo": "{{ site.giscus.repo }}",
28+
"data-repo-id": "{{ site.giscus.repo_id }}",
29+
"data-category": "{{ site.giscus.category }}",
30+
"data-category-id": "{{ site.giscus.category_id }}",
31+
"data-mapping": "{{ site.giscus.mapping }}",
32+
"data-strict": "{{ site.giscus.strict }}",
33+
"data-reactions-enabled": "{{ site.giscus.reactions_enabled }}",
34+
"data-emit-metadata": "{{ site.giscus.emit_metadata }}",
35+
"data-input-position": "{{ site.giscus.input_position }}",
36+
"data-theme": giscusTheme,
37+
"data-lang": "{{ site.giscus.lang }}",
38+
crossorigin: "anonymous",
39+
async: true,
40+
};
41+
42+
let giscusScript = document.createElement("script");
43+
Object.entries(giscusAttributes).forEach(([key, value]) =>
44+
giscusScript.setAttribute(key, value)
45+
);
46+
document.getElementById("giscus_thread").appendChild(giscusScript);
47+
})();
48+

0 commit comments

Comments
 (0)