Skip to content

Commit da111c0

Browse files
committed
Update_includes
1 parent 1bf5da4 commit da111c0

File tree

3 files changed

+170
-0
lines changed

3 files changed

+170
-0
lines changed

_includes/comments/disqus.html

+50
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
<!-- The Disqus lazy loading. -->
2+
3+
<div id="disqus_thread">
4+
<p class="text-center text-muted small">Comments powered by <a href="https://disqus.com/">Disqus</a>.</p>
5+
</div>
6+
7+
<script type="text/javascript">
8+
var disqus_config = function () {
9+
this.page.url = '{{ page.url | absolute_url }}';
10+
this.page.identifier = '{{ page.url }}';
11+
};
12+
13+
{%- comment -%} Lazy loading {%- endcomment -%}
14+
var disqus_observer = new IntersectionObserver(
15+
function (entries) {
16+
if (entries[0].isIntersecting) {
17+
(function () {
18+
var d = document,
19+
s = d.createElement('script');
20+
s.src = 'https://{{ site.comments.disqus.shortname }}.disqus.com/embed.js';
21+
s.setAttribute('data-timestamp', +new Date());
22+
(d.head || d.body).appendChild(s);
23+
})();
24+
25+
disqus_observer.disconnect();
26+
}
27+
},
28+
{ threshold: [0] }
29+
);
30+
31+
disqus_observer.observe(document.getElementById('disqus_thread'));
32+
33+
{%- comment -%} Auto switch theme {%- endcomment -%}
34+
function reloadDisqus() {
35+
if (event.source === window && event.data && event.data.direction === ModeToggle.ID) {
36+
{%- comment -%} Disqus hasn't been loaded {%- endcomment -%}
37+
if (typeof DISQUS === 'undefined') {
38+
return;
39+
}
40+
41+
if (document.readyState == 'complete') {
42+
DISQUS.reset({ reload: true, config: disqus_config });
43+
}
44+
}
45+
}
46+
47+
if (document.getElementById('mode-toggle')) {
48+
window.addEventListener('message', reloadDisqus);
49+
}
50+
</script>

_includes/comments/giscus.html

+71
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
<!-- https://giscus.app/ -->
2+
<script type="text/javascript">
3+
(function () {
4+
const origin = 'https://giscus.app';
5+
const lightTheme = 'light';
6+
const darkTheme = 'dark_dimmed';
7+
8+
let initTheme = lightTheme;
9+
const html = document.documentElement;
10+
11+
if (
12+
(html.hasAttribute('data-mode') &&
13+
html.getAttribute('data-mode') === 'dark') ||
14+
(!html.hasAttribute('data-mode') &&
15+
window.matchMedia('(prefers-color-scheme: dark)').matches)
16+
) {
17+
initTheme = darkTheme;
18+
}
19+
20+
let lang = '{{ site.comments.giscus.lang | default: lang }}';
21+
{%- comment -%} https://github.com/giscus/giscus/tree/main/locales {%- endcomment -%}
22+
if (lang.length > 2 && !lang.startsWith('zh')) {
23+
lang = lang.slice(0, 2);
24+
}
25+
26+
let giscusAttributes = {
27+
src: 'https://giscus.app/client.js',
28+
'data-repo': '{{ site.comments.giscus.repo}}',
29+
'data-repo-id': '{{ site.comments.giscus.repo_id }}',
30+
'data-category': '{{ site.comments.giscus.category }}',
31+
'data-category-id': '{{ site.comments.giscus.category_id }}',
32+
'data-mapping': '{{ site.comments.giscus.mapping | default: 'pathname' }}',
33+
'data-strict' : '{{ site.comments.giscus.strict | default: '0' }}',
34+
'data-reactions-enabled': '{{ site.comments.giscus.reactions_enabled | default: '1' }}',
35+
'data-emit-metadata': '0',
36+
'data-theme': initTheme,
37+
'data-input-position': '{{ site.comments.giscus.input_position | default: 'bottom' }}',
38+
'data-lang': lang,
39+
'data-loading': 'lazy',
40+
crossorigin: 'anonymous',
41+
async: ''
42+
};
43+
44+
let giscusScript = document.createElement('script');
45+
Object.entries(giscusAttributes).forEach(([key, value]) =>
46+
giscusScript.setAttribute(key, value)
47+
);
48+
document.getElementById('tail-wrapper').appendChild(giscusScript);
49+
50+
addEventListener('message', (event) => {
51+
if (
52+
event.source === window &&
53+
event.data &&
54+
event.data.direction === ModeToggle.ID
55+
) {
56+
{%- comment -%} global theme mode changed {%- endcomment -%}
57+
const mode = event.data.message;
58+
const theme = mode === ModeToggle.DARK_MODE ? darkTheme : lightTheme;
59+
60+
const message = {
61+
setConfig: {
62+
theme: theme
63+
}
64+
};
65+
66+
const giscus = document.getElementsByClassName('giscus-frame')[0].contentWindow;
67+
giscus.postMessage({ giscus: message }, origin);
68+
}
69+
});
70+
})();
71+
</script>

_includes/comments/utterances.html

+49
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
<!-- https://utteranc.es/ -->
2+
<script
3+
src="https://utteranc.es/client.js"
4+
repo="{{ site.comments.utterances.repo }}"
5+
issue-term="{{ site.comments.utterances.issue_term }}"
6+
crossorigin="anonymous"
7+
async
8+
></script>
9+
10+
<script type="text/javascript">
11+
(function () {
12+
const origin = 'https://utteranc.es';
13+
const lightTheme = 'github-light';
14+
const darkTheme = 'github-dark';
15+
let initTheme = lightTheme;
16+
const html = document.documentElement;
17+
18+
if (
19+
(html.hasAttribute('data-mode') && html.getAttribute('data-mode') === 'dark') ||
20+
(!html.hasAttribute('data-mode') && window.matchMedia('(prefers-color-scheme: dark)').matches)
21+
) {
22+
initTheme = darkTheme;
23+
}
24+
25+
addEventListener('message', (event) => {
26+
let theme;
27+
28+
{%- comment -%} credit to <https://github.com/utterance/utterances/issues/170#issuecomment-594036347> {%- endcomment -%}
29+
if (event.origin === origin) {
30+
{%- comment -%} page initial {%- endcomment -%}
31+
theme = initTheme;
32+
} else if (event.source === window && event.data && event.data.direction === ModeToggle.ID) {
33+
{%- comment -%} global theme mode changed {%- endcomment -%}
34+
const mode = event.data.message;
35+
theme = mode === ModeToggle.DARK_MODE ? darkTheme : lightTheme;
36+
} else {
37+
return;
38+
}
39+
40+
const message = {
41+
type: 'set-theme',
42+
theme: theme
43+
};
44+
45+
const utterances = document.getElementsByClassName('utterances-frame')[0].contentWindow;
46+
utterances.postMessage(message, origin);
47+
});
48+
})();
49+
</script>

0 commit comments

Comments
 (0)