Skip to content

Commit

Permalink
recursive search for levels, allowing children and subs
Browse files Browse the repository at this point in the history
  • Loading branch information
anopsy committed Feb 17, 2025
1 parent c2bdb10 commit 6917823
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 97 deletions.
82 changes: 38 additions & 44 deletions _includes/components/children_nav.html
Original file line number Diff line number Diff line change
@@ -1,71 +1,42 @@
{%- comment -%}
Include as: {%- include components/children_nav.html -%}
Depends on: page, site, nav_breadcrumbs.
Results in: HTML for the children-navigation component.
Includes: components/nav/sorted.html, toc_heading_custom.html.
Overwrites:
nav_ancestor_links, nav_top_node_titles, nav_child_candidates, nav_children,
nav_child, nav_child_ok, nav_child_ancestor, nav_sorted.
{%- endcomment -%}

{%- comment -%}
Whether a page has any children is checked efficiently by inspecting the cached
site_nav. If the page has no children, nav_children is set to an empty array;
otherwise nav_children is left unset. (The site_nav is rendered the first time
it is included, and that may overwrite various variables.)
Recursive children navigation. Works with both `subcategories` and `children`.
{%- endcomment -%}

{%- if page.has_children == false -%}
{%- assign nav_children = "" | split: "" -%}
{%- else -%}

{%- capture site_nav -%}
{%- include_cached components/site_nav.html all=true -%}
{%- include_cached components/site_nav.html all=true -%}
{%- endcapture -%}

{%- assign nav_children = nil -%}

{%- capture nav_list_link -%}
<a href="{{ page.url | relative_url }}" class="nav-list-link">
<a href="{{ page.url | relative_url }}" class="nav-list-link"></a>
{%- endcapture -%}

{%- capture nav_list_simple -%}
<ul class="nav-list">
{%- endcapture -%}
{%- assign nav_child_start = site_nav | split: nav_list_link | last | split: "</a>" | slice: 1 | first -%}

{%- assign nav_child_start = site_nav
| split: nav_list_link | last
| split: "</a>" | slice: 1 | first -%}
{%- assign nav_child_test = nav_child_start | remove_first: "<ul class=\"nav-list\">" | prepend: "<ul class=\"nav-list\">" -%}

{%- assign nav_child_test = nav_child_start
| remove_first: nav_list_simple | prepend: nav_list_simple -%}

{%- if nav_child_start != nav_child_test -%}
{%- assign nav_children = "" | split: "" -%}
{%- endif -%}

{%- endif -%}

{%- unless nav_children -%}

{%- comment -%}
The layout is assumed to include components/breadcrumbs.html before this file,
otherwise it needs to be included here.
{%- endcomment -%}

{%- assign nav_ancestors = "" | split: "" -%}
{%- for nav_link in nav_breadcrumbs -%}
{%- assign nav_title = nav_link | split: ">" | slice: 1 | first | append: ">" | remove: "</a>" -%}
{%- assign nav_ancestors = nav_ancestors | push: nav_title -%}
{%- endfor -%}

{%- assign nav_parenthood = site[page.collection] | default: site.html_pages
| where_exp: "item", "item.title != nil" | group_by: "parent" -%}
{%- assign nav_parenthood = site[page.collection] | default: site.html_pages | where_exp: "item", "item.title != nil" | group_by: "parent" -%}

{%- assign nav_top_nodes = nav_parenthood
| where_exp: "item", "item.name == ''" | map: "items" | first -%}
{%- assign nav_top_nodes = nav_parenthood | where_exp: "item", "item.name == ''" | map: "items" | first -%}

{% assign nav_top_node_titles = nav_top_nodes | map: "title" -%}
{%- assign nav_top_node_titles = nav_top_nodes | map: "title" -%}

{%- include components/nav/children.html node=page ancestors=nav_ancestors all=true -%}

Expand All @@ -79,12 +50,35 @@

<hr>
{% include toc_heading_custom.html %}
<ul>
{% for nav_child in nav_children %}
<li>
<a href="{{ nav_child.url | relative_url }}">{{ nav_child.title }}</a>{% if nav_child.summary %} - {{ nav_child.summary }}{% endif %}
</li>
{% endfor %}

<ul class="nav-list">
{% macro render_nav(navlist) %}
<ul class="nav-list">
{% for nav in navlist %}
{% assign has_children = nav.subcategories | default: nav.children %}
{% assign active_class = "" %}
{% if page.path == nav.page %}
{% assign active_class = "active" %}
{% endif %}

<li class="nav-list-item {{ active_class }}">
{% if has_children %}
<a href="#" class="nav-list-expander">
<svg viewBox="0 0 24 24"><use xlink:href="#svg-arrow-right"></use></svg>
</a>
{% endif %}

<a class="nav-list-link" href="{% link {{ nav.page }} %}">{{ nav.title }}</a>

{% if has_children %}
{% render_nav has_children %}
{% endif %}
</li>
{% endfor %}
</ul>
{% endmacro %}

{% render_nav nav_children %}
</ul>

{%- endif -%}
{%- endif -%}
76 changes: 23 additions & 53 deletions _includes/nav.html
Original file line number Diff line number Diff line change
@@ -1,57 +1,27 @@
{% macro render_nav(navlist) %}
<ul class="nav-list">
{% if page.sidebar == "core" %}
{% assign navlist = site.data.navcore %}
{% elsif page.sidebar == "sql" %}
{% assign navlist = site.data.navsql %}
{% else %}
{% assign navlist = site.data.nav %}
{% endif %}


{% for nav in navlist %}
{% if nav.subcategories != null %}
{% assign current_parent = nav.subcategories | where:"page", page.path | size %}
{% if current_parent != 0 %}
{% assign classlist = "active" %}
{% else %}
{% assign classlist = "" %}
{% endif %}
<li class="nav-list-item {{classlist}}">
<a href="#"
class="nav-list-expander"><svg viewBox="0 0 24 24">
<use xlink:href="#svg-arrow-right"></use>
</svg></a>
{% if nav.page %}
<a class="nav-list-link"
href="{% link {{ nav.page }} %}">{{ nav.title }}</a>
{% else %}
<a class="nav-list-link"
href="#">{{ nav.title }}</a>
{% endif %}
<ul class="nav-list ">
{% for subcategory in nav.subcategories %}
{% if page.path == subcategory.page %}
{% assign classlist = "active" %}
{% else %}
{% assign classlist = "" %}
{% endif %}
<li class="nav-list-item">
<a class="nav-list-link {{ classlist }}"
href="{% link {{ subcategory.page }} %}">{{ subcategory.subtitle }}</a>
</li>
{% endfor %}
</ul>
</li>
{% else %}
{% for nav in navlist %}
{% assign has_children = nav.subcategories | default: nav.children %}
{% assign active_class = "" %}
{% if page.path == nav.page %}
{% assign classlist = "active" %}
{% else %}
{% assign classlist = "" %}
{% assign active_class = "active" %}
{% endif %}
<li class="nav-list-item">
<a class="nav-list-link {{classlist}}"
href="{% link {{ nav.page }} %}">{{ nav.title }}</a>

<li class="nav-list-item {{ active_class }}">
{% if has_children %}
<a href="#" class="nav-list-expander">
<svg viewBox="0 0 24 24"><use xlink:href="#svg-arrow-right"></use></svg>
</a>
{% endif %}

<a class="nav-list-link" href="{% link {{ nav.page }} %}">{{ nav.title }}</a>

{% if has_children %}
{% render_nav has_children %}
{% endif %}
</li>
{% endif %}
{% endfor %}
</ul>
{% endfor %}
</ul>
{% endmacro %}

{% render_nav navlist %}

0 comments on commit 6917823

Please sign in to comment.