Module: Jekyll::Filters::Menu

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

Instance Method Summary collapse

Instance Method Details

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