Skip to content

Instantly share code, notes, and snippets.

@pduchnovsky
Last active December 11, 2023 11:39
Show Gist options
  • Save pduchnovsky/b7d599fd1a99b745b374812aa6d301eb to your computer and use it in GitHub Desktop.
Save pduchnovsky/b7d599fd1a99b745b374812aa6d301eb to your computer and use it in GitHub Desktop.
Disqus 'on-click' for your Hugo generated pages | https://duchnovsky.com/2020/11/disqus-on-click-for-hugo/
<!-- Adapted/developed by pduchnovsky, https://duchnovsky.com/2020/11/disqus-on-click-for-hugo/ -->
{{- if and (not (eq (.Site.DisqusShortname | default "") "")) (eq (.Params.disableComments | default false) false) -}}
{{- if .Site.Params.disqusOnClick -}}
{{- $pc := .Site.Config.Privacy.Disqus -}}
{{- if not $pc.Disable -}}
{{- if .Site.DisqusShortname -}}
<section id="disqus" style="text-align: center">
<button
class="disqus-comment-count"
data-disqus-identifier="{{ .File.UniqueID }}"
id="show-comments"
onclick="disqus();return false;"
>
Comments
</button>
<div id="disqus_thread"></div>
<script>
window.onload = function () {
if (["localhost", "127.0.0.1"].indexOf(window.location.hostname) != -1) {
document.getElementById("disqus_thread").innerHTML =
"Disqus comments not available when the website is previewed locally.";
document.getElementById("show-comments").style.display = "none";
return;
}
};
var disqus_loaded = false;
var disqus_config = function () {
this.page.identifier = "{{ .File.UniqueID }}";
this.page.title = "{{ .Title }}";
this.page.url = "{{ .Permalink | html }}";
};
function disqus() {
if (!disqus_loaded) {
disqus_loaded = true;
var e = document.createElement("script");
e.type = "text/javascript";
e.async = true;
e.src = "//{{ .Site.DisqusShortname }}.disqus.com/embed.js";
(
document.getElementsByTagName("head")[0] ||
document.getElementsByTagName("body")[0]
).appendChild(e);
document.getElementById("show-comments").style.display = "none";
}
}
var hash = window.location.hash.substr(1);
if (hash.length > 8) {
if (hash.substring(0, 8) == "comment-") {
disqus();
}
}
if (
/bot|google|baidu|bing|msn|duckduckgo|slurp|yandex/i.test(
navigator.userAgent
)
) {
disqus();
}
</script>
<script
id="dsq-count-scr"
src="https://{{ .Site.DisqusShortname }}.disqus.com/count.js"
async
></script>
<noscript>
Please enable JavaScript to view the
<a href="https://disqus.com/?ref_noscript"> comments powered by Disqus. </a>
</noscript>
<a href="https://disqus.com" class="dsq-brlink">
powered by <span class="logo-disqus">Disqus</span>
</a>
</section>
{{- end -}}
{{- end -}}
{{- else -}}
{{ template "_internal/disqus.html" . }}
{{- end -}}
{{- end -}}
button {
color: white !important;
background-color: #1565c0;
border: none;
border-radius: 5px;
padding: 10px 20px;
margin: 6px 4px;
font-size: 1.6rem;
transition-duration: 0.4s;
cursor: pointer;
}
button:hover {
background-color: #42a5f5;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment