{{/*  Header  */}}
{{ $header_js := resources.Get "js/header.js" }}
{{- if hugo.IsProduction -}}
    {{ $header_js = $header_js | js.Build (dict "minify" true) | resources.Fingerprint "sha512" -}}
{{- else -}}
    {{ $header_js = $header_js | js.Build -}}
{{- end -}}
<script defer src="{{ $header_js.RelPermalink }}" integrity="{{- $header_js.Data.Integrity -}}"></script>

{{/*  Zooming.js  */}}
{{ if (.Scratch.Get "params").enable_zooming_js }}
    {{ $zooming_js := resources.Get "js/zooming.js" }}
    {{- if hugo.IsProduction -}}
        {{ $zooming_js = $zooming_js | js.Build (dict "minify" true) | resources.Fingerprint "sha512" -}}
    {{- else -}}
        {{ $zooming_js = $zooming_js | js.Build -}}
    {{- end -}}
    <script defer src="{{ $zooming_js.RelPermalink }}" integrity="{{- $zooming_js.Data.Integrity -}}"></script>
{{ end }}

{{/* MathJax */}}
{{ if (.Scratch.Get "params").math }}
    {{ $math_js := resources.Get "js/math.js" }}
    {{- if hugo.IsProduction -}}
        {{ $math_js = $math_js | js.Build (dict "minify" true) | resources.Fingerprint "sha512" -}}
    {{- else -}}
        {{ $math_js = $math_js | js.Build -}}
    {{- end -}}
    <script type="text/javascript" id="MathJax-script" async src="https://cdn.jsdelivr.net/npm/mathjax@3/es5/tex-mml-chtml.js"></script>
    <script defer src="{{ $math_js.RelPermalink }}" integrity="{{- $math_js.Data.Integrity -}}"></script>
{{ end }}

{{/*  Load script for only page contain codeblock  */}}
{{ if (findRE "<pre" .Content 1) }}
    {{ if eq (.Scratch.Get "params").syntax_highlight.lib "builtin" }}
        {{ $enable_code_copy := (.Scratch.Get "params").syntax_highlight.builtin.enable_code_copy | default true }}
        {{ if $enable_code_copy }}
            {{ $code_copy_js := resources.Get "js/builtin-copy.js" }}
            {{- if hugo.IsProduction -}}
                {{ $code_copy_js = $code_copy_js | js.Build (dict "minify" true) | resources.Fingerprint "sha512" -}}
            {{- else -}}
                {{ $code_copy_js = $code_copy_js | js.Build -}}
            {{- end -}}
            <script defer src="{{ $code_copy_js.RelPermalink }}" integrity="{{- $code_copy_js.Data.Integrity -}}"></script>
        {{ end }}
    {{ else if eq (.Scratch.Get "params").syntax_highlight.lib "prism.js" }}
        {{ $prism_js := slice (resources.Get "lib/js/prism-v1.29.0.min.js") }}

        {{ $enable_line_no := (.Scratch.Get "params").syntax_highlight.prism.enable_line_no | default true }}
        {{ if $enable_line_no }}
            {{ $prism_js = $prism_js | append (resources.Get "lib/js/prism-line-numbers-v1.29.0.min.js") }}
        {{ end }}

        {{ $enable_code_copy := (.Scratch.Get "params").syntax_highlight.prism.enable_code_copy | default true }}
        {{ if $enable_code_copy }}
            {{ $prism_js = $prism_js | append (resources.Get "lib/js/prism-copy-to-clipboard-v1.29.0.min.js") }}
        {{ end }}

        {{ $prism_js = $prism_js | append (resources.Get "js/prism-autoloader.js") | append (resources.Get "js/prism.js") | resources.Concat "js/prism.js" }}
        {{- if hugo.IsProduction -}}
            {{ $prism_js = $prism_js | js.Build (dict "minify" true) | resources.Fingerprint "sha512" -}}
        {{- else -}}
            {{ $prism_js = $prism_js | js.Build -}}
        {{- end -}}
        <script defer src="{{ $prism_js.RelPermalink }}" integrity="{{- $prism_js.Data.Integrity -}}" data-manual></script>
    {{ end }}
{{ end }}

{{ if (.Scratch.Get "params").enable_site_search }}
    {{ $target_path := printf "js/search-%s.js" .Site.LanguageCode }}
    {{ $index_url := "index.json" | absLangURL }}
    {{ $build_opts := dict "targetPath" $target_path "params" (dict "index_url" $index_url) }}
    {{ $search_js := resources.Get "js/search.js" | js.Build $build_opts }}
    {{- if hugo.IsProduction -}}
        {{ $search_js = $search_js | js.Build (merge $build_opts (dict "minify" true)) | resources.Fingerprint "sha512" -}}
    {{- else -}}
        {{ $search_js = $search_js | js.Build $build_opts -}}
    {{- end -}}
    <script defer src="{{ $search_js.RelPermalink }}" integrity="{{- $search_js.Data.Integrity -}}"></script>
{{ end }}

{{/*  Load highlighter for emgithub  */}}
{{ if .HasShortcode "emgithub" }}
    {{ $js := slice (resources.Get "lib/js/prism-v1.29.0.min.js") }}
    {{ $js = $js | append (resources.Get "lib/js/prism-line-numbers-v1.29.0.min.js") }}
    {{ $js = $js | append (resources.Get "lib/js/prism-copy-to-clipboard-v1.29.0.min.js") }}
    {{ $js = $js | append (resources.Get "js/prism-autoloader.js") }}
    {{ $js = $js | resources.Concat "js/emgithub-prism.js" }}
    {{- if hugo.IsProduction -}}
        {{ $js = $js | js.Build (dict "minify" true) | resources.Fingerprint "sha512" -}}
    {{- else -}}
        {{ $js = $js | js.Build }}
    {{- end -}}
    <script defer src="{{ $js.RelPermalink }}" integrity="{{- $js.Data.Integrity -}}" data-manual></script>
{{ end }}
