From ad84713468add6b4ade8ff1754183565a8993dee Mon Sep 17 00:00:00 2001
From: Nour Agha <nour@agha.dev>
Date: Wed, 04 Jan 2023 19:15:19 +0000
Subject: [PATCH] Add giscus (#753)
---
layouts/posts/single.html | 1
docs/configurations.md | 21 ++++++++++
assets/js/coder.js | 11 +++++
layouts/partials/posts/giscus.html | 33 ++++++++++++++++
4 files changed, 66 insertions(+), 0 deletions(-)
diff --git a/assets/js/coder.js b/assets/js/coder.js
index 0697dd5..9b46cfc 100644
--- a/assets/js/coder.js
+++ b/assets/js/coder.js
@@ -75,6 +75,17 @@
})
}
+
+ function sendMessage(message) {
+ const iframe = document.querySelector('iframe.giscus-frame');
+ if (!iframe) return;
+ iframe.contentWindow.postMessage({ giscus: message }, 'https://giscus.app');
+ }
+ sendMessage({
+ setConfig: {
+ theme: theme,
+ },
+ });
// Create and send event
const event = new Event('themeChanged');
diff --git a/docs/configurations.md b/docs/configurations.md
index 2202971..18dcc46 100644
--- a/docs/configurations.md
+++ b/docs/configurations.md
@@ -14,6 +14,7 @@
* [Disqus](#disqus)
* [Commento](#commento)
* [Utterances](#utterances)
+ * [Giscus](#giscus)
* [Theme Parameters](#theme-parameters)
* [Social Icons Configuration](#social-icons-configuration)
* [Menu Items Configurations](#menu-items-configurations)
@@ -39,6 +40,7 @@
* [Disqus](https://disqus.com/)
* [Commento](https://commento.io/)
* [Utterances](https://utteranc.es/)
+ * [Giscus](https://giscus.app/)
### Analytics
@@ -123,6 +125,25 @@
theme = "" # https://utteranc.es/#heading-theme
```
+#### Giscus
+
+```toml
+[params.giscus] # https://giscus.app
+ repo = ""
+ repoID = ""
+ category = ""
+ categoryID = ""
+ mapping = ""
+ term = ""
+ strict = ""
+ reactionsEnabled = ""
+ emitMetadata = ""
+ inputPosition = ""
+ theme = ""
+ lang = ""
+ loading = ""
+```
+
## Theme Parameters
These are all the parameters used by `hugo-coder` theme.
diff --git a/layouts/partials/posts/giscus.html b/layouts/partials/posts/giscus.html
new file mode 100644
index 0000000..3bf25d2
--- /dev/null
+++ b/layouts/partials/posts/giscus.html
@@ -0,0 +1,33 @@
+{{- if isset .Site.Params "giscus" -}}
+ {{- if and (isset .Site.Params.giscus "repo") (not (eq .Site.Params.giscus.repo "" )) (eq (.Params.disableComments | default false) false) -}}
+ <div class="comments">
+ <script>
+
+ let getTheme = window.localStorage && window.localStorage.getItem("colorscheme");
+
+ getTheme = getTheme == null ? '{{$.Site.Params.giscus.theme}}' : getTheme;
+
+ let s = document.createElement('script');
+ s.src = 'https://giscus.app/client.js';
+ s.setAttribute('data-repo', '{{ .Site.Params.giscus.repo }}');
+ s.setAttribute('data-repo-id', '{{ .Site.Params.giscus.repoID }}');
+ s.setAttribute('data-category', '{{ .Site.Params.giscus.category }}');
+ s.setAttribute('data-category-id', '{{ .Site.Params.giscus.categoryID }}');
+ s.setAttribute('data-mapping', '{{ default "pathname" .Site.Params.giscus.mapping }}');
+ s.setAttribute('data-term', '{{ .Site.Params.giscus.term }}');
+ s.setAttribute('data-strict', '{{ default "0" .Site.Params.giscus.strict }}');
+ s.setAttribute('data-reactions-enabled', '{{ default "1" .Site.Params.giscus.reactionsEnabled }}');
+ s.setAttribute('data-emit-metadata', '{{ default "0" .Site.Params.giscus.emitMetadata }}');
+ s.setAttribute('data-input-position', '{{ default "bottom" .Site.Params.giscus.inputPosition }}');
+ s.setAttribute('data-theme', getTheme);
+ s.setAttribute('data-lang', '{{ default "en" .Site.Params.giscus.lang }}');
+ s.setAttribute('data-loading', '{{ .Site.Params.giscus.loading }}');
+ s.setAttribute('crossorigin', 'anonymous');
+ s.setAttribute('async', '');
+ document.querySelector('div.comments').innerHTML = '';
+ document.querySelector('div.comments').appendChild(s);
+
+ </script>
+ </div>
+ {{- end -}}
+{{- end -}}
diff --git a/layouts/posts/single.html b/layouts/posts/single.html
index cbcc546..3ddcf88 100644
--- a/layouts/posts/single.html
+++ b/layouts/posts/single.html
@@ -44,6 +44,7 @@
{{ partial "posts/disqus.html" . }}
{{ partial "posts/commento.html" . }}
{{ partial "posts/utterances.html" . }}
+ {{ partial "posts/giscus.html" . }}
</footer>
</article>
--
Gitblit v1.10.0