diff options
Diffstat (limited to 'app/helpers/breadcrumbs_helper.rb')
-rw-r--r-- | app/helpers/breadcrumbs_helper.rb | 49 |
1 files changed, 49 insertions, 0 deletions
diff --git a/app/helpers/breadcrumbs_helper.rb b/app/helpers/breadcrumbs_helper.rb index b067376cea0..ade7c48b03f 100644 --- a/app/helpers/breadcrumbs_helper.rb +++ b/app/helpers/breadcrumbs_helper.rb @@ -32,4 +32,53 @@ module BreadcrumbsHelper @breadcrumb_dropdown_links[location] ||= [] @breadcrumb_dropdown_links[location] << link end + + def push_to_schema_breadcrumb(text, link) + list_item = schema_list_item(text, link, schema_breadcrumb_list.size + 1) + + schema_breadcrumb_list.push(list_item) + end + + def schema_breadcrumb_json + { + '@context': 'https://schema.org', + '@type': 'BreadcrumbList', + 'itemListElement': build_item_list_elements + }.to_json + end + + private + + def schema_breadcrumb_list + @schema_breadcrumb_list ||= [] + end + + def build_item_list_elements + return @schema_breadcrumb_list unless @breadcrumbs_extra_links&.any? + + last_element = schema_breadcrumb_list.pop + + @breadcrumbs_extra_links.each do |el| + push_to_schema_breadcrumb(el[:text], el[:link]) + end + + last_element['position'] = schema_breadcrumb_list.last['position'] + 1 + schema_breadcrumb_list.push(last_element) + end + + def schema_list_item(text, link, position) + { + '@type' => 'ListItem', + 'position' => position, + 'name' => text, + 'item' => ensure_absolute_link(link) + } + end + + def ensure_absolute_link(link) + url = URI.parse(link) + url.absolute? ? link : URI.join(request.base_url, link).to_s + rescue URI::InvalidURIError + "#{request.base_url}#{request.path}" + end end |