shithub: hugo

Download patch

ref: 2e1e4934b60ce8081a7f3a79191ed204f3098481
parent: a54404968a4b36579797f2e7ff7f5eada94866d9
author: Yihui Xie <[email protected]>
date: Tue Jun 27 02:40:01 EDT 2017

tpl: Improve the built-in Disqus template (#3639)

* Improve the built-in Disqus template

  Set `disqus_identifier`, `disqus_title`, and `disqus_url`
  only if the user has explicitly provided them.

  Do not load Disqus when the website is previewed locally,
  otherwise it is very confusing.

* Use disqus_config instead of three global variables

  https://help.disqus.com/customer/portal/articles/472098-javascript-configuration-variables

--- a/tpl/tplimpl/template_embedded.go
+++ b/tpl/tplimpl/template_embedded.go
@@ -167,14 +167,18 @@
 
 	t.addInternalTemplate("", "disqus.html", `{{ if .Site.DisqusShortname }}<div id="disqus_thread"></div>
 <script type="text/javascript">
-    var disqus_shortname = '{{ .Site.DisqusShortname }}';
-    var disqus_identifier = '{{with .GetParam "disqus_identifier" }}{{ . }}{{ else }}{{ .Permalink }}{{end}}';
-    var disqus_title = '{{with .GetParam "disqus_title" }}{{ . }}{{ else }}{{ .Title }}{{end}}';
-    var disqus_url = '{{with .GetParam "disqus_url" }}{{ . | html  }}{{ else }}{{ .Permalink }}{{end}}';
-
+    var disqus_config = function () {
+    {{with .GetParam "disqus_identifier" }}this.page.identifier = '{{ . }}';{{end}}
+    {{with .GetParam "disqus_title" }}this.page.title = '{{ . }}';{{end}}
+    {{with .GetParam "disqus_url" }}this.page.url = '{{ . | html  }}';{{end}}
+    };
     (function() {
+        if (["localhost", "127.0.0.1"].indexOf(window.location.hostname) != -1) {
+            document.getElementById('disqus_thread').innerHTML = 'Disqus comments not available by default when the website is previewed locally.';
+            return;
+        }
         var dsq = document.createElement('script'); dsq.type = 'text/javascript'; dsq.async = true;
-        dsq.src = '//' + disqus_shortname + '.disqus.com/embed.js';
+        dsq.src = '//' + {{ .Site.DisqusShortname }} + '.disqus.com/embed.js';
         (document.getElementsByTagName('head')[0] || document.getElementsByTagName('body')[0]).appendChild(dsq);
     })();
 </script>