site_js.html 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  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. {{ printf "<script src=\"%s\" integrity=\"%s\" crossorigin=\"anonymous\"></script>" (printf $js.instantpage.url $js.instantpage.version) $js.instantpage.sri | safeHTML }}
  9. {{ $require_isotope := site.Params.require_isotope | default true }}
  10. {{ if $require_isotope }}
  11. {{ printf "<script src=\"%s\" integrity=\"%s\" crossorigin=\"anonymous\"></script>" (printf $js.imagesLoaded.url $js.imagesLoaded.version) $js.imagesLoaded.sri | safeHTML }}
  12. {{ printf "<script src=\"%s\" integrity=\"%s\" crossorigin=\"anonymous\"></script>" (printf $js.isotope.url $js.isotope.version) $js.isotope.sri | safeHTML }}
  13. {{ end }}
  14. {{/* Workaround `.HasShortcode "gallery"` detection issue on `home/` WidgetPage v1 */}}
  15. {{ if ($scr.Get "HasNestedGalleryShortcode") | or site.Params.require_fancybox }}
  16. {{ printf "<script src=\"%s\" integrity=\"%s\" crossorigin=\"anonymous\"></script>" (printf $js.fancybox.url $js.fancybox.version) $js.fancybox.sri | safeHTML }}
  17. {{ end }}
  18. {{ if or .Params.diagram site.Params.diagram }}
  19. {{ printf "<script src=\"%s\" integrity=\"%s\" crossorigin=\"anonymous\" title=\"mermaid\"></script>" (printf $js.mermaid.url $js.mermaid.version) $js.mermaid.sri | safeHTML }}
  20. {{ end }}
  21. {{ if $.Scratch.Get "highlight_enabled" }}
  22. {{ $v := $js.highlight.version }}
  23. {{ printf "<script src=\"%s\" integrity=\"%s\" crossorigin=\"anonymous\"></script>" (printf $js.highlight.url $v) $js.highlight.sri | safeHTML }}
  24. {{ $v := $js.highlight_lang.version }}
  25. {{ range site.Params.highlight_languages }}
  26. {{ printf "<script src=\"%s\" crossorigin=\"anonymous\"></script>" (printf $js.highlight_lang.url $v .) | safeHTML }}
  27. {{ end }}
  28. {{ end }}
  29. {{ end }}
  30. {{/* Maps JS. */}}
  31. {{ $map_provider := lower site.Params.map.provider }}
  32. {{ if eq $map_provider "google" }}
  33. <script async defer src="https://maps.googleapis.com/maps/api/js?key={{ site.Params.map.api_key }}"></script>
  34. {{ if ($scr.Get "use_cdn") }}
  35. {{ printf "<script src=\"%s\" integrity=\"%s\" crossorigin=\"anonymous\"></script>" (printf $js.gmaps.url $js.gmaps.version) $js.gmaps.sri | safeHTML }}
  36. {{ end }}
  37. {{ else if (in (slice "mapnik" "mapbox") $map_provider) | and ($scr.Get "use_cdn") }}
  38. {{ printf "<script src=\"%s\" integrity=\"%s\" crossorigin=\"anonymous\"></script>" (printf $js.leaflet.url $js.leaflet.version) $js.leaflet.sri | safeHTML }}
  39. {{ end }}
  40. {{/* Load hash anchors for documentation pages. */}}
  41. {{ if in (slice "book" "docs") .Type }}
  42. {{ printf "<script src=\"%s\" integrity=\"%s\" crossorigin=\"anonymous\"></script>" (printf $js.anchor.url $js.anchor.version) $js.anchor.sri | safeHTML }}
  43. <script>
  44. anchors.add();
  45. </script>
  46. {{ end }}
  47. {{ $algoliaConfig := dict }}
  48. {{ $search_provider := lower site.Params.search.provider }}
  49. {{ if eq $search_provider "wowchemy" }}
  50. {{/* Wowchemy built-in search result template (Fuse). */}}
  51. <script id="search-hit-fuse-template" type="text/x-template">
  52. <div class="search-hit" id="summary-{{"{{key}}"}}">
  53. <div class="search-hit-content">
  54. <div class="search-hit-name">
  55. {{ printf "<a href=\"%s\">%s</a>" "{{relpermalink}}" "{{title}}" | safeHTML }}
  56. <div class="article-metadata search-hit-type">{{"{{type}}"}}</div>
  57. <p class="search-hit-description">{{"{{snippet}}"}}</p>
  58. </div>
  59. </div>
  60. </div>
  61. </script>
  62. {{ if ($scr.Get "use_cdn") }}
  63. {{ printf "<script src=\"%s\" integrity=\"%s\" crossorigin=\"anonymous\"></script>" (printf $js.fuse.url $js.fuse.version) $js.fuse.sri | safeHTML }}
  64. {{ printf "<script src=\"%s\" integrity=\"%s\" crossorigin=\"anonymous\"></script>" (printf $js.mark.url $js.mark.version) $js.mark.sri | safeHTML }}
  65. {{ end }}
  66. {{ else if eq $search_provider "algolia" }}
  67. {{/* Algolia search result template. */}}
  68. <script id="search-hit-algolia-template" type="text/html">
  69. <div class="search-hit">
  70. <div class="search-hit-content">
  71. <div class="search-hit-name">
  72. {{ printf "<a href=\"%s\">{{{_highlightResult.title.value}}}</a>" "{{relpermalink}}" | safeHTML }}
  73. </div>
  74. <div class="article-metadata search-hit-type">{{"{{type}}"}}</div>
  75. <p class="search-hit-description">{{ safeHTML "{{{_highlightResult.summary.value}}}" }}</p>
  76. </div>
  77. </div>
  78. </script>
  79. {{ if ($scr.Get "use_cdn") }}
  80. {{ printf "<script src=\"%s\" integrity=\"%s\" crossorigin=\"anonymous\"></script>" (printf $js.instantsearch.url $js.instantsearch.version) $js.instantsearch.sri | safeHTML }}
  81. {{ end }}
  82. {{ $algoliaConfig = dict "appId" (site.Params.search.algolia.app_id | default "") "apiKey" (site.Params.search.algolia.api_key | default "") "indexName" (site.Params.search.algolia.index_name | default "") "poweredBy" (site.Params.search.algolia.show_logo | default false) }}
  83. {{ end }}
  84. {{/* Charts */}}
  85. {{ if .HasShortcode "chart" }}
  86. {{ printf "<script src=\"%s\" integrity=\"%s\" crossorigin=\"anonymous\"></script>" (printf $js.plotly.url $js.plotly.version) $js.plotly.sri | safeHTML }}
  87. {{ end }}
  88. {{/* Disqus Comment Count JS. */}}
  89. {{/* Note: count can appear both in listing previews and on the content page itself. */}}
  90. {{ $comments_provider := trim (site.Params.comments.provider | lower) " " }}
  91. {{ if eq $comments_provider "disqus" | and (site.Params.comments.disqus.show_count | default true) }}
  92. <script id="dsq-count-scr" src="https://{{site.Params.comments.disqus.shortname}}.disqus.com/count.js" async></script>
  93. {{end}}
  94. {{/* Rename as GitHub Pages doesn't automatically publish folders with underscores. */}}
  95. {{ $js_bootstrap := slice (resources.Get "js/_vendor/bootstrap.bundle.min.js") | resources.Concat "js/bootstrap.bundle.min.js" }}
  96. {{- if hugo.IsProduction -}}
  97. {{ $js_bootstrap = $js_bootstrap | fingerprint "md5" }}{{/* Note: no `minify` as using pre-minified JS. */}}
  98. {{- end -}}
  99. <script src="{{ $js_bootstrap.RelPermalink }}"></script>
  100. {{ $js_search_params := dict }}
  101. {{ if in (slice "wowchemy" "algolia") $search_provider }}
  102. {{/* Configure search engine. */}}
  103. {{ $min_length := site.Params.search.wowchemy.min_length | default 1 }}
  104. {{ $threshold := site.Params.search.wowchemy.threshold | default 0.3 }}
  105. {{ $search_config := dict "indexURI" ("/index.json" | relLangURL) "threshold" $threshold "minLength" $min_length }}
  106. {{ $search_i18n := dict "placeholder" (i18n "search_placeholder") "results" (i18n "search_results") "no_results" (i18n "search_no_results") }}
  107. {{ $content_types := dict "post" (i18n "posts") "project" (i18n "projects") "publication" (i18n "publications") "event" (i18n "talks") "slides" (i18n "slides") "authors" (i18n "authors") }}
  108. {{ $js_search_params = dict "search_config" $search_config "algoliaConfig" $algoliaConfig "i18n" $search_i18n "content_type" $content_types }}
  109. {{ end }}
  110. {{ $js_license := printf "/*! Wowchemy v%s | https://wowchemy.com/ */\n" site.Data.wowchemy.version }}
  111. {{ $js_license := $js_license | printf "%s/*! Copyright 2016-present George Cushen (https://georgecushen.com/) */\n" }}
  112. {{ $js_license := $js_license | printf "%s/*! License: https://github.com/wowchemy/wowchemy-hugo-modules/blob/main/LICENSE.md */\n" }}
  113. {{ $js_bundle_head := $js_license | resources.FromString "js/bundle-head.js" }}
  114. {{ $js_params := dict "hugoEnvironment" hugo.Environment "codeHighlighting" ($scr.Get "highlight_enabled" | default false) "searchEnabled" (in (slice "wowchemy" "algolia") $search_provider) }}
  115. {{ $js_academic := resources.Get "js/wowchemy.js" | js.Build (dict "targetPath" (printf "%s/js/wow-core.js" .Lang ) "params" $js_params) }}
  116. {{ $js_bundle := slice $js_academic }}
  117. {{ if eq $search_provider "wowchemy" }}
  118. {{ $js_academic_search := resources.Get "js/wowchemy-search.js" | js.Build (dict "targetPath" (printf "%s/js/wow-search-built.js" .Lang) "params" $js_search_params) }}
  119. {{ $js_bundle = $js_bundle | append $js_academic_search }}
  120. {{ else if eq $search_provider "algolia" }}
  121. {{ $js_algolia_search := resources.Get "js/algolia-search.js" | js.Build (dict "targetPath" (printf "%s/js/algolia-search-built.js" .Lang) "params" $js_search_params) }}
  122. {{ $js_bundle = $js_bundle | append $js_algolia_search }}
  123. {{ end }}
  124. {{ range site.Params.plugins_js }}
  125. {{ $js_bundle = $js_bundle | append (resources.Get (printf "js/%s.js" .)) }}
  126. {{ end }}
  127. {{ $js_bundle = $js_bundle | resources.Concat (printf "%s/js/wowchemy-bundle.js" .Lang) }}
  128. {{- if hugo.IsProduction -}}
  129. {{- $js_bundle = $js_bundle | js.Build (dict "format" "iife") | minify -}}
  130. {{- else -}}
  131. {{- $js_bundle = $js_bundle | js.Build (dict "format" "iife" "sourceMap" "inline") -}}
  132. {{- end -}}
  133. {{ $js_bundle = slice $js_bundle_head $js_bundle | resources.Concat (printf "%s/js/wowchemy.min.js" .Lang) }}
  134. {{- if hugo.IsProduction -}}
  135. {{ $js_bundle = $js_bundle | fingerprint "md5" }}
  136. {{- end -}}
  137. <script src="{{ $js_bundle.RelPermalink }}"></script>
  138. {{ partial "custom_js" . }}