<!DOCTYPE html>
<html>

<head>
  <meta http-equiv="Content-Type" content="text/html;charset=utf-8" />
  <meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no">
  <link rel="stylesheet" href="{{ '/assets/css/style.css?v=' | append: site.github.build_revision | relative_url }}" media="screen" type="text/css">
  <title>Tree-sitter｜{{ page.title }}</title>
  <link rel="icon" type="image/png" href="{{ '/assets/images/favicon-32x32.png' | relative_url }}" sizes="32x32" />
  <link rel="icon" type="image/png" href="{{ '/assets/images/favicon-16x16.png' | relative_url }}" sizes="16x16" />
</head>

<body class="sidebar-hidden">
  <script>
    if (localStorage.getItem('sidebar-active') === 'true') {
      document.body.classList.remove('sidebar-hidden');
    }
  </script>

  <div id="container">
    <div id="sidebar">
      <nav id="table-of-contents">
        <a class="logo table-of-contents-section" href="https://github.com/tree-sitter/tree-sitter">
          <img title="Tree-sitter logo, link to github repo" src="{{ '/assets/images/tree-sitter-small.png' | relative_url }}" width=200 height=200 />
          <span class="github-repo">GitHub repository</span>
        </a>

        {% for other_page in site.html_pages %}
          {% if page.title == other_page.title %}
            <li class="table-of-contents-section active">
              <a class="nav-link" href="{{ other_page.url | relative_url }}">
                {{ other_page.title }}
              </a>

              <div id="current-page-table-of-contents">
                {% capture whitespace %}
                {% assign min_header = 2 %}
                {% assign max_header = 3 %}
                {% assign nodes = content | split: "<h" %}
                {% assign first_header = true %}
                {% for node in nodes %}
                  {% if node == "" %}
                    {% continue %}
                  {% endif %}

                  {% assign header_level = node | replace: '"', '' | slice: 0, 1 | times: 1 %}

                  {% if header_level < min_header or header_level > max_header %}
                    {% continue %}
                  {% endif %}

                  {% if first_header %}
                    {% assign first_header = false %}
                    {% assign min_header = header_level %}
                  {% endif %}

                  {% assign indent_level = header_level | minus: min_header | add: 1 %}
                  {% assign header_content = node | split: '</h' %}
                  {% assign header_content = header_content[0] %}

                  {% assign html_id = header_content | split: 'id="' %}
                  {% assign html_id = html_id[1] | split: '"' %}
                  {% assign html_id = html_id[0] %}

                  {% capture header_attrs_to_strip %}{{ header_content | split: '>' | first }}>{% endcapture %}
                  {% assign header = header_content | replace: header_attrs_to_strip, '' %}

                  {% assign space = '' %}
                  {% for i in (1..indent_level) %}
                    {% assign space = space | prepend: '    ' %}
                  {% endfor %}

{% capture my_toc %}{{ my_toc }}
{{ space }}- [{{ header }}](#{{ html_id }}){: .nav-link}{% endcapture %}

                {% endfor %}
                {% endcapture %}
                {{ my_toc | strip | markdownify | strip }}
              </ul>
            </li>
          {% else %}
            <li class="table-of-contents-section">
              <a class="nav-link" href="{{ other_page.url | relative_url }}">
                {{ other_page.title }}
              </a>
            </li>
          {% endif %}
        {% endfor %}
      </nav>
    </div>

    <a id="sidebar-toggle-link" href="#">☰</a>

    <main id="main-content">
      {{ content }}
    </main>
  </div>
</body>
</html>

<script
  src="https://code.jquery.com/jquery-3.3.1.min.js"
  crossorigin="anonymous">
</script>

<script
  src="https://maxcdn.bootstrapcdn.com/bootstrap/4.1.0/js/bootstrap.bundle.min.js">
</script>

<script>
  $('#sidebar-toggle-link').click(function(e) {
    e.preventDefault();
    $(document.body).toggleClass('sidebar-hidden');
    localStorage.setItem(
      'sidebar-active',
      localStorage.getItem('sidebar-active') === 'true' ? 'false' : 'true'
    );
  });

  if (document.body.scrollHeight > window.innerHeight + 500) {
    $(document.body).scrollspy({
      target: '#current-page-table-of-contents',
      offset: 40
    });
  }

  $(document).scroll(function() {
    if ($(document).scrollLeft() > 0) {
      localStorage.setItem('sidebar-active', 'false');
      $(document.body).addClass('sidebar-hidden');
    }
  });

  $('h2, h3').filter('[id]').each(function() {
    $(this).html('<a href="#'+$(this).attr('id')+'">' + $(this).text() + '</a>');
  });
</script>
