Last active
November 6, 2017 01:58
-
-
Save necojackarc/860f41e0e21be8bcde046e9b139dc0e0 to your computer and use it in GitHub Desktop.
Render breadcrumbs in JSON-LD with Breadcrumbs On Rails
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# app/lib/breadcrumbs/json_ld_builder.rb | |
class Breadcrumbs::JsonLdBuilder < BreadcrumbsOnRails::Breadcrumbs::Builder | |
def render | |
JSON.pretty_generate( | |
"@context" => "http://schema.org", | |
"@type" => "BreadcrumbList", | |
"itemListElement" => | |
@elements.map.with_index(1) { |elt, idx| render_element(elt, idx) }, | |
) | |
end | |
private | |
def render_element(element, index) | |
{ | |
"@type" => "ListItem", | |
"position" => index, | |
"item" => { | |
"@id" => "#{@context.root_url.chop}#{compute_path(element)}", | |
"name" => compute_name(element), | |
}, | |
} | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I have made a gem based on this Gist.
Please check necojackarc/breadcrumbs_on_rails-json_ld.