Skip to content

Commit

Permalink
reorganises navigation macro into own section
Browse files Browse the repository at this point in the history
  • Loading branch information
hry-gh committed Aug 5, 2024
1 parent 9957657 commit 7e3fc87
Show file tree
Hide file tree
Showing 2 changed files with 114 additions and 1 deletion.
3 changes: 2 additions & 1 deletion templates/base.html
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
{% import "macros.html" as macros %}
{% import "navigation.html" as navigation %}

{%- if page -%}
{% set pageOrSubsection = page -%}
Expand Down Expand Up @@ -58,7 +59,7 @@
{%- set index = get_section(path="_index.md", metadata_only=true) -%}
{%- for subsectionPath in index.subsections | sort -%}
{%- set subsection = get_section(path=subsectionPath) -%}
{{ macros::render_subsection(currentPage=pageOrSubsection, subsection=subsection, depth=0) -}}
{{ navigation::render_subsection(currentPage=pageOrSubsection, subsection=subsection, depth=0) -}}
{%- endfor -%}
{%- endblock menu -%}
</ul>
Expand Down
112 changes: 112 additions & 0 deletions templates/navigation.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
{% macro render_subsection(currentPage, subsection, depth) %}
{%- if currentPage.ancestors | length %}
{%- set myParent = currentPage.ancestors | last -%}
{%- else %}
{%- set myParent = subsection.ancestors | last %}
{%- endif %}

{%- set myParentSection = get_section(path=myParent, metadata_only=true) -%}

{%- set subsectionParentUrl = subsection.ancestors | last %}
{%- set subsectionParent = get_section(path=subsectionParentUrl, metadata_only=true) %}

{%- set renderAll = false %}
{%- for page in currentPage.ancestors %}
{%- set iteratorSection = get_section(path=page, metadata_only=true) %}
{%- if iteratorSection.path == subsectionParent.path %}
{%- set_global renderAll = true %}
{%- break %}
{%- endif %}
{%- endfor %}

{%- if depth > 1
and current_path != subsection.path
and current_path != myParentSection.path
and current_path != subsectionParent.path
and not renderAll %}
{%- set class = "hidden" %}
{%- else %}
{%- set class = "" %}
{%- endif %}

{%- set linkClass = "" %}
{%- set firstChar = subsection.title | split(pat="") | nth(n = 1) -%}
{%- if firstChar == "/" -%}
{%- set class = class ~ " code-nav" %}
{%- set linkClass = "type" %}
{%- else %}
{%- set class = class ~ " text-title" %}
{%- endif -%}

{%- set iconClass = "" -%}
{%- if currentPage.ancestors is containing(subsection.relative_path) or current_path == subsection.path or 1 > depth -%}
{%- set iconClass = "fa-rotate-90" -%}
{%- endif -%}

{%- set spanClass = " nav-link-inactive" -%}
{%- if current_path == subsection.path %}
{%- set spanClass = " nav-link-active" %}
{%- endif -%}

{%- set depthIndent = 20 * depth -%}
<li class="{{class}}" data-expand-tag="{{subsection.path}}">
<span style="padding-left: {{depthIndent}}px" class="flex flex-row{{spanClass}}">
<span class="px-1"><i class="fa-solid fa-caret-right text-blue-300 {{iconClass}} nav-toggle cursor-pointer"></i></span>
<a href="{{ subsection.permalink | safe }}" class="{{linkClass}}">
{{ subsection.title }}
</a>
</span>
{%- if subsection.subsections -%}
<ul>
{%- for lowerSubPath in subsection.subsections | sort -%}
{% set prod = get_env(name="ZOLA_ENV", default="dev") != "prod" %}
{% set lowerSubsection = get_section(path=lowerSubPath, metadata_only=prod) %}{# TODO: this is the most expensive call to get_section() we have, need to optimize to save 20s #}
{{- self::render_subsection(currentPage = currentPage, subsection=lowerSubsection, depth=depth+1) -}}
{%- endfor -%}
</ul>
{%- endif -%}

{%- if subsection.pages -%}
<ul>
{%- for page in subsection.pages | sort(attribute="title") -%}
{{- self::render_subsection_pages(page=page, currentPage=currentPage, subsection=subsection, depth=depth, currentParent=myParentSection) -}}
{%- endfor -%}
</ul>
{%- endif -%}
</li>
{% endmacro render_subsection %}

{% macro render_subsection_pages(page, currentPage, subsection, depth, currentParent) %}
{%- set renderAll = false %}
{%- for page in currentPage.ancestors %}
{%- set iteratorSection = get_section(path=page, metadata_only=true) %}
{%- if iteratorSection.path == subsection.path %}
{%- set_global renderAll = true %}
{%- break %}
{%- endif %}
{%- endfor %}

{%- if depth > 0
and current_path != subsection.path
and currentParent.path != subsection.path
and not renderAll %}
{%- set class = "hidden" %}
{%- else %}
{%- set class = "" %}
{%- endif -%}

{%- set spanClass = " nav-link-inactive" -%}
{%- if current_path == page.path %}
{%- set spanClass = " nav-link-active" %}
{%- endif -%}

{%- set depthIndent = 20 * (depth + 1) -%}
<li class="text-title {{class}}">
<span style="padding-left: {{depthIndent}}px" class="flex flex-row{{spanClass}}">
<span><i class="fa-solid fa-minus"></i></span>
<a href="{{ page.permalink | safe }}">
{{ page.title }}
</a>
</span>
</li>
{% endmacro %}

0 comments on commit 7e3fc87

Please sign in to comment.