Integrate utterances comments

This commit is contained in:
Cotes Chung 2021-12-11 17:24:08 +08:00
parent 75176522e3
commit d315bf57e7
5 changed files with 64 additions and 6 deletions

View File

@ -88,10 +88,14 @@ avatar: '/commons/avatar.jpg'
toc: true
comments:
active: # The global switch for posts comments, e.g., 'disqus'. Keep it empty means disable
# The comment active options are as follows
active: # The global switch for posts comments, e.g., 'disqus'. Keep it empty means disable
# The active options are as follows:
disqus:
shortname: # fill with the Disqus shortname. https://help.disqus.com/en/articles/1717111-what-s-a-shortname
shortname: # fill with the Disqus shortname. https://help.disqus.com/en/articles/1717111-what-s-a-shortname
# utterances settings https://utteranc.es/
utterances:
repo: # <gh-username>/<repo>
issue_term: # < url | pathname | title | ...>
paginate: 10
@ -113,7 +117,6 @@ collections:
output: true
sort_by: order
defaults:
-
scope:

View File

@ -1,4 +1,5 @@
<!-- The comments switcher -->
{% if page.comments and site.comments.active %}
{% include {{ site.comments.active | append: '.html' }} %}
{% capture path %}comments/{{ site.comments.active }}.html{% endcapture %}
{% include {{ path }} %}
{% endif %}

View File

@ -0,0 +1,51 @@
<!-- https://utteranc.es/ -->
<script src="https://utteranc.es/client.js"
repo="{{ site.comments.utterances.repo }}"
issue-term="{{ site.comments.utterances.issue_term }}"
crossorigin="anonymous"
async>
</script>
<script type="text/javascript">
$(function() {
const origin = "https://utteranc.es";
const iframe = "iframe.utterances-frame";
const lightTheme = "github-light";
const darkTheme = "github-dark";
let initTheme = lightTheme;
if ($("html[mode=dark]").length > 0
|| ($("html[mode]").length == 0
&& window.matchMedia("(prefers-color-scheme: dark)").matches)) {
initTheme = darkTheme;
}
addEventListener("message", (event) => {
let theme;
/* credit to <https://github.com/utterance/utterances/issues/170#issuecomment-594036347> */
if (event.origin === origin) {
/* page initial */
theme = initTheme;
} else if (event.source === window && event.data &&
event.data.direction === ModeToggle.ID) {
/* global theme mode changed */
const mode = event.data.message;
theme = (mode === ModeToggle.DARK_MODE ? darkTheme : lightTheme);
} else {
return;
}
const message = {
type: "set-theme",
theme: theme
};
const utterances = document.querySelector(iframe).contentWindow;
utterances.postMessage(message, origin);
});
});
</script>

View File

@ -233,9 +233,12 @@ nav[data-toggle=toc] {
#tail-wrapper {
min-height: 2rem;
> div:last-of-type {
margin-bottom: 2rem;
}
#disqus_thread {
min-height: 7.5rem;
margin-bottom: 2rem;
}
}