Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Render breadcrumbs in JSON-LD with Breadcrumbs On Rails
<!-- app/views/layouts/_json_ld_breadcrumbs.html.erb -->
<script type="application/ld+json">
<%= render_breadcrumbs builder: Breadcrumbs::JsonLdBuilder %>
</script>
# 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
@necojackarc
Copy link
Author

necojackarc commented Nov 6, 2017

I have made a gem based on this Gist.
Please check necojackarc/breadcrumbs_on_rails-json_ld.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment