Module: Jekyll::Filters::Menu
- Defined in:
- lib/jekyll/filters/menu.rb
Instance Method Summary collapse
-
#menu_active_item(page_url) ⇒ String
Verifies if the given URL is an active item on the menu.
-
#site_menu ⇒ Hash
The menu is defined in a data file that corresponds to the site language.
Instance Method Details
#menu_active_item(page_url) ⇒ String
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 %
22 23 24 25 26 |
# File 'lib/jekyll/filters/menu.rb', line 22 def (page_url) &.find do |key, _value| key == page_url || page_url.start_with?(key) end&.last end |
#site_menu ⇒ Hash
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 = @context.registers[:site] @site_menu ||= site.data.dig(site.config['locale'], 'menu', 'items')&.reduce({}) do |, 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 if item['href'].nil? || item['href']&.empty? || item['href']&.start_with?('#') [item['href']] = item['href'] item['active']&.each do |a| [a] = item['href'] end end end |