support specifying comment system in both config and front matter

in config: site.Params.defaultCommentSystems
in front matter: pageCommentSystems
This commit is contained in:
Reorx 2022-05-31 22:41:10 +08:00
parent 81f885a7b9
commit 5c47607d23
4 changed files with 52 additions and 37 deletions

View File

@ -1,13 +1,27 @@
{{- /* You can add your own layouts/comments.html to override this file */ -}} {{- /* You can add your own layouts/comments.html to override this file */ -}}
{{- with site.Params.commentSystem.remark42 -}} {{- $pageCommentSystems := .Param "pageCommentSystems"}}
{{- partial "remark42.html" . }} {{- if not $pageCommentSystems }}
{{- end -}} {{- $pageCommentSystems = site.Params.defaultCommentSystems }}
{{- end }}
{{- with site.Params.commentSystem.telegramWidget -}} {{- $page := . -}}
{{- partial "telegram_widget.html" . }} {{- with site.Params.commentSystems -}}
{{- end -}} {{- if $pageCommentSystems.remark42 -}}
{{- with .remark42 -}}
{{- partial "remark42.html" . }}
{{- end -}}
{{- end -}}
{{- with site.Params.commentSystem.disqus -}} {{- if $pageCommentSystems.telegramWidget -}}
{{- partial "disqus.html" . }} {{- with .telegramWidget -}}
{{- partial "telegram_widget.html" . }}
{{- end -}}
{{- end -}}
{{- if $pageCommentSystems.disqus -}}
{{- with .disqus -}}
{{- partial "disqus.html" (dict "page" $page "disqus" .) }}
{{- end -}}
{{- end -}}
{{- end -}} {{- end -}}

View File

@ -1,3 +1,30 @@
<div class="disqus-container"> <div class="disqus-container">
<div id="disqus_thread"></div> <div id="disqus_thread"></div>
</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>

View File

@ -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>

View File

@ -1,4 +1,3 @@
{{- with site.Params.commentSystem.telegramWidget }}
<div class="telegram-widget-container"></div> <div class="telegram-widget-container"></div>
<script> <script>
@ -34,6 +33,9 @@
if (!toggleThemeCallbacks) { if (!toggleThemeCallbacks) {
return; return;
} }
if (document.getElementsByClassName('telegram-widget-container').length === 0) {
return;
}
toggleThemeCallbacks.push((isDark) => { toggleThemeCallbacks.push((isDark) => {
// console.log('window toggle-theme for telegram widget') // console.log('window toggle-theme for telegram widget')
@ -48,4 +50,3 @@
}); });
})(); })();
</script> </script>
{{- end }}