mirror of
https://github.com/gohugoio/hugo.git
synced 2025-04-30 15:40:05 +03:00
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
This commit is contained in:
parent
a54404968a
commit
2e1e4934b6
1 changed files with 10 additions and 6 deletions
|
@ -167,14 +167,18 @@ func (t *templateHandler) embedTemplates() {
|
||||||
|
|
||||||
t.addInternalTemplate("", "disqus.html", `{{ if .Site.DisqusShortname }}<div id="disqus_thread"></div>
|
t.addInternalTemplate("", "disqus.html", `{{ if .Site.DisqusShortname }}<div id="disqus_thread"></div>
|
||||||
<script type="text/javascript">
|
<script type="text/javascript">
|
||||||
var disqus_shortname = '{{ .Site.DisqusShortname }}';
|
var disqus_config = function () {
|
||||||
var disqus_identifier = '{{with .GetParam "disqus_identifier" }}{{ . }}{{ else }}{{ .Permalink }}{{end}}';
|
{{with .GetParam "disqus_identifier" }}this.page.identifier = '{{ . }}';{{end}}
|
||||||
var disqus_title = '{{with .GetParam "disqus_title" }}{{ . }}{{ else }}{{ .Title }}{{end}}';
|
{{with .GetParam "disqus_title" }}this.page.title = '{{ . }}';{{end}}
|
||||||
var disqus_url = '{{with .GetParam "disqus_url" }}{{ . | html }}{{ else }}{{ .Permalink }}{{end}}';
|
{{with .GetParam "disqus_url" }}this.page.url = '{{ . | html }}';{{end}}
|
||||||
|
};
|
||||||
(function() {
|
(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;
|
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);
|
(document.getElementsByTagName('head')[0] || document.getElementsByTagName('body')[0]).appendChild(dsq);
|
||||||
})();
|
})();
|
||||||
</script>
|
</script>
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue