site_js.html 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. {{ $scr := $.Scratch }}
  2. {{/* Attempt to load local vendor JS, otherwise load from CDN. */}}
  3. {{ $js := site.Data.assets.js }}
  4. {{ if not ($scr.Get "use_cdn") }}
  5. <script src="{{ printf "/js/vendor/%s" ($scr.Get "vendor_js_filename") | relURL }}"></script>
  6. {{ else }}
  7. {{ printf "<script src=\"%s\" integrity=\"%s\" crossorigin=\"anonymous\"></script>" (printf $js.jQuery.url $js.jQuery.version) $js.jQuery.sri | safeHTML }}
  8. {{ $require_isotope := site.Params.require_isotope | default true }}
  9. {{ if $require_isotope }}
  10. {{ printf "<script src=\"%s\" integrity=\"%s\" crossorigin=\"anonymous\"></script>" (printf $js.imagesLoaded.url $js.imagesLoaded.version) $js.imagesLoaded.sri | safeHTML }}
  11. {{ printf "<script src=\"%s\" integrity=\"%s\" crossorigin=\"anonymous\"></script>" (printf $js.isotope.url $js.isotope.version) $js.isotope.sri | safeHTML }}
  12. {{ end }}
  13. {{ printf "<script src=\"%s\" integrity=\"%s\" crossorigin=\"anonymous\"></script>" (printf $js.fancybox.url $js.fancybox.version) $js.fancybox.sri | safeHTML }}
  14. {{ printf "<script src=\"%s\" integrity=\"%s\" crossorigin=\"anonymous\"></script>" (printf $js.instantpage.url $js.instantpage.version) $js.instantpage.sri | safeHTML }}
  15. {{ if or .Params.diagram site.Params.diagram }}
  16. {{ printf "<script src=\"%s\" integrity=\"%s\" crossorigin=\"anonymous\" title=\"mermaid\"></script>" (printf $js.mermaid.url $js.mermaid.version) $js.mermaid.sri | safeHTML }}
  17. {{ end }}
  18. {{ if $.Scratch.Get "highlight_enabled" }}
  19. {{ $v := $js.highlight.version }}
  20. {{ printf "<script src=\"%s\" integrity=\"%s\" crossorigin=\"anonymous\"></script>" (printf $js.highlight.url $v) $js.highlight.sri | safeHTML }}
  21. {{ range site.Params.highlight_languages }}
  22. <script src="https://cdnjs.cloudflare.com/ajax/libs/highlight.js/{{ $v }}/languages/{{ . }}.min.js"></script>
  23. {{ end }}
  24. {{ end }}
  25. {{ end }}
  26. {{/* Maps JS. */}}
  27. {{ if eq site.Params.map.engine 1 }}
  28. <script async defer src="https://maps.googleapis.com/maps/api/js?key={{ site.Params.map.api_key }}"></script>
  29. {{ if ($scr.Get "use_cdn") }}
  30. {{ printf "<script src=\"%s\" integrity=\"%s\" crossorigin=\"anonymous\"></script>" (printf $js.gmaps.url $js.gmaps.version) $js.gmaps.sri | safeHTML }}
  31. {{ end }}
  32. {{ else if and (or (eq site.Params.map.engine 2) (eq site.Params.map.engine 3)) ($scr.Get "use_cdn") }}
  33. {{ printf "<script src=\"%s\" integrity=\"%s\" crossorigin=\"anonymous\"></script>" (printf $js.leaflet.url $js.leaflet.version) $js.leaflet.sri | safeHTML }}
  34. {{ end }}
  35. {{/* Initialise code highlighting. */}}
  36. {{ if $scr.Get "highlight_enabled" }}
  37. <script>const code_highlighting = true;</script>
  38. {{ else }}
  39. <script>const code_highlighting = false;</script>
  40. {{ end }}
  41. {{ if ne site.Params.search.engine 0 }}
  42. {{/* Configure search engine. */}}
  43. {{ $min_length := site.Params.search.wowchemy.min_length | default 1 }}
  44. {{ $threshold := site.Params.search.wowchemy.threshold | default 0.3 }}
  45. {{ $search_i18n := dict "placeholder" (i18n "search_placeholder") "results" (i18n "search_results") "no_results" (i18n "search_no_results") }}
  46. {{ $search_config := dict "indexURI" ("/index.json" | relLangURL) "threshold" $threshold "minLength" $min_length }}
  47. <script>
  48. const search_config = {{ $search_config | jsonify | safeJS }};
  49. const i18n = {{ $search_i18n | jsonify | safeJS }};
  50. const content_type = {
  51. 'post': {{ i18n "posts" }},
  52. 'project': {{ i18n "projects" }},
  53. 'publication' : {{ i18n "publications" }},
  54. 'event' : {{ i18n "talks" }},
  55. 'slides' : {{ i18n "slides" | default (i18n "btn_slides") }}
  56. };
  57. </script>
  58. {{ end }}
  59. {{/* Load hash anchors for documentation pages. */}}
  60. {{ if in (slice "book" "docs") .Type }}
  61. {{ printf "<script src=\"%s\" integrity=\"%s\" crossorigin=\"anonymous\"></script>" (printf $js.anchor.url $js.anchor.version) $js.anchor.sri | safeHTML }}
  62. <script>
  63. anchors.add();
  64. </script>
  65. {{ end }}
  66. {{ if eq site.Params.search.engine 1 }}
  67. {{/* Fuse search result template. */}}
  68. <script id="search-hit-fuse-template" type="text/x-template">
  69. <div class="search-hit" id="summary-{{"{{key}}"}}">
  70. <div class="search-hit-content">
  71. <div class="search-hit-name">
  72. {{ printf "<a href=\"%s\">%s</a>" "{{relpermalink}}" "{{title}}" | safeHTML }}
  73. <div class="article-metadata search-hit-type">{{"{{type}}"}}</div>
  74. <p class="search-hit-description">{{"{{snippet}}"}}</p>
  75. </div>
  76. </div>
  77. </div>
  78. </script>
  79. {{ else if eq site.Params.search.engine 2 }}
  80. {{/* Algolia search result template. */}}
  81. <script id="search-hit-algolia-template" type="text/html">
  82. <div class="search-hit">
  83. <div class="search-hit-content">
  84. <div class="search-hit-name">
  85. {{ printf "<a href=\"%s\">{{{_highlightResult.title.value}}}</a>" "{{relpermalink}}" | safeHTML }}
  86. </div>
  87. <div class="article-metadata search-hit-type">{{"{{type}}"}}</div>
  88. <p class="search-hit-description">{{ safeHTML "{{{_highlightResult.summary.value}}}" }}</p>
  89. </div>
  90. </div>
  91. </script>
  92. {{ end }}
  93. {{/* Fuse search engine. */}}
  94. {{ if and (eq site.Params.search.engine 1) ($scr.Get "use_cdn") }}
  95. {{ printf "<script src=\"%s\" integrity=\"%s\" crossorigin=\"anonymous\"></script>" (printf $js.fuse.url $js.fuse.version) $js.fuse.sri | safeHTML }}
  96. {{ printf "<script src=\"%s\" integrity=\"%s\" crossorigin=\"anonymous\"></script>" (printf $js.mark.url $js.mark.version) $js.mark.sri | safeHTML }}
  97. {{ end }}
  98. {{/* Algolia search engine. */}}
  99. {{ if eq site.Params.search.engine 2 }}
  100. {{ if ($scr.Get "use_cdn") }}
  101. {{ printf "<script src=\"%s\" integrity=\"%s\" crossorigin=\"anonymous\"></script>" (printf $js.instantsearch.url $js.instantsearch.version) $js.instantsearch.sri | safeHTML }}
  102. {{ end }}
  103. <script>
  104. const algoliaConfig = {
  105. appId: {{ site.Params.search.algolia.app_id }},
  106. apiKey: {{ site.Params.search.algolia.api_key }},
  107. indexName: {{ site.Params.search.algolia.index_name }},
  108. poweredBy: {{ site.Params.search.algolia.show_logo | default false }}
  109. };
  110. </script>
  111. {{ end }}
  112. {{/* Netlify Identity integration. */}}
  113. {{/* Complements loading of Netlify JS in `site_head`. */}}
  114. {{ if .IsHome | and (site.Params.cms.netlify_cms | default false) }}
  115. <script>
  116. if (window.netlifyIdentity) {
  117. window.netlifyIdentity.on("init", user => {
  118. if (!user) {
  119. window.netlifyIdentity.on("login", () => {
  120. document.location.href = "/admin/";
  121. });
  122. }
  123. });
  124. }
  125. </script>
  126. {{ end }}
  127. {{/* Charts */}}
  128. {{ if .HasShortcode "chart" }}
  129. <script>
  130. window.PlotlyConfig = {MathJaxConfig: 'local'};
  131. </script>
  132. {{ printf "<script src=\"%s\" integrity=\"%s\" crossorigin=\"anonymous\"></script>" (printf $js.plotly.url $js.plotly.version) $js.plotly.sri | safeHTML }}
  133. {{ end }}
  134. {{/* Disqus Comment Count JS. */}}
  135. {{ if and (eq site.Params.comments.engine 1) (site.Params.comments.disqus.show_count | default true) }}
  136. <script id="dsq-count-scr" src="https://{{site.Params.comments.disqus.shortname}}.disqus.com/count.js" async></script>
  137. {{ end }}
  138. {{ $js_license := printf "/*! Wowchemy v%s | https://wowchemy.com/ */\n" site.Data.wowchemy.version }}
  139. {{ $js_license := $js_license | printf "%s/*! Copyright 2016-present George Cushen (https://georgecushen.com/) */\n" }}
  140. {{ $js_license := $js_license | printf "%s/*! License: https://github.com/wowchemy/wowchemy-hugo-modules/blob/master/LICENSE.md */\n" }}
  141. {{ $js_bundle_head := $js_license | resources.FromString "js/bundle-head.js" }}
  142. {{ $js_linebreak := "\n" | resources.FromString "js/linebreak.js" }}{{/* Fix no line break after Bootstrap JS causing error. */}}
  143. {{ $js_params := dict "hugoEnvironment" hugo.Environment }}
  144. {{ $js_academic := resources.Get "js/wowchemy.js" | js.Build (dict "params" $js_params) }}
  145. {{ $js_bootstrap := resources.Get "js/_vendor/bootstrap.bundle.js" }}
  146. {{ $js_bundle := slice $js_bootstrap $js_linebreak $js_academic }}
  147. {{ if eq site.Params.search.engine 1 }}
  148. {{ $js_academic_search := resources.Get "js/wowchemy-search.js" }}
  149. {{ $js_bundle = $js_bundle | append $js_academic_search }}
  150. {{ else if eq site.Params.search.engine 2 }}
  151. {{ $js_algolia_search := resources.Get "js/algolia-search.js" }}
  152. {{ $js_bundle = $js_bundle | append $js_algolia_search }}
  153. {{ end }}
  154. {{ range site.Params.plugins_js }}
  155. {{ $js_bundle = $js_bundle | append (resources.Get (printf "js/%s.js" .)) }}
  156. {{ end }}
  157. {{ $js_bundle := $js_bundle | resources.Concat "js/wowchemy-bundle-pre.js" }}
  158. {{- if eq hugo.Environment "production" -}}
  159. {{ $js_bundle = $js_bundle | minify }}
  160. {{- end -}}
  161. {{ $js_bundle := slice $js_bundle_head $js_bundle | resources.Concat "js/wowchemy.min.js" }}
  162. {{- if eq hugo.Environment "production" -}}
  163. {{- $js_bundle = $js_bundle | fingerprint "md5" -}}
  164. {{- end -}}
  165. <script src="{{ $js_bundle.RelPermalink }}"></script>
  166. {{ partial "custom_js" . }}