mirror of
https://github.com/pawroman/zola-theme-terminimal.git
synced 2025-01-09 20:21:06 +01:00
Improve current menu item detection
This commit is contained in:
parent
9f26ec2814
commit
a1cbba7796
|
@ -5,20 +5,30 @@
|
||||||
<nav class="menu">
|
<nav class="menu">
|
||||||
<ul class="menu__inner">
|
<ul class="menu__inner">
|
||||||
{%- for item in menu_items %}
|
{%- for item in menu_items %}
|
||||||
{%- set rel_item_url = item.url | replace(from="$BASE_URL", to="") -%}
|
{%- set abs_item_url = item.url | replace(from="$BASE_URL", to=config.base_url) -%}
|
||||||
{%- set current = rel_item_url == current_path
|
{%- set is_current = current_url == abs_item_url ~ "/"
|
||||||
or rel_item_url == "/" ~ current_path
|
or current_url is starting_with(abs_item_url)
|
||||||
or rel_item_url ~ "/" == "/" ~ current_path
|
|
||||||
or rel_item_url == "" and current_path == "/"
|
|
||||||
-%}
|
-%}
|
||||||
{%- if current %}
|
{%- set is_base = abs_item_url == config.base_url
|
||||||
|
or abs_item_url == config.base_url ~ "/"
|
||||||
|
-%}
|
||||||
|
|
||||||
|
{%- if is_base %}
|
||||||
|
{%- set_global base_item = item -%}
|
||||||
|
{% endif -%}
|
||||||
|
|
||||||
|
{%- if is_current and not is_base %}
|
||||||
{%- set_global current_item = item -%}
|
{%- set_global current_item = item -%}
|
||||||
{% endif -%}
|
{% endif -%}
|
||||||
{% endfor -%}
|
{% endfor -%}
|
||||||
|
|
||||||
|
{%- if not current_item and base_item %}
|
||||||
|
{# Did not match any menu URLs -- assume it's a blog post #}
|
||||||
|
{%- set current_item = base_item -%}
|
||||||
|
{% endif -%}
|
||||||
|
|
||||||
{%- for item in menu_items %}
|
{%- for item in menu_items %}
|
||||||
{%- set blog_post = not current_item and item.url == "$BASE_URL" -%}
|
<li {%- if current_item and current_item == item %} class="active" {%- endif %}>
|
||||||
<li {%- if current_item and item == current_item or blog_post %} class="active" {%- endif %}>
|
|
||||||
{%- if item.newtab -%}
|
{%- if item.newtab -%}
|
||||||
<a href="{{ item.url | replace(from="$BASE_URL", to=config.base_url) | safe }}" target="_blank" rel="noopener noreferrer">{{ item.name | safe }}</a>
|
<a href="{{ item.url | replace(from="$BASE_URL", to=config.base_url) | safe }}" target="_blank" rel="noopener noreferrer">{{ item.name | safe }}</a>
|
||||||
{%- else -%}
|
{%- else -%}
|
||||||
|
|
Loading…
Reference in a new issue