Skip to content

Commit

Permalink
Add LinkCollection#structured_data (#26)
Browse files Browse the repository at this point in the history
Fixes #25
  • Loading branch information
dkniffin committed Nov 3, 2020
1 parent cebc7b1 commit b416901
Show file tree
Hide file tree
Showing 3 changed files with 75 additions and 1 deletion.
17 changes: 17 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -219,6 +219,23 @@ You can use the `breadcrumbs` method directly as an array. It will return an arr
<% end %>
```

If you use this approach, you lose the built-in semantic breadcrumb functionality. One way to
add them back is to use JSON-LD structured data:

```erb
<script type="application/ld+json">
<%= breadcrumbs.structured_data(url_base: "https://example.com")) %>
</script>
```

Or, you can infer `url_base` from `request`:

```erb
<script type="application/ld+json">
<%= breadcrumbs.structured_data(url_base: "#{request.protocol}#{request.host_with_port}")) %>
</script>
```

## Getting the parent breadcrumb

If you want to add a link to the parent breadcrumb, you can use the `parent_breadcrumb` view helper.
Expand Down
23 changes: 22 additions & 1 deletion lib/gretel/renderer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,27 @@ def initialize(context, links, options = {})
concat links
end

# Returns a hash matching the JSON-LD Structured Data schema
# https://developers.google.com/search/docs/data-types/breadcrumb#json-ld
def structured_data(url_base:)
url_base = url_base.chomp("/") # Remove trailing `/`, if present

items = @links.each_with_index.map do |link, i|
{
"@type": "ListItem",
"position": i + 1,
"name": link.text,
"item": "#{url_base}#{link.url}"
}
end

{
"@context": "https://schema.org",
"@type": "BreadcrumbList",
"itemListElement": items
}
end

# Helper for returning all link keys to allow for simple testing.
def keys
map(&:key)
Expand Down Expand Up @@ -192,7 +213,7 @@ def render
end

html = html_fragments.join(" ").html_safe

if options[:semantic]
content_tag(options[:container_tag], html, id: options[:id], class: options[:class], itemscope: "", itemtype: "https://schema.org/BreadcrumbList")
else
Expand Down
36 changes: 36 additions & 0 deletions spec/lib/gretel/view_helpers_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -489,6 +489,42 @@
end
end

describe 'Structured data' do
# https://developers.google.com/search/docs/data-types/breadcrumb#year-genre%20example
it "returns an object with the correct structure" do
breadcrumb :with_parent

expected_result = {
"@context": "https://schema.org",
"@type": "BreadcrumbList",
"itemListElement": [
{
"@type": "ListItem",
"position": 1,
"name": "Home",
"item": "https://example.com/"
},
{
"@type": "ListItem",
"position": 2,
"name": "About",
"item": "https://example.com/about"
},
{
"@type": "ListItem",
"position": 3,
"name": "Contact",
"item": "https://example.com/about/contact"
}
]
}

expect(breadcrumbs.structured_data(url_base: "https://example.com")).to eq(expected_result)
# Ensure there's no extra trailing slashes
expect(breadcrumbs.structured_data(url_base: "https://example.com/")).to eq(expected_result)
end
end

private

def setup_loading_from_tmp_folder
Expand Down

0 comments on commit b416901

Please sign in to comment.