Skip to content

Commit 2e1e493

Browse files
yihuianthonyfok
authored andcommitted
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
1 parent a544049 commit 2e1e493

File tree

1 file changed

+10
-6
lines changed

1 file changed

+10
-6
lines changed

‎tpl/tplimpl/template_embedded.go‎

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -167,14 +167,18 @@ func (t *templateHandler) embedTemplates() {
167167

168168
t.addInternalTemplate("", "disqus.html", `{{ if .Site.DisqusShortname }}<div id="disqus_thread"></div>
169169
<script type="text/javascript">
170-
var disqus_shortname = '{{ .Site.DisqusShortname }}';
171-
var disqus_identifier = '{{with .GetParam "disqus_identifier" }}{{ . }}{{ else }}{{ .Permalink }}{{end}}';
172-
var disqus_title = '{{with .GetParam "disqus_title" }}{{ . }}{{ else }}{{ .Title }}{{end}}';
173-
var disqus_url = '{{with .GetParam "disqus_url" }}{{ . | html }}{{ else }}{{ .Permalink }}{{end}}';
174-
170+
var disqus_config = function () {
171+
{{with .GetParam "disqus_identifier" }}this.page.identifier = '{{ . }}';{{end}}
172+
{{with .GetParam "disqus_title" }}this.page.title = '{{ . }}';{{end}}
173+
{{with .GetParam "disqus_url" }}this.page.url = '{{ . | html }}';{{end}}
174+
};
175175
(function() {
176+
if (["localhost", "127.0.0.1"].indexOf(window.location.hostname) != -1) {
177+
document.getElementById('disqus_thread').innerHTML = 'Disqus comments not available by default when the website is previewed locally.';
178+
return;
179+
}
176180
var dsq = document.createElement('script'); dsq.type = 'text/javascript'; dsq.async = true;
177-
dsq.src = '//' + disqus_shortname + '.disqus.com/embed.js';
181+
dsq.src = '//' + {{ .Site.DisqusShortname }} + '.disqus.com/embed.js';
178182
(document.getElementsByTagName('head')[0] || document.getElementsByTagName('body')[0]).appendChild(dsq);
179183
})();
180184
</script>

0 commit comments

Comments
 (0)