Skip to content

Commit b9f1fc8

Browse files
authored
Add support for giscus comments (alshedivat#1028)
adds support for comments on posts using https://giscus.app/
1 parent 5c5a2c8 commit b9f1fc8

10 files changed

+114
-44
lines changed

_config.yml

+18-2
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,23 @@ permalink: /blog/:year/:title/
122122
pagination:
123123
enabled: true
124124

125-
# Comments
125+
# Giscus comments (RECOMMENDED)
126+
# Follow instructions on https://giscus.app/ to setup for your repo to fill out
127+
# the information below.
128+
giscus:
129+
repo: alshedivat/al-folio # <your-github-user-name>/<your-github-repo-name>
130+
repo_id: MDEwOlJlcG9zaXRvcnk2MDAyNDM2NQ==
131+
category: Comments # name of the category under which discussions will be created
132+
category_id: DIC_kwDOA5PmLc4CTBt6
133+
mapping: title # identify discussions by post title
134+
strict: 1 # use strict identification mode
135+
reactions_enabled: 1 # enable (1) or disable (0) emoji reactions
136+
input_position: bottom # whether to display input form below (bottom) or above (top) the comments
137+
theme: preferred_color_scheme # name of the color scheme (preferred works well with al-folio light/dark mode)
138+
emit_metadata: 0
139+
lang: en
140+
141+
# Disqus comments (DEPRECATED)
126142
disqus_shortname: al-folio # put your disqus shortname
127143
# https://help.disqus.com/en/articles/1717111-what-s-a-shortname
128144

@@ -265,7 +281,7 @@ more_authors_animation_delay: 10 # more authors are revealed on click using ani
265281

266282

267283
# -----------------------------------------------------------------------------
268-
# Jekyll Link Attributes
284+
# Jekyll Link Attributes
269285
# -----------------------------------------------------------------------------
270286

271287
# These are the defaults

_includes/disqus.html

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<div id="disqus_thread" style="max-width: {{ site.max_width }}; margin: 0 auto;">
2+
<script type="text/javascript">
3+
var disqus_shortname = '{{ site.disqus_shortname }}';
4+
var disqus_identifier = '{{ page.id }}';
5+
var disqus_title = {{ page.title | jsonify }};
6+
(function() {
7+
var dsq = document.createElement('script'); dsq.type = 'text/javascript'; dsq.async = true;
8+
dsq.src = '//' + disqus_shortname + '.disqus.com/embed.js';
9+
(document.getElementsByTagName('head')[0] || document.getElementsByTagName('body')[0]).appendChild(dsq);
10+
})();
11+
</script>
12+
<noscript>Please enable JavaScript to view the <a href="http://disqus.com/?ref_noscript">comments powered by Disqus.</a></noscript>
13+
</div>

_includes/giscus.html

+27
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
<div id="giscus_thread" style="max-width: {{ site.max_width }}; margin: 0 auto;">
2+
<script>
3+
let giscusTheme = localStorage.getItem("theme");
4+
let giscusAttributes = {
5+
"src": "https://giscus.app/client.js",
6+
"data-repo": "{{ site.giscus.repo }}",
7+
"data-repo-id": "{{ site.giscus.repo_id }}",
8+
"data-category": "{{ site.giscus.category }}",
9+
"data-category-id": "{{ site.giscus.category_id }}",
10+
"data-mapping": "{{ site.giscus.mapping }}",
11+
"data-strict": "{{ site.giscus.strict }}",
12+
"data-reactions-enabled": "{{ site.giscus.reactions_enabled }}",
13+
"data-emit-metadata": "{{ site.giscus.emit_metadata }}",
14+
"data-input-position": "{{ site.giscus.input_position }}",
15+
"data-theme": giscusTheme,
16+
"data-lang": "{{ site.giscus.lang }}",
17+
"crossorigin": "anonymous",
18+
"async": "",
19+
};
20+
21+
22+
let giscusScript = document.createElement("script");
23+
Object.entries(giscusAttributes).forEach(([key, value]) => giscusScript.setAttribute(key, value));
24+
document.getElementById("giscus_thread").appendChild(giscusScript);
25+
</script>
26+
<noscript>Please enable JavaScript to view the <a href="http://giscus.app/?ref_noscript">comments powered by giscus.</a></noscript>
27+
</div>

_layouts/distill.html

+6-13
Original file line numberDiff line numberDiff line change
@@ -98,20 +98,13 @@ <h3>Contents</h3>
9898

9999
<d-bibliography src="{{ page.bibliography | prepend: '/assets/bibliography/' | relative_url }}"></d-bibliography>
100100

101-
{%- if site.disqus_shortname and page.comments -%}
102-
<div id="disqus_thread" style="max-width: 800px; margin: 0 auto"></div>
103-
<script type="text/javascript">
104-
var disqus_shortname = '{{ site.disqus_shortname }}';
105-
var disqus_identifier = '{{ page.id }}';
106-
var disqus_title = {{ page.title | jsonify }};
107-
(function() {
108-
var dsq = document.createElement('script'); dsq.type = 'text/javascript'; dsq.async = true;
109-
dsq.src = '//' + disqus_shortname + '.disqus.com/embed.js';
110-
(document.getElementsByTagName('head')[0] || document.getElementsByTagName('body')[0]).appendChild(dsq);
111-
})();
112-
</script>
113-
<noscript>Please enable JavaScript to view the <a href="http://disqus.com/?ref_noscript">comments powered by Disqus.</a></noscript>
101+
{%- if site.disqus_shortname and page.disqus_comments -%}
102+
{% include disqus.html %}
114103
{%- endif %}
104+
{%- if site.giscus.repo and page.giscus_comments -%}
105+
{% include giscus.html %}
106+
{%- endif -%}
107+
115108
</div>
116109

117110
<!-- Footer -->

_layouts/post.html

+5-13
Original file line numberDiff line numberDiff line change
@@ -43,19 +43,11 @@ <h1 class="post-title">{{ page.title }}</h1>
4343
{{ content }}
4444
</article>
4545

46-
{%- if site.disqus_shortname and page.comments -%}
47-
<div id="disqus_thread" style="max-width: {{ site.max_width }}; margin: 0 auto"></div>
48-
<script type="text/javascript">
49-
var disqus_shortname = '{{ site.disqus_shortname }}';
50-
var disqus_identifier = '{{ page.id }}';
51-
var disqus_title = {{ page.title | jsonify }};
52-
(function() {
53-
var dsq = document.createElement('script'); dsq.type = 'text/javascript'; dsq.async = true;
54-
dsq.src = '//' + disqus_shortname + '.disqus.com/embed.js';
55-
(document.getElementsByTagName('head')[0] || document.getElementsByTagName('body')[0]).appendChild(dsq);
56-
})();
57-
</script>
58-
<noscript>Please enable JavaScript to view the <a href="http://disqus.com/?ref_noscript">comments powered by Disqus.</a></noscript>
46+
{%- if site.disqus_shortname and page.disqus_comments -%}
47+
{% include disqus.html %}
5948
{%- endif %}
49+
{%- if site.giscus.repo and page.giscus_comments -%}
50+
{% include giscus.html %}
51+
{%- endif -%}
6052

6153
</div>
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
---
22
layout: post
3-
title: a post with comments
3+
title: a post with disqus comments
44
date: 2015-10-20 11:59:00-0400
5-
description: an example of a blog post with comments
6-
comments: true
5+
description: an example of a blog post with disqus comments
76
categories: sample-posts external-services
7+
disqus_comments: true
88
---
99
This post shows how to add DISQUS comments.

_posts/2018-12-22-distill.md

+11-10
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
layout: distill
33
title: a distill-style blog post
44
description: an example of a distill-style blog post and main elements
5+
giscus_comments: true
56
date: 2021-05-22
67

78
authors:
@@ -180,7 +181,7 @@ Strikethrough uses two tildes. ~~Scratch this.~~
180181

181182
1. First ordered list item
182183
2. Another item
183-
⋅⋅* Unordered sub-list.
184+
⋅⋅* Unordered sub-list.
184185
1. Actual numbers don't matter, just that it's a number
185186
⋅⋅1. Ordered sub-list
186187
4. And another item.
@@ -207,8 +208,8 @@ Strikethrough uses two tildes. ~~Scratch this.~~
207208

208209
Or leave it empty and use the [link text itself].
209210

210-
URLs and URLs in angle brackets will automatically get turned into links.
211-
http://www.example.com or <http://www.example.com> and sometimes
211+
URLs and URLs in angle brackets will automatically get turned into links.
212+
http://www.example.com or <http://www.example.com> and sometimes
212213
example.com (but not on Github, for example).
213214

214215
Some text to show that the reference links can follow later.
@@ -219,10 +220,10 @@ Some text to show that the reference links can follow later.
219220

220221
Here's our logo (hover to see the title text):
221222

222-
Inline-style:
223+
Inline-style:
223224
![alt text](https://github.com/adam-p/markdown-here/raw/master/src/common/images/icon48.png "Logo Title Text 1")
224225

225-
Reference-style:
226+
Reference-style:
226227
![alt text][logo]
227228

228229
[logo]: https://github.com/adam-p/markdown-here/raw/master/src/common/images/icon48.png "Logo Title Text 2"
@@ -233,14 +234,14 @@ Inline `code` has `back-ticks around` it.
233234
var s = "JavaScript syntax highlighting";
234235
alert(s);
235236
```
236-
237+
237238
```python
238239
s = "Python syntax highlighting"
239240
print s
240241
```
241-
242+
242243
```
243-
No language indicated, so no syntax highlighting.
244+
No language indicated, so no syntax highlighting.
244245
But let's throw in a <b>tag</b>.
245246
```
246247

@@ -253,7 +254,7 @@ Colons can be used to align columns.
253254
| zebra stripes | are neat | $1 |
254255

255256
There must be at least 3 dashes separating each header cell.
256-
The outer pipes (|) are optional, and you don't need to make the
257+
The outer pipes (|) are optional, and you don't need to make the
257258
raw Markdown line up prettily. You can also use inline Markdown.
258259

259260
Markdown | Less | Pretty
@@ -266,7 +267,7 @@ Markdown | Less | Pretty
266267
267268
Quote break.
268269

269-
> This is a very long line that will still be quoted properly when it wraps. Oh boy let's keep writing to make sure this is long enough to actually wrap for everyone. Oh, you can *put* **Markdown** into a blockquote.
270+
> This is a very long line that will still be quoted properly when it wraps. Oh boy let's keep writing to make sure this is long enough to actually wrap for everyone. Oh, you can *put* **Markdown** into a blockquote.
270271
271272

272273
Here's a line for us to start with.

_posts/2022-02-01-redirect.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
---
22
layout: post
33
title: a post with redirect
4-
date: 2021-07-04 17:39:00
4+
date: 2022-02-01 17:39:00
55
description: you can also redirect to assets like pdf
66
redirect: /assets/pdf/example_pdf.pdf
77
---

_posts/2022-12-10-giscus-comments.md

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
---
2+
layout: post
3+
title: a post with giscus comments
4+
date: 2022-12-10 11:59:00-0400
5+
description: an example of a blog post with giscus comments
6+
categories: sample-posts external-services
7+
giscus_comments: true
8+
---
9+
This post shows how to add GISCUS comments.

assets/js/theme.js

+21-2
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ let toggleTheme = (theme) => {
1212
let setTheme = (theme) => {
1313
transTheme();
1414
setHighlight(theme);
15+
setGiscusTheme(theme);
1516

1617
if (theme) {
1718
document.documentElement.setAttribute("data-theme", theme);
@@ -20,7 +21,7 @@ let setTheme = (theme) => {
2021
document.documentElement.removeAttribute("data-theme");
2122
}
2223
localStorage.setItem("theme", theme);
23-
24+
2425
// Updates the background of medium-zoom overlay.
2526
if (typeof medium_zoom !== 'undefined') {
2627
medium_zoom.update({
@@ -30,6 +31,7 @@ let setTheme = (theme) => {
3031
}
3132
};
3233

34+
3335
let setHighlight = (theme) => {
3436
if (theme == "dark") {
3537
document.getElementById("highlight_theme_light").media = "none";
@@ -41,6 +43,23 @@ let setHighlight = (theme) => {
4143
}
4244

4345

46+
let setGiscusTheme = (theme) => {
47+
48+
function sendMessage(message) {
49+
const iframe = document.querySelector('iframe.giscus-frame');
50+
if (!iframe) return;
51+
iframe.contentWindow.postMessage({ giscus: message }, 'https://giscus.app');
52+
}
53+
54+
sendMessage({
55+
setConfig: {
56+
theme: theme
57+
}
58+
});
59+
60+
}
61+
62+
4463
let transTheme = () => {
4564
document.documentElement.classList.add("transition");
4665
window.setTimeout(() => {
@@ -56,7 +75,7 @@ let initTheme = (theme) => {
5675
theme = 'dark';
5776
}
5877
}
59-
78+
6079
setTheme(theme);
6180
}
6281

0 commit comments

Comments
 (0)