{% if site.search_enabled %}
  <script type="module" src="{{ '/assets/js/search/ninja-keys.min.js' | relative_url | bust_file_cache }}"></script>
  <ninja-keys hideBreadcrumbs noAutoLoadMdIcons placeholder="Type to start searching"></ninja-keys>
  <script>
    let searchTheme = determineComputedTheme();
    const ninjaKeys = document.querySelector('ninja-keys');

    if (searchTheme === 'dark') {
      ninjaKeys.classList.add('dark');
    } else {
      ninjaKeys.classList.remove('dark');
    }

    const openSearchModal = () => {
      // collapse navbarNav if expanded on mobile
      const $navbarNav = $('#navbarNav');
      if ($navbarNav.hasClass('show')) {
        $navbarNav.collapse('hide');
      }
      ninjaKeys.open();
    };
  </script>
  <script>
    // get the ninja-keys element
    const ninja = document.querySelector('ninja-keys');

    // add the home and posts menu items
    ninja.data = [
      {%- for page in site.pages -%}
        {%- if page.permalink == '/' -%}{%- assign about_title = page.title | strip -%}{%- endif -%}
      {%- endfor -%}
      {
        id: "nav-{{ about_title | slugify }}",
        title: "{{ about_title | truncatewords: 13 }}",
        section: "Navigation",
        handler: () => {
          window.location.href = "{{ '/' | relative_url }}";
        },
      },
      {%- assign sorted_pages = site.pages | sort: "nav_order" -%}
      {%- for p in sorted_pages -%}
        {%- if p.nav and p.autogen == null -%}
          {%- if p.dropdown -%}
            {%- for child in p.children -%}
              {%- unless child.title == 'divider' -%}
                {
                  {%- assign title = child.title | escape | strip -%}
                  {%- if child.permalink contains "/blog/" -%}{%- assign url = "/blog/" -%} {%- else -%}{%- assign url = child.url -%}{%- endif -%}
                  id: "dropdown-{{ title | slugify }}",
                  title: "{{ title | truncatewords: 13 }}",
                  description: "{{ child.description | strip_html | strip_newlines | escape | strip }}",
                  section: "Dropdown",
                  handler: () => {
                    window.location.href = "{{ url | relative_url }}";
                  },
                },
              {%- endunless -%}
            {%- endfor -%}

          {%- else -%}
            {
              {%- assign title = p.title | escape | strip -%}
              {%- if p.permalink contains "/blog/" -%}{%- assign url = "/blog/" -%} {%- else -%}{%- assign url = p.url -%}{%- endif -%}
              id: "nav-{{ title | slugify }}",
              title: "{{ title | truncatewords: 13 }}",
              description: "{{ p.description | strip_html | strip_newlines | escape | strip }}",
              section: "Navigation",
              handler: () => {
                window.location.href = "{{ url | relative_url }}";
              },
            },
          {%- endif -%}
        {%- endif -%}
      {%- endfor -%}
      {%- for post in site.posts -%}
        {
          {%- assign title = post.title | escape | strip -%}
          id: "post-{{ title | slugify }}",
          {% if post.redirect == blank %}
            title: "{{ title | truncatewords: 13 }}",
          {% elsif post.redirect contains '://' %}
            title: '{{ title | truncatewords: 13 }} <svg width="1.2rem" height="1.2rem" top=".5rem" viewBox="0 0 40 40" xmlns="http://www.w3.org/2000/svg"><path d="M17 13.5v6H5v-12h6m3-3h6v6m0-6-9 9" class="icon_svg-stroke" stroke="#999" stroke-width="1.5" fill="none" fill-rule="evenodd" stroke-linecap="round" stroke-linejoin="round"></path></svg>',
          {% else %}
            title: "{{ title | truncatewords: 13 }}",
          {% endif %}
          description: "{{ post.description | strip_html | strip_newlines | escape | strip }}",
          section: "Posts",
          handler: () => {
            {% if post.redirect == blank %}
              window.location.href = "{{ post.url | relative_url }}";
            {% elsif post.redirect contains '://' %}
              window.open("{{ post.redirect }}", "_blank");
            {% else %}
              window.location.href = "{{ post.redirect | relative_url }}";
            {% endif %}
          },
        },
      {%- endfor -%}
      {%- for collection in site.collections -%}
        {%- if collection.label != 'posts' -%}
          {%- for item in collection.docs -%}
            {
              {%- if item.inline -%}
                {%- assign title = item.content | newline_to_br | replace: "<br />", " " | replace: "<br/>", " " | strip_html | strip_newlines | escape | strip -%}
              {%- else -%}
                {%- assign title = item.title | newline_to_br | replace: "<br />", " " | replace: "<br/>", " " | strip_html | strip_newlines | escape | strip -%}
              {%- endif -%}
              id: "{{ collection.label }}-{{ title | slugify }}",
              title: '{{ title | emojify | truncatewords: 13 }}',
              description: "{{ item.description | strip_html | strip_newlines | escape | strip }}",
              section: "{{ collection.label | capitalize }}",
              {%- unless item.inline -%}
                handler: () => {
                  window.location.href = "{{ item.url | relative_url }}";
                },
              {%- endunless -%}
            },
          {%- endfor -%}
        {%- endif -%}
      {%- endfor -%}
      {%- if site.socials_in_search -%}
        {%- if site.email -%}
          {
            id: 'socials-email',
            title: 'Send email',
            section: 'Socials',
            handler: () => {
              window.open("mailto:{{ site.email | encode_email }}", "_blank");
            },
          },
        {%- endif -%}
        {%- if site.telegram_username -%}
          {
            id: 'socials-telegram',
            title: 'Telegram',
            section: 'Socials',
            handler: () => {
              window.open("https://telegram.me/{{ site.telegram_username }}", "_blank");
            },
          },
        {%- endif -%}
        {%- if site.whatsapp_number -%}
          {
            id: 'socials-whatsapp',
            title: 'WhatsApp',
            section: 'Socials',
            handler: () => {
              window.open("https://wa.me/{{ site.whatsapp_number }}", "_blank");
            },
          },
        {%- endif -%}
        {%- if site.orcid_id -%}
          {
            id: 'socials-orcid',
            title: 'ORCID',
            section: 'Socials',
            handler: () => {
              window.open("https://orcid.org/{{ site.orcid_id }}", "_blank");
            },
          },
        {%- endif -%}
        {%- if site.scholar_userid -%}
          {
            id: 'socials-google-scholar',
            title: 'Google Scholar',
            section: 'Socials',
            handler: () => {
              window.open("https://scholar.google.com/citations?user={{ site.scholar_userid }}", "_blank");
            },
          },
        {%- endif -%}
        {%- if site.semanticscholar_id -%}
          {
            id: 'socials-semantic-scholar',
            title: 'Semantic Scholar',
            section: 'Socials',
            handler: () => {
              window.open("https://www.semanticscholar.org/author/{{ site.semanticscholar_id }}", "_blank");
            },
          },
        {%- endif -%}
        {%- if site.publons_id -%}
          {
            id: 'socials-publons',
            title: 'Publons',
            section: 'Socials',
            handler: () => {
              window.open("https://publons.com/a/{{ site.publons_id }}/", "_blank");
            },
          },
        {%- endif -%}
        {%- if site.lattes_id -%}
          {
            id: 'socials-lattes',
            title: 'Lattes',
            section: 'Socials',
            handler: () => {
              window.open("http://lattes.cnpq.br/{{ site.lattes_id }}", "_blank");
            },
          },
        {%- endif -%}
        {%- if site.osf_id -%}
          {
            id: 'socials-open-science-framework',
            title: 'Open Science Framework',
            section: 'Socials',
            handler: () => {
              window.open("https://osf.io/{{ site.osf_id }}/", "_blank");
            },
          },
        {%- endif -%}
        {%- if site.research_gate_profile -%}
          {
            id: 'socials-researchgate',
            title: 'ResearchGate',
            section: 'Socials',
            handler: () => {
              window.open("https://www.researchgate.net/profile/{{ site.research_gate_profile }}/", "_blank");
            },
          },
        {%- endif -%}
        {%- if site.ieee_id -%}
          {
            id: 'socials-ieee-xplore',
            title: 'IEEE Xplore',
            section: 'Socials',
            handler: () => {
              window.open("https://ieeexplore.ieee.org/author/{{ site.ieee_id }}/", "_blank");
            },
          },
        {%- endif -%}
        {%- if site.acm_id -%}
          {
            id: 'socials-acm-dl',
            title: 'ACM DL',
            section: 'Socials',
            handler: () => {
              window.open("https://dl.acm.org/profile/{{ site.acm_id }}/", "_blank");
            },
          },
        {%- endif -%}
        {%- if site.scopus_id -%}
          {
            id: 'socials-scopus',
            title: 'Scopus',
            section: 'Socials',
            handler: () => {
              window.open("https://www.scopus.com/authid/detail.uri?authorId={{ site.scopus_id }}", "_blank");
            },
          },
        {%- endif -%}
        {%- if site.github_username -%}
          {
            id: 'socials-github',
            title: 'GitHub',
            section: 'Socials',
            handler: () => {
              window.open("https://github.com/{{ site.github_username }}", "_blank");
            },
          },
        {%- endif -%}
        {%- if site.linkedin_username -%}
          {
            id: 'socials-linkedin',
            title: 'LinkedIn',
            section: 'Socials',
            handler: () => {
              window.open("https://www.linkedin.com/in/{{ site.linkedin_username }}", "_blank");
            },
          },
        {%- endif -%}
        {%- if site.x_username -%}
          {
            id: 'socials-x',
            title: 'X',
            description: 'Twitter',
            section: 'Socials',
            handler: () => {
              window.open("https://twitter.com/{{ site.x_username }}", "_blank");
            },
          },
        {%- endif -%}
        {%- if site.mastodon_username -%}
          {
            id: 'socials-mastodon',
            title: 'Mastodon',
            section: 'Socials',
            handler: () => {
              window.open("https://{{ site.mastodon_username  }}", "_blank");
            },
          },
        {%- endif -%}
        {%- if site.medium_username -%}
          {
            id: 'socials-medium',
            title: 'Medium',
            section: 'Socials',
            handler: () => {
              window.open("https://medium.com/@{{ site.medium_username }}", "_blank");
            },
          },
        {%- endif -%}
        {%- if site.quora_username -%}
          {
            id: 'socials-quora',
            title: 'Quora',
            section: 'Socials',
            handler: () => {
              window.open("https://www.quora.com/profile/{{ site.quora_username }}", "_blank");
            },
          },
        {%- endif -%}
        {%- if site.flickr_id -%}
          {
            id: 'socials-flickr',
            title: 'Flickr',
            section: 'Socials',
            handler: () => {
              window.open("https://www.flickr.com/{{ site.flickr_id }}", "_blank");
            },
          },
        {%- endif -%}
        {%- if site.blogger_url -%}
          {
            id: 'socials-blogger',
            title: 'Blogger',
            section: 'Socials',
            handler: () => {
              window.open("{{ site.blogger_url }}", "_blank");
            },
          },
        {%- endif -%}
        {%- if site.work_url -%}
          {
            id: 'socials-work',
            title: 'Work',
            section: 'Socials',
            handler: () => {
              window.open("{{ site.work_url }}", "_blank");
            },
          },
        {%- endif -%}
        {%- if site.wikidata_id -%}
          {
            id: 'socials-wikidata',
            title: 'Wikidata',
            section: 'Socials',
            handler: () => {
              window.open("https://www.wikidata.org/wiki/{{ site.wikidata_id }}", "_blank");
            },
          },
        {%- endif -%}
        {%- if site.wikipedia_id -%}
          {
            id: 'socials-wikipedia',
            title: 'Wikipedia',
            section: 'Socials',
            handler: () => {
              window.open("https://wikipedia.org/wiki/User:{{ site.wikipedia_id }}", "_blank");
            },
          },
        {%- endif -%}
        {%- if site.strava_userid -%}
          {
            id: 'socials-strava',
            title: 'Strava',
            section: 'Socials',
            handler: () => {
              window.open("https://www.strava.com/athletes/{{ site.strava_userid }}", "_blank");
            },
          },
        {%- endif -%}
        {%- if site.keybase_username -%}
          {
            id: 'socials-keybase',
            title: 'Keybase',
            section: 'Socials',
            handler: () => {
              window.open("https://keybase.io/{{ site.keybase_username }}", "_blank");
            },
          },
        {%- endif -%}
        {%- if site.gitlab_username -%}
          {
            id: 'socials-gitlab',
            title: 'GitLab',
            section: 'Socials',
            handler: () => {
              window.open("https://gitlab.com/{{ site.gitlab_username }}", "_blank");
            },
          },
        {%- endif -%}
        {%- if site.dblp_url -%}
          {
            id: 'socials-dblp',
            title: 'DBLP',
            section: 'Socials',
            handler: () => {
              window.open("{{ site.dblp_url }}", "_blank");
            },
          },
        {%- endif -%}
        {%- if site.stackoverflow_id -%}
          {
            id: 'socials-stackoverflow',
            title: 'Stackoverflow',
            section: 'Socials',
            handler: () => {
              window.open("https://stackoverflow.com/users/{{ site.stackoverflow_id }}", "_blank");
            },
          },
        {%- endif -%}
        {%- if site.kaggle_id -%}
          {
            id: 'socials-kaggle',
            title: 'Kaggle',
            section: 'Socials',
            handler: () => {
              window.open("https://www.kaggle.com/{{ site.kaggle_id }}", "_blank");
            },
          },
        {%- endif -%}
        {%- if site.lastfm_id -%}
          {
            id: 'socials-last-fm',
            title: 'Last FM',
            section: 'Socials',
            handler: () => {
              window.open("https://www.last.fm/user/{{ site.lastfm_id }}", "_blank");
            },
          },
        {%- endif -%}
        {%- if site.spotify_id -%}
          {
            id: 'socials-spotify',
            title: 'Spotify',
            section: 'Socials',
            handler: () => {
              window.open("https://open.spotify.com/user/{{ site.spotify_id }}", "_blank");
            },
          },
        {%- endif -%}
        {%- if site.pinterest_id -%}
          {
            id: 'socials-pinterest',
            title: 'Pinterest',
            section: 'Socials',
            handler: () => {
              window.open("https://www.pinterest.com/{{ site.pinterest_id }}", "_blank");
            },
          },
        {%- endif -%}
        {%- if site.unsplash_id -%}
          {
            id: 'socials-unsplash',
            title: 'Unsplash',
            section: 'Socials',
            handler: () => {
              window.open("https://unsplash.com/@{{ site.unsplash_id }}", "_blank");
            },
          },
        {%- endif -%}
        {%- if site.instagram_id -%}
          {
            id: 'socials-instagram',
            title: 'Instagram',
            section: 'Socials',
            handler: () => {
              window.open("https://instagram.com/{{ site.instagram_id }}", "_blank");
            },
          },
        {%- endif -%}
        {%- if site.facebook_id -%}
          {
            id: 'socials-facebook',
            title: 'Facebook',
            section: 'Socials',
            handler: () => {
              window.open("https://facebook.com/{{ site.facebook_id }}", "_blank");
            },
          },
        {%- endif -%}
        {%- if site.bluesky_url -%}
          {
            id: 'socials-bluesky',
            title: 'Bluesky',
            section: 'Socials',
            handler: () => {
              window.open("https://bsky.app/profile/{{ site.bluesky_url }}", "_blank");
            },
          },
        {%- endif -%}
        {%- if site.youtube_id -%}
          {
            id: 'socials-youtube',
            title: 'YouTube',
            section: 'Socials',
            handler: () => {
              window.open("https://youtube.com/@{{ site.youtube_id }}", "_blank");
            },
          },
        {%- endif -%}
        {%- if site.discord_id -%}
          {
            id: 'socials-discord',
            title: 'Discord',
            section: 'Socials',
            handler: () => {
              window.open("https://discord.com/users/{{ site.discord_id }}", "_blank");
            },
          },
        {%- endif -%}
        {%- if site.zotero_username -%}
          {
            id: 'socials-zotero',
            title: 'Zotero',
            section: 'Socials',
            handler: () => {
              window.open("https://www.zotero.org/{{ site.zotero_username }}", "_blank");
            },
          },
        {%- endif -%}
        {%- if site.rss_icon -%}
          {
            id: 'socials-rss',
            title: 'RSS Feed',
            section: 'Socials',
            handler: () => {
              window.open("{{ site.baseurl }}/feed.xml", "_blank");
            },
          },
        {%- endif -%}
        {%- comment -%}
        {%- if site.wechat_qr -%}
          // check how to add wechat qr code
          {
            id: 'socials-wechat',
            title: 'WeChat',
            section: 'Socials',
            handler: () => {
              window.open("", "_blank");
            },
          },
        {%- endif -%}
        {%- endcomment -%}
      {%- endif -%}
      {%- if site.enable_darkmode -%}
        {
          id: 'light-theme',
          title: 'Change theme to light',
          description: 'Change the theme of the site to Light',
          section: 'Theme',
          handler: () => {
            setThemeSetting("light");
          },
        },
        {
          id: 'dark-theme',
          title: 'Change theme to dark',
          description: 'Change the theme of the site to Dark',
          section: 'Theme',
          handler: () => {
            setThemeSetting("dark");
          },
        },
        {
          id: 'system-theme',
          title: 'Use system default theme',
          description: 'Change the theme of the site to System Default',
          section: 'Theme',
          handler: () => {
            setThemeSetting("system");
          },
        },
      {%- endif -%}
    ];
  </script>
  <script src="{{ '/assets/js/shortcut-key.js' | relative_url | bust_file_cache }}"></script>
{% endif %}
