Skip to content

Commit

Permalink
tpl: Improve the built-in Disqus template (#3639)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
yihui authored and anthonyfok committed Jun 27, 2017
1 parent a544049 commit 2e1e493
Showing 1 changed file with 10 additions and 6 deletions.
16 changes: 10 additions & 6 deletions tpl/tplimpl/template_embedded.go
Original file line number Diff line number Diff line change
Expand Up @@ -167,14 +167,18 @@ func (t *templateHandler) embedTemplates() {

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>
Expand Down

0 comments on commit 2e1e493

Please sign in to comment.