-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Adding Schema.org tags for better discovery (#688)
- Loading branch information
1 parent
7a6e7dd
commit b7bf538
Showing
6 changed files
with
149 additions
and
0 deletions.
There are no files selected for viewing
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
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 |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -33,6 +33,7 @@ | |
More info: https://getbootstrap.com/docs/4.6/components/popovers/ | ||
--> | ||
<script type="text/javascript" src="https://cdn.jsdelivr.net/npm/[email protected]/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') %>"> | ||
|
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
<% if [email protected]?%> | ||
<!-- 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", | ||
"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> | ||
<% end %> | ||
|
||
|
||
|
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
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 |
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
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