Browse Source

Improve current menu item detection

master
Paweł Romanowski 6 years ago
parent
commit
a1cbba7796
  1. 26
      templates/macros/menu.html

26
templates/macros/menu.html

@ -5,20 +5,30 @@
<nav class="menu">
<ul class="menu__inner">
{%- for item in menu_items %}
{%- set rel_item_url = item.url | replace(from="$BASE_URL", to="") -%}
{%- set current = rel_item_url == current_path
or rel_item_url == "/" ~ current_path
or rel_item_url ~ "/" == "/" ~ current_path
or rel_item_url == "" and current_path == "/"
{%- set abs_item_url = item.url | replace(from="$BASE_URL", to=config.base_url) -%}
{%- set is_current = current_url == abs_item_url ~ "/"
or current_url is starting_with(abs_item_url)
-%}
{%- 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 -%}
{% endif -%}
{% 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 %}
{%- set blog_post = not current_item and item.url == "$BASE_URL" -%}
<li {%- if current_item and item == current_item or blog_post %} class="active" {%- endif %}>
<li {%- if current_item and current_item == item %} class="active" {%- endif %}>
{%- 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>
{%- else -%}

Loading…
Cancel
Save