Skip to content
Snippets Groups Projects

Add schema markup to breadcrumb

Merged Francisco Javier López requested to merge 36844-fj-add-schema-to-breadcrumb-json into master
All threads resolved!
Files
9
@@ -32,4 +32,46 @@ def add_to_breadcrumb_dropdown(link, location: :before)
@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' => link
}
end
end
Loading