From fd9fdebc0b0fbff0eaf0321c7840e373ecfccf9a Mon Sep 17 00:00:00 2001 From: Julianna Tetreault <32834804+juliannatetreault@users.noreply.github.com> Date: Wed, 15 Apr 2020 13:03:05 -0600 Subject: [PATCH] [deploy] Migrate from Schema.org to JSON-LD (#6956) * Add JSON-LD in place of schema.org in _video_player.html.erb * Remove unnecessary tags around JSON-LD to clean up templates * Fix indenatation and remove extra elements from show.html.erb and _profile_header.html.erb * Remove empty line from articles/show.html.erb * Clean up JSON-LD commas across erb templates * Revert articleBody changes, adjust nesting, and remove extra quote * Add application/ld+json to allowed_types in .erb-lint.yml This addition makes it so that the no verify flag is no longer required * Add additional data for Articles, Users, and Orgs to better SEO * Add additional data to JSON-LD structures *Add sameAs data to articles/show.html.erb *Add sameAs data to users/show.html.erb *Make adjustments to VideoObject data in _video_player *Make adjustments to Org in _profile_header *Add removed erb back to all erb templates * Refactor code to fix syntax Google syntax errors * Add missing double quotes to values * Remove unnecessary meta tags from _video_player.html.erb * Add additional key-value pairs to data structures * General code clean up * Clean up existing data structures *Remove useless meta tag and replace with a tag *Small refactor to users/show.html.erb data * Extract user json-ld out of users/show.html.erb *Move json-ld logic to stories_controller *Use user_json_ld variable in place of entire data structure *Set user_json_ld as a before_action in the controller * Add specs around JSON-LD data in templates *Add request spec for users/show.html.erb *Add request spec for articles/show.html.erb *Add request spec for _video_player.html.erb *Add request spec for _profile_header.html.erb *WIP: Fix pending specs for all templates * Remove useless meta tags from users/show and _profile_header.html.erb * Add additional test coverage to users/show.html.erb *Add additional tests to user_show_spec *Ensure that all tests pass *Small refactor to users/show.html.erb JSON data structure * Add additional test coverage around JSON-LD data *Add more robust tests to template specs *Remove unnecessary comments from specs *Uncomment pending specs * Adjust modified date to be more readable in article specs * Refactor spec to be more readable and update GitHub username test * Remove meta erb from articles/show.html.erb * Remove meta data from _video_player.html.erb * Clean up useless codeand whitespace for final review * Extract users/show data structure from template into StoriesController * Remove JSON-LD object from users/show.html.erb * Place JSON-LD for users/show.html.erb in StoriesController * Store user_json_ld in set_json_ld method in StoriesController * Call user_json_ld variable in script tag in users/show * Clean up trailing commas and refactor articles/show.html.erb * Remove redundant user/profile_header_spec.rb * Remove unnecessary code from _profile_header and articles/show * Remove Organization from articles/show.html.erb * Remove unnecessary comments from _profile_header.html.erb * Extract user_json_ld into separate methods to fix Code Climate failures * Rename user_profile_details and user_same_as methods to be more explicit * Refactored user_json_ld methods to use symbols instead of strings * Use symbols to fetch keys from a hash instead of strings * Changed user_url in data structure * Removal of erb tags and general clean up * Revert schema changes * Remove unnecessary quotation marks --- .erb-lint.yml | 1 + app/controllers/stories_controller.rb | 55 +++++++++++++++++ app/views/articles/_video_player.html.erb | 28 +++++---- app/views/articles/show.html.erb | 59 +++++++++++++------ app/views/users/_profile_header.html.erb | 27 +++++++-- app/views/users/show.html.erb | 15 +++-- spec/requests/articles/articles_show_spec.rb | 40 +++++++++++++ .../articles/video_player_show_spec.rb | 37 ++++++++++++ spec/requests/user/user_show_spec.rb | 52 ++++++++++++++++ 9 files changed, 274 insertions(+), 40 deletions(-) create mode 100644 spec/requests/articles/articles_show_spec.rb create mode 100644 spec/requests/articles/video_player_show_spec.rb create mode 100644 spec/requests/user/user_show_spec.rb diff --git a/.erb-lint.yml b/.erb-lint.yml index c078a6547..1ce33920c 100644 --- a/.erb-lint.yml +++ b/.erb-lint.yml @@ -12,6 +12,7 @@ linters: allowed_types: - 'text/javascript' - 'text/x-tmpl' + - 'application/ld+json' allow_blank: true disallow_inline_scripts: false Rubocop: diff --git a/app/controllers/stories_controller.rb b/app/controllers/stories_controller.rb index bc54bf566..e4112c56d 100644 --- a/app/controllers/stories_controller.rb +++ b/app/controllers/stories_controller.rb @@ -205,6 +205,7 @@ class StoriesController < ApplicationController return if performed? set_surrogate_key_header "articles-user-#{@user.id}" + set_json_ld render template: "users/show" end @@ -330,4 +331,58 @@ class StoriesController < ApplicationController def assign_classified_listings @classified_listings = ClassifiedListing.where(published: true).select(:title, :category, :slug, :bumped_at) end + + def set_json_ld + @user_json_ld = { + "@context": "http://schema.org", + "@type": "Person", + "mainEntityOfPage": { + "@type": "WebPage", + "@id": URL.user(@user) + }, + "url": URL.user(@user), + "sameAs": [], + "image": ProfileImage.new(@user).get(width: 320), + "name": @user.name, + "email": "", + "jobTitle": "", + "description": @user.summary.presence || ["404 bio not found"].sample, + "disambiguatingDescription": [], + "worksFor": [ + { + "@type": "Organization" + }, + ], + "alumniOf": "" + } + set_user_profile_json_ld + set_user_same_as_json_ld + end + + def set_user_profile_json_ld + @user_json_ld[:disambiguatingDescription].append(@user.mostly_work_with) if @user.mostly_work_with.present? + @user_json_ld[:disambiguatingDescription].append(@user.currently_hacking_on) if @user.currently_hacking_on.present? + @user_json_ld[:disambiguatingDescription].append(@user.currently_learning) if @user.currently_learning.present? + @user_json_ld[:worksFor][0][:employer_name] = @user.employer_name if @user.employer_name.present? + @user_json_ld[:worksFor][0][:employer_url] = @user.employer_url if @user.employer_url.present? + @user_json_ld[:alumniOf] = @user.education if @user.education.present? + @user_json_ld[:email] = @user.email if @user.email_public + @user_json_ld[:jobTitle] = @user.employment_title if @user.employment_title.present? + @user_json_ld[:sameAs].append(@user.twitter_username) if @user.twitter_username.present? + @user_json_ld[:sameAs].append(@user.github_username) if @user.github_username.present? + end + + def set_user_same_as_json_ld + @user_json_ld[:sameAs].append(@user.mastodon_url) if @user.mastodon_url.present? + @user_json_ld[:sameAs].append(@user.facebook_url) if @user.facebook_url.present? + @user_json_ld[:sameAs].append(@user.linkedin_url) if @user.linkedin_url.present? + @user_json_ld[:sameAs].append(@user.behance_url) if @user.behance_url.present? + @user_json_ld[:sameAs].append(@user.stackoverflow_url) if @user.stackoverflow_url.present? + @user_json_ld[:sameAs].append(@user.dribbble_url) if @user.dribbble_url.present? + @user_json_ld[:sameAs].append(@user.medium_url) if @user.medium_url.present? + @user_json_ld[:sameAs].append(@user.gitlab_url) if @user.gitlab_url.present? + @user_json_ld[:sameAs].append(@user.instagram_url) if @user.instagram_url.present? + @user_json_ld[:sameAs].append(@user.twitch_username) if @user.twitch_username.present? + @user_json_ld[:sameAs].append(@user.website_url) if @user.website_url.present? + end end diff --git a/app/views/articles/_video_player.html.erb b/app/views/articles/_video_player.html.erb index e38640587..0021c91d8 100644 --- a/app/views/articles/_video_player.html.erb +++ b/app/views/articles/_video_player.html.erb @@ -1,16 +1,22 @@ -
- <% if meta_tags %> - - - - - - <% minutes, seconds = article.video_duration_in_minutes.split(":") %> - sM%sS", minutes: minutes, seconds: seconds) %>" /> -
- <% end %> +
+ <% minutes, seconds = article.video_duration_in_minutes.split(":") %> +
+ +
<% end %> + + <% if @article.processed_html.include? "runkit-element" %> <%= javascript_include_tag "https://embed.runkit.com" %> <% end %> @@ -85,17 +119,7 @@ <% if @article.video_state == "PROGRESSING" %>

⏳ Video Transcoding In Progress ⏳

<% end %> -
<% end %> -

+

<%= @article.title %>

-