module Jekyll::Filters::Menu
Public Instance Methods
menu_active_item(page_url)
click to toggle source
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 %}
<a href="{{ item.href }}" class="{{ page.url | menu_active | equals: item.href | ternary: 'active', '' }}"> {{ item.title }} </a>
{% endfor %}
@param [String] The URL to be verified @return [String] The item URL
# File lib/jekyll/filters/menu.rb 22 def menu_active_item(page_url) 23 site_menu&.find do |key, value| 24 key == page_url || page_url.start_with?(key) 25 end&.last 26 end
site_menu()
click to toggle source
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.
@see _data/es.yml @see {0xacab.org/sutty/jekyll/jekyll-locales} @return [Hash]
# File lib/jekyll/filters/menu.rb 35 def site_menu 36 site = @context.registers[:site] 37 @site_menu ||= site.data.dig(site.config['locale'], 'menu', 'items')&.reduce({}) do |menu, item| 38 # If the item has a layout, we pick the first post and update 39 # the href. We can't do the same for all posts because we 40 # wouldn't be able to cache the menu. 41 if item['layout'] 42 doc = site.documents.find do |doc| 43 doc.data['layout'] == item['layout'] 44 end 45 46 item['href'] = doc&.url 47 end 48 49 # Ignore empty or anchor items 50 next menu if item['href'].nil? || item['href'].empty? || item['href']&.start_with?('#') 51 52 menu[item['href']] = item['href'] 53 54 item['active']&.each do |a| 55 menu[a] = item['href'] 56 end 57 58 menu 59 end 60 end