Skip to content

Instantly share code, notes, and snippets.

@necojackarc
Last active November 6, 2017 01:58
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save necojackarc/860f41e0e21be8bcde046e9b139dc0e0 to your computer and use it in GitHub Desktop.
Save necojackarc/860f41e0e21be8bcde046e9b139dc0e0 to your computer and use it in GitHub Desktop.
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

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