Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding Schema.org tags for better discovery #688

Merged
merged 12 commits into from
Oct 23, 2024
43 changes: 43 additions & 0 deletions app/helpers/schema_org_helper.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
# frozen_string_literal: true

# rubocop:disable Rails/OutputSafety
module SchemaOrgHelper
def keywords_helper(subjects)
keywords_json = subjects.map do |subject|
'"' + html_escape(subject) + '"'
end

html = "[" + keywords_json.join(",") + "]"
html.html_safe
end

def authors_helper(authors)
authors_json = authors.each.map do |author|
json_str = "\n\t\t\t{\n\t\t\t" + '"name": ' + '"' + author.value + '"'
if author.affiliation_name.present?
json_str += ",\n\t\t\t" + '"affiliation": ' + '"' + author.affiliation_name + '"'
end
if author.orcid.present?
json_str += ",\n\t\t\t" + '"identifier": ' + '"' + author.orcid + '"'
end
json_str += "\n\t\t\t}"
json_str
end
html = "[" + authors_json.join(",") + "]"
html.html_safe
end

def license_helper(licenses)
if licenses.count == 0
""
else
html = '"license": {'
html += "\n\t\t\t" + '"@type": ' + '"Dataset"' + ",\n" \
"\t\t\t" + '"text": ' + '"' + licenses[0]['identifier'] + '"' + ",\n" \
"\t\t\t" + '"url": ' + '"' + licenses[0]['uri'] + '"'
html += "\n\t\t\t},"
html.html_safe
end
end
end
# rubocop:enable Rails/OutputSafety
1 change: 1 addition & 0 deletions app/views/layouts/blacklight/base.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
More info: https://getbootstrap.com/docs/4.6/components/popovers/
-->
<script type="text/javascript" src="https://cdn.jsdelivr.net/npm/popper.js@1.16.1/dist/umd/popper.min.js"></script>
<%= render partial: 'shared/schema_org'%>
</head>
<body class="<%= render_body_class %>">
<nav id="skip-link" role="navigation" aria-label="<%= t('blacklight.skip_links.label') %>">
Expand Down
28 changes: 28 additions & 0 deletions app/views/shared/_schema_org.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<% if !@document.nil?%>
<!-- Renders Schema.org tags -->
<script type ="application/ld+json">
{ "@context": "http://schema.org",
"@type": "Dataset",
"identifier": "<%= @document.doi_url %>",
"name": "<%= @document.title %>",
"author": <%= authors_helper(@document.authors_ordered) %>,
"version": "1",
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hard-coded to 1 for, but will get a real value once this has been implemented: #694

"description": "<%= @document.description %>",
"keywords": <%= keywords_helper(@document.subject) %>,
"citation":
[
"<a href="<%= @document.doi_url%>" target="_blank"> "<%= @document.citation.to_s('apa')%>" </a>"
],
"schemaVersion": "https://schema.org/version/28.0",
<%= license_helper(@document.rights_enhanced) %>
"provider":
{
"@type": "Organization",
"name": "Princeton University"
}
}
</script>
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There is also an additional ticket to add dates once we index them (see #695)

<% end %>



67 changes: 67 additions & 0 deletions spec/helpers/schema_org_helper_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
# frozen_string_literal: true

require 'rails_helper'
RSpec.describe SchemaOrgHelper, type: :helper do
let(:author_with_orcid) do
{
"value" => "some_name",
"identifier" => {
"scheme" => "ORCID",
"value" => "0000-0000-1111-1111"
},
"affiliations" => [
{ "value" => "some_affiliation" }
]
}
end

let(:author_without_orcid) do
{
"value" => "some_name"
}
end

let(:license) do
{
"identifier" => "some_identifier",
"uri" => "some_uri"
}
end

describe "#render_sidebar_related_identifiers" do
it "renders keywords" do
expect(helper.keywords_helper(['a', 'b'])).to eq '["a","b"]'
end

it "does not render keywords" do
expect(helper.keywords_helper([])).to eq '[]'
end

it "renders authors with orcid and affiliation" do
author = [Author.new(author_with_orcid)]
# Expected output with orcid and affiliation
expected_output = "[\n\t\t\t{\n\t\t\t\"name\": \"some_name\",\n\t\t\t\"affiliation\": \"some_affiliation\",\n\t\t\t\"identifier\": \"0000-0000-1111-1111\"\n\t\t\t}]"

# Call the helper function with the authors array and verify the result
expect(helper.authors_helper(author)).to eq expected_output
end

it "renders authors without orcid nor affiliation" do
author = [Author.new(author_without_orcid)]
# Expected output without orcid and affiliation
expected_output = "[\n\t\t\t{\n\t\t\t\"name\": \"some_name\"\n\t\t\t}]"

# Call the helper function with the authors array and verify the result
expect(helper.authors_helper(author)).to eq expected_output
end

it "render one license" do
expected_output = "\"license\": {\n\t\t\t\"@type\": \"Dataset\",\n\t\t\t\"text\": \"some_identifier\",\n\t\t\t\"url\": \"some_uri\"\n\t\t\t},"
expect(helper.license_helper([license])).to eq expected_output
end

it "renders no license" do
expect(helper.license_helper([])).to eq ""
end
end
end
5 changes: 5 additions & 0 deletions spec/system/search_pdc_results_page_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -41,4 +41,9 @@
click_on "View larger"
expect(page).to have_content "2022\n2022"
end

it 'does not render Schema.org tags' do
visit '/?search_field=all_fields&q='
expect(page.html.include?('"@context": "http://schema.org",')).to be false
end
end
5 changes: 5 additions & 0 deletions spec/system/show_pdc_describe_record_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,11 @@
expect(first_filename_spot).to eq("readme.txt")
end

it 'renders Schema.org tags' do
visit '/catalog/doi-10-34770-bm4s-t361'
expect(page.html.include?('"@context": "http://schema.org",')).to be true
end

context "when crawler visits the site" do
before do
allow_any_instance_of(CatalogController).to receive(:agent_is_crawler?).and_return(true)
Expand Down