lib/nanoc/helpers/breadcrumbs.rb in nanoc-4.4.2 vs lib/nanoc/helpers/breadcrumbs.rb in nanoc-4.4.3

- old
+ new

@@ -1,31 +1,25 @@ module Nanoc::Helpers # @see http://nanoc.ws/doc/reference/helpers/#breadcrumbs module Breadcrumbs - class CannotGetBreadcrumbsForNonLegacyItem < Nanoc::Int::Errors::Generic - def initialize(identifier) - super("You cannot build a breadcrumbs trail for an item that has a “full” identifier (#{identifier}). Doing so is only possible for items that have a legacy identifier.") - end - end - # @return [Array] def breadcrumbs_trail - unless @item.identifier.legacy? - raise CannotGetBreadcrumbsForNonLegacyItem.new(@item.identifier) - end + # e.g. ['', '/foo', '/foo/bar'] + components = item.identifier.components + prefixes = components.inject(['']) { |acc, elem| acc + [acc.last + '/' + elem] } - trail = [] - idx_start = 0 - - loop do - idx = @item.identifier.to_s.index('/', idx_start) - break if idx.nil? - - idx_start = idx + 1 - identifier = @item.identifier.to_s[0..idx] - trail << @items[identifier] + if @item.identifier.legacy? + prefixes.map { |pr| @items[Nanoc::Identifier.new('/' + pr, type: :legacy)] } + else + prefixes + .reject { |pr| pr =~ /^\/index\./ } + .map do |pr| + if pr == '' + @items['/index.*'] + else + @items[Nanoc::Identifier.new(pr).without_ext + '.*'] + end + end end - - trail end end end