    {{ $scr := $.Scratch }}

    {{/* Config LaTeX math rendering. */}}
    {{ if or .Params.math .Site.Params.math }}
    <script type="text/x-mathjax-config">
      MathJax.Hub.Config({
        CommonHTML: { linebreaks: { automatic: true } },
        tex2jax: { inlineMath: [ ['$', '$'], ['\\(','\\)'] ], displayMath: [ ['$$','$$'], ['\\[', '\\]'] ], processEscapes: false },
        TeX: { noUndefined: { attributes: { mathcolor: 'red', mathbackground: '#FFEEEE', mathsize: '90%' } } },
        messageStyle: 'none'
      });
    </script>
    {{ end }}

    {{/* Attempt to load local vendor JS, otherwise load from CDN. */}}
    {{ $js := .Site.Data.assets.js }}
    {{ if not ($scr.Get "use_cdn") }}
      <script src="{{ printf "/js/vendor/%s" ($scr.Get "vendor_js_filename") | relURL }}"></script>
    {{ else }}
      {{ printf "<script src=\"%s\" integrity=\"%s\" crossorigin=\"anonymous\"></script>" (printf $js.jQuery.url $js.jQuery.version) $js.jQuery.sri | safeHTML }}
      {{ printf "<script src=\"%s\" integrity=\"%s\" crossorigin=\"anonymous\"></script>" (printf $js.imagesLoaded.url $js.imagesLoaded.version) $js.imagesLoaded.sri | safeHTML }}
      {{ printf "<script src=\"%s\" integrity=\"%s\" crossorigin=\"anonymous\"></script>" (printf $js.bootstrap.url $js.bootstrap.version) $js.bootstrap.sri | safeHTML }}
      {{ printf "<script src=\"%s\" integrity=\"%s\" crossorigin=\"anonymous\"></script>" (printf $js.isotope.url $js.isotope.version) $js.isotope.sri | safeHTML }}
      {{ printf "<script src=\"%s\" integrity=\"%s\" crossorigin=\"anonymous\"></script>" (printf $js.fancybox.url $js.fancybox.version) $js.fancybox.sri | safeHTML }}

      {{ if $.Scratch.Get "highlight_enabled" }}
        {{ $v := $js.highlight.version }}
        {{ printf "<script src=\"%s\" integrity=\"%s\" crossorigin=\"anonymous\"></script>" (printf $js.highlight.url $v) $js.highlight.sri | safeHTML }}
        {{ range .Site.Params.highlight_languages }}
        <script src="//cdnjs.cloudflare.com/ajax/libs/highlight.js/{{ $v }}/languages/{{ . }}.min.js"></script>
        {{ end }}
      {{ end }}

      {{/* LaTeX math rendering. */}}
      {{ if or .Params.math .Site.Params.math }}
      {{ printf "<script src=\"%s\" integrity=\"%s\" crossorigin=\"anonymous\" async></script>" (printf $js.mathJax.url $js.mathJax.version) $js.mathJax.sri | safeHTML }}
      {{ end }}
    {{ end }}

    <script src="{{ "/js/hugo-academic.js" | relURL }}"></script>
    {{ range .Site.Params.custom_js }}
    <script src="{{ "/js/" | relURL }}{{ . }}"></script>
    {{ end }}

    {{/* Maps JS. */}}
    {{ if eq .Site.Params.map 1 }}
      <script async defer src="//maps.googleapis.com/maps/api/js?key={{ .Site.Params.map_api_key }}"></script>
      {{ if ($scr.Get "use_cdn") }}
      {{ printf "<script src=\"%s\" integrity=\"%s\" crossorigin=\"anonymous\"></script>" (printf $js.gmaps.url $js.gmaps.version) $js.gmaps.sri | safeHTML }}
      {{ end }}
    {{ else if and (or (eq .Site.Params.map 2) (eq .Site.Params.map 3)) ($scr.Get "use_cdn") }}
      {{ printf "<script src=\"%s\" integrity=\"%s\" crossorigin=\"anonymous\"></script>" (printf $js.leaflet.url $js.leaflet.version) $js.leaflet.sri | safeHTML }}
    {{ end }}

    {{/* Comments JS. */}}
    {{ $comments_enabled := and $.Site.DisqusShortname (not (or $.Site.Params.disable_comments $.Params.disable_comments)) }}
    {{ if and $comments_enabled ($.Site.Params.comment_count | default true) }}
    <script id="dsq-count-scr" src="//{{ .Site.DisqusShortname }}.disqus.com/count.js" async></script>
    {{ end }}

    {{/* Initialise code highlighting. */}}
    {{ if $.Scratch.Get "highlight_enabled" }}
    <script>hljs.initHighlightingOnLoad();</script>
    {{ end }}

    {{ if ne .Site.Params.search.engine 0 }}
    {{/* Configure search engine. */}}
    <script>
      const search_index_filename = {{ "/index.json" | relURL }};
      const i18n = {
        'placeholder': {{ i18n "search_placeholder" }},
        'results': {{ i18n "search_results" }},
        'no_results': {{ i18n "search_no_results" }}
      };
      const content_type = {
        'post': {{ i18n "posts" }},
        'project': {{ i18n "projects" }},
        'publication' : {{ i18n "publications" }},
        'talk' : {{ i18n "talks" }}
        };
    </script>
    {{ end }}

    {{/* Load hash anchors for documentation pages. */}}
    {{ if eq .Type "docs" }}
    {{ printf "<script src=\"%s\" integrity=\"%s\" crossorigin=\"anonymous\"></script>" (printf $js.anchor.url $js.anchor.version) $js.anchor.sri | safeHTML }}
    <script>
      anchors.add();
    </script>
    {{ end }}

    {{ if eq $.Site.Params.search.engine 1 }}
    {{/* Fuse search result template. */}}
    <script id="search-hit-fuse-template" type="text/x-template">
      <div class="search-hit" id="summary-{{"{{key}}"}}">
      <div class="search-hit-content">
        <div class="search-hit-name">
          {{ printf "<a href=\"%s\">%s</a>" "{{relpermalink}}" "{{title}}" | safeHTML }}
          <div class="article-metadata search-hit-type">{{"{{type}}"}}</div>
          <p class="search-hit-description">{{"{{snippet}}"}}</p>
        </div>
      </div>
      </div>
    </script>
    {{ else if eq $.Site.Params.search.engine 2 }}
    {{/* Algolia search result template. */}}
    <script id="search-hit-algolia-template" type="text/html">
      <div class="search-hit">
        <div class="search-hit-content">
          <div class="search-hit-name">
            {{ printf "<a href=\"%s\">{{{_highlightResult.title.value}}}</a>" "{{relpermalink}}" | safeHTML }}
          </div>
          <div class="article-metadata search-hit-type">{{"{{type}}"}}</div>
          <p class="search-hit-description">{{ safeHTML "{{{_highlightResult.summary.value}}}" }}</p>
        </div>
      </div>
    </script>
    {{ end }}

    {{/* Fuse search engine. */}}
    {{ if eq .Site.Params.search.engine 1 }}
    {{ printf "<script src=\"%s\" integrity=\"%s\" crossorigin=\"anonymous\"></script>" (printf $js.fuse.url $js.fuse.version) $js.fuse.sri | safeHTML }}
    {{ printf "<script src=\"%s\" integrity=\"%s\" crossorigin=\"anonymous\"></script>" (printf $js.mark.url $js.mark.version) $js.mark.sri | safeHTML }}
    <script src="{{ "/js/search.js" | relURL }}"></script>
    {{ end }}

    {{/* Algolia search engine. */}}
    {{ if eq .Site.Params.search.engine 2 }}
    {{ if ($scr.Get "use_cdn") }}
    {{ printf "<script src=\"%s\" integrity=\"%s\" crossorigin=\"anonymous\"></script>" (printf $js.instantsearch.url $js.instantsearch.version) $js.instantsearch.sri | safeHTML }}
    {{ end }}
    <script>
      if ( $('#search-box').length ) {
        function getTemplate(templateName) {
          return document.querySelector(`#${templateName}-template`).innerHTML;
        }

        const options = {
          appId: {{ .Site.Params.search.algolia.app_id }},
          apiKey: {{ .Site.Params.search.algolia.api_key }},
          indexName: {{ .Site.Params.search.algolia.index_name }},
          routing: true,
          searchParameters: {
            hitsPerPage: 10
          },
          searchFunction: function(helper) {
            let searchResults = document.querySelector('#search-hits')
            if (helper.state.query === '') {
              searchResults.style.display = 'none';
              return;
            }
            helper.search();
            searchResults.style.display = 'block';
          }
        };

        const search = instantsearch(options);

        // Initialize search box.
        search.addWidget(
          instantsearch.widgets.searchBox({
            container: '#search-box',
            autofocus: false,
            reset: true,
            poweredBy: {{ .Site.Params.search.algolia.show_logo | default false }},
            placeholder: i18n.placeholder
          })
        );

        // Initialize search results.
        search.addWidget(
          instantsearch.widgets.infiniteHits({
            container: '#search-hits',
            escapeHits: true,
            templates: {
              empty: '<div class="search-no-results">' + i18n.no_results + '</div>',
              item: getTemplate('search-hit-algolia')
            },
            cssClasses: {
              showmoreButton: 'btn btn-outline-primary'
            }
          })
        );

        // On render search results, localize the content type metadata.
        search.on('render', function() {
          $('.search-hit-type').each(function( index ) {
            let content_key = $( this ).text();
            if (content_key in content_type) {
              $( this ).text(content_type[content_key]);
            }
          });
        });

        // Start search.
        search.start();
      }
    </script>
    {{ end }}

  </body>
</html>
