{{/* cSpell:ignore docsearch markmap plantuml katex hasmermaid */ -}}

{{ if .Site.Params.markmap.enable -}}
<style>
.markmap > svg {
  width: 100%;
  height: 300px;
}
</style>
<script>
window.markmap = {
  autoLoader: {
      manual: true,
      onReady() {
        const { autoLoader, builtInPlugins } = window.markmap;
        autoLoader.transformPlugins = builtInPlugins.filter(plugin => plugin.name !== 'prism');
      },
  },
};
</script>
<script src="https://cdn.jsdelivr.net/npm/markmap-autoloader"></script>
{{ end -}}

{{ if .Site.Params.plantuml.enable -}}
  <script src='{{ "js/deflate.js" | relURL }}'></script>
{{ end -}}

{{ $jsBs := resources.Get "vendor/bootstrap/dist/js/bootstrap.bundle.js" -}}
{{ $jsBase := resources.Get "js/base.js" -}}
{{ $jsSearch := resources.Get "js/search.js" | resources.ExecuteAsTemplate "js/search.js" .Site.Home -}}
{{ $jsMarkmap := resources.Get "js/markmap.js" | resources.ExecuteAsTemplate "js/markmap.js" . -}}
{{ $jsPlantuml := resources.Get "js/plantuml.js" | resources.ExecuteAsTemplate "js/plantuml.js" . -}}
{{ $jsDrawio := resources.Get "js/drawio.js" | resources.ExecuteAsTemplate "js/drawio.js" . -}}
{{ if .Site.Params.offlineSearch -}}
  {{ $jsSearch = resources.Get "js/offline-search.js" -}}
{{ end -}}

{{ $jsArray := slice $jsBs $jsBase $jsSearch $jsPlantuml $jsMarkmap $jsDrawio -}}

{{ if .Page.Store.Get "hasmermaid" -}}
  {{ partial "scripts/mermaid.html" . -}}
{{ end -}}

{{ if .Page.Store.Get "hasMath" -}}
  {{ partial "scripts/katex.html" . -}}
{{ end -}}

{{- $darkMode := partialCached "dark-mode-config.html" "dark-mode-global" -}}
{{ if $darkMode.enable -}}
  {{ $jsArray = $jsArray | append (resources.Get "js/dark-mode.js") -}}
{{ end -}}

{{ if not (.Param "ui.scrollSpy.disable") -}}
  {{ $jsScrollSpyPatch := resources.Get "js/scrollspy-patch.js" -}}
  {{ $jsArray = $jsArray | append $jsScrollSpyPatch -}}
{{ end -}}

{{ $js := $jsArray | resources.Concat "js/main.js" -}}
{{ if hugo.IsProduction -}}
  {{ $js = $js | minify | fingerprint -}}
  <script src="{{ $js.RelPermalink }}" integrity="{{ $js.Data.Integrity }}" crossorigin="anonymous"></script>
{{ else -}}
  <script src="{{ $js.RelPermalink }}"></script>
{{ end -}}

{{ if .Site.Params.prism_syntax_highlighting -}}
  <script src='{{ "js/prism.js" | relURL }}'></script>
{{ else if ( not .Site.Params.disable_click2copy_chroma ) -}}
  {{ $c2cJS := resources.Get "js/click-to-copy.js" -}}
  {{ if hugo.IsProduction -}}
    {{ $c2cJS = $c2cJS | minify | fingerprint -}}
  {{ end -}}
  <script defer src="{{ $c2cJS.RelPermalink }}" {{ with $c2cJS.Data.Integrity -}}
    integrity="{{ . }}" {{ end -}}
    crossorigin="anonymous"></script>
{{ end -}}

{{ if and .Site.Params.search (isset .Site.Params.search "algolia") -}}
  {{ template "algolia/scripts" .Site.Params.search.algolia -}}
{{ end -}}
<script src='{{ "js/tabpane-persist.js" | relURL }}'></script>
{{ partial "hooks/body-end.html" . -}}

{{ define "algolia/scripts" -}}
<script src="https://cdn.jsdelivr.net/npm/@docsearch/js@3.8.2"
  integrity="sha512-lsD+XVzdBI6ZquXc8gqbw0/bgrfIsMJwY/8xvmvbN+U3gZSeG7BXQoCq4zv/yCmntR2GLHtgB+bD4ESPsKIbIA=="
  crossorigin="anonymous" ></script>
<script type="text/javascript">
const containers = ['#docsearch-0', '#docsearch-1'];
for (let c of containers) {
  docsearch({
    container: c,
    appId: {{ .appId | default "R2IYF7ETH7" }},
    apiKey: {{ .apiKey | default "599cec31baffa4868cae4e79f180729b" }},
    indexName: {{ .indexName | default "docsearch" }},
  });
}
</script>
{{ end -}}
