summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorReorx <novoreorx@gmail.com>2022-05-31 22:41:10 +0800
committerReorx <novoreorx@gmail.com>2022-05-31 22:41:10 +0800
commit5c47607d23d31bda44f2f660df372a924daa2381 (patch)
tree314fb7db71f0a7b57d342ace9efdb20035d93e12
parent81f885a7b99ac845348ba750a9419f50632fa985 (diff)
support specifying comment system in both config and front matter
in config: site.Params.defaultCommentSystems in front matter: pageCommentSystems
-rw-r--r--layouts/partials/comments.html30
-rw-r--r--layouts/partials/disqus.html27
-rw-r--r--layouts/partials/disqus_script.html27
-rw-r--r--layouts/partials/telegram_widget.html5
4 files changed, 52 insertions, 37 deletions
diff --git a/layouts/partials/comments.html b/layouts/partials/comments.html
index a9d5391..36460f4 100644
--- a/layouts/partials/comments.html
+++ b/layouts/partials/comments.html
@@ -1,13 +1,27 @@
{{- /* You can add your own layouts/comments.html to override this file */ -}}
-{{- with site.Params.commentSystem.remark42 -}}
- {{- partial "remark42.html" . }}
-{{- end -}}
+{{- $pageCommentSystems := .Param "pageCommentSystems"}}
+{{- if not $pageCommentSystems }}
+ {{- $pageCommentSystems = site.Params.defaultCommentSystems }}
+{{- end }}
-{{- with site.Params.commentSystem.telegramWidget -}}
- {{- partial "telegram_widget.html" . }}
-{{- end -}}
+{{- $page := . -}}
+{{- with site.Params.commentSystems -}}
+ {{- if $pageCommentSystems.remark42 -}}
+ {{- with .remark42 -}}
+ {{- partial "remark42.html" . }}
+ {{- end -}}
+ {{- end -}}
+
+ {{- if $pageCommentSystems.telegramWidget -}}
+ {{- with .telegramWidget -}}
+ {{- partial "telegram_widget.html" . }}
+ {{- end -}}
+ {{- end -}}
-{{- with site.Params.commentSystem.disqus -}}
- {{- partial "disqus.html" . }}
+ {{- if $pageCommentSystems.disqus -}}
+ {{- with .disqus -}}
+ {{- partial "disqus.html" (dict "page" $page "disqus" .) }}
+ {{- end -}}
+ {{- end -}}
{{- end -}}
diff --git a/layouts/partials/disqus.html b/layouts/partials/disqus.html
index dc86fc2..e6b9b33 100644
--- a/layouts/partials/disqus.html
+++ b/layouts/partials/disqus.html
@@ -1,3 +1,30 @@
<div class="disqus-container">
<div id="disqus_thread"></div>
</div>
+
+<script type="text/javascript">
+ var disqus_config = function () {
+ this.page.url = '{{ .page.Permalink }}'; // Replace PAGE_URL with your page's canonical URL variable
+ this.page.identifier = '{{ .page.Permalink | path.Base }}'; // Replace PAGE_IDENTIFIER with your page's unique identifier variable
+ };
+
+ (function () {
+ const disqus = window.DISQUS
+ if (disqus) {
+ // Use DISQUS.reset instead of reloading embed.js please.
+ // https://help.disqus.com/en/articles/1717163-using-disqus-on-ajax-sites
+ disqus.reset({
+ reload: true,
+ config: disqus_config,
+ });
+ } else {
+ var d = document, s = d.createElement('script');
+ s.src = '//' + '{{ .disqus.shortname }}' + '.disqus.com/embed.js';
+ s.defer = true;
+ s.setAttribute('data-timestamp', +new Date());
+ // prevent the <script> from loading mutiple times by InstantClick
+ s.setAttribute('data-no-instant', '')
+ d.head.appendChild(s);
+ }
+ })();
+</script>
diff --git a/layouts/partials/disqus_script.html b/layouts/partials/disqus_script.html
deleted file mode 100644
index 4840d14..0000000
--- a/layouts/partials/disqus_script.html
+++ /dev/null
@@ -1,27 +0,0 @@
-<script type="text/javascript">
- var disqus_config = function () {
- this.page.url = '{{ .Page.Permalink }}'; // Replace PAGE_URL with your page's canonical URL variable
- this.page.identifier = '{{ .Page.Permalink | path.Base }}'; // Replace PAGE_IDENTIFIER with your page's unique identifier variable
- };
-
- (function () {
- let disqusEmbedLoaded = window.disqusEmbedLoaded;
- if (disqusEmbedLoaded) {
- // Use DISQUS.reset instead of reloading embed.js please.
- // https://help.disqus.com/en/articles/1717163-using-disqus-on-ajax-sites
- DISQUS.reset({
- reload: true,
- config: disqus_config,
- });
- } else {
- var d = document, s = d.createElement('script');
- s.src = '//' + '{{ site.Params.commentSystem.disqus.shortname }}' + '.disqus.com/embed.js';
- s.setAttribute('data-timestamp', +new Date());
- // prevent the <script> from loading mutiple times by InstantClick
- s.setAttribute('data-no-instant', '')
- d.head.appendChild(s);
-
- window.disqusEmbedLoaded = true;
- }
- })();
-</script>
diff --git a/layouts/partials/telegram_widget.html b/layouts/partials/telegram_widget.html
index 430c72e..1f614c7 100644
--- a/layouts/partials/telegram_widget.html
+++ b/layouts/partials/telegram_widget.html
@@ -1,4 +1,3 @@
-{{- with site.Params.commentSystem.telegramWidget }}
<div class="telegram-widget-container"></div>
<script>
@@ -34,6 +33,9 @@
if (!toggleThemeCallbacks) {
return;
}
+ if (document.getElementsByClassName('telegram-widget-container').length === 0) {
+ return;
+ }
toggleThemeCallbacks.push((isDark) => {
// console.log('window toggle-theme for telegram widget')
@@ -48,4 +50,3 @@
});
})();
</script>
-{{- end }}