Module: Jekyll::Filters::Menu

Defined in:
lib/jekyll/filters/menu.rb

Instance Method Summary collapse

Instance Method Details

Verifies if the given URL is an active item on the menu.

Example usage:

assign active_url = page.url | menu_active % for item in site.i18n.menu.items %

{{ item.title }}
</a>

endfor %

Parameters:

  • The (String)

    URL to be verified

Returns:

  • (String)

    The item URL



22
23
24
25
26
# File 'lib/jekyll/filters/menu.rb', line 22

def menu_active_item(page_url)
  site_menu&.find do |key, _value|
    key == page_url || page_url.start_with?(key)
  end&.last
end

The menu is defined in a data file that corresponds to the site language. This method converts the menu items into a map of URL parts and actual URLs.



35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/jekyll/filters/menu.rb', line 35

def site_menu
  site = @context.registers[:site]
  @site_menu ||= site.data.dig(site.config['locale'], 'menu', 'items')&.reduce({}) do |menu, item|
    # If the item has a layout, we pick the first post and update
    # the href.  We can't do the same for all posts because we
    # wouldn't be able to cache the menu.
    if item['layout']
      doc = site.documents.find do |doc|
        doc.data['layout'] == item['layout']
      end

      item['href'] = doc&.url
    end

    # Ignore empty or anchor items
    next menu if item['href'].nil? || item['href']&.empty? || item['href']&.start_with?('#')

    menu[item['href']] = item['href']

    item['active']&.each do |a|
      menu[a] = item['href']
    end

    menu
  end
end