Skip to content

Commit

Permalink
Strip raw HTML from article titles (#1337)
Browse files Browse the repository at this point in the history
* Add remove_wrapper_paragraph_tag arg to MarkdownHelper#render_markdown

* Blank space

* Move some page title related helpers to MetaHelper from ApplicationController

* Start PageTitle PORO

* Add test case: PageTitle.with text:

* Add test case: PageTitle.with path: arg

* Un-implement #page_title helper, just passes along @title

* Strip HTML from PageTitle#content

* Require rails/html/sanitizer in PageTitle

* Use PageTitle#content in #page_title

* Add spec for passing an Array to PageTitle.new

* Add fallback rubric to #page_title

* Make text a positional arg for easier normal case calling

* Update categories titles

* Update ArticleArchive titles

* Update Books titles

* Podcast / Episode titles

* Logos titles

* Pages titles

* Search titles

* Support titles

* Tag titles

* Tool titles

* Video titles

* Sign in title

* Add issue number to Issue#show

* Add PageTitle to #admin_title

* Reset render_markdown

* Delete #title_prefix

* Reset app/views/articles/_related.html.erb

* Add /contact and /about titles
  • Loading branch information
veganstraightedge authored Nov 3, 2019
1 parent 6812560 commit 5a4e4c9
Show file tree
Hide file tree
Showing 39 changed files with 172 additions and 90 deletions.
2 changes: 1 addition & 1 deletion .rubocop.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ Layout/AlignHash:
EnforcedColonStyle: table

Metrics/AbcSize:
Max: 63
Max: 64

Metrics/BlockLength:
Exclude:
Expand Down
4 changes: 2 additions & 2 deletions app/controllers/admin/admin_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,15 @@ class AdminController < ApplicationController
layout 'admin'

def admin_title model = nil, keys = []
return t(".#{action_name}_title", default: '') if model.blank?
return PageTitle.new(['Admin', t(".#{action_name}_title", default: '')]).content if model.blank?

translation_vars = {}

keys.each_with_object(translation_vars) do |key, hash|
hash[key] = model.send(key)
end

t(".#{action_name}_title", translation_vars)
PageTitle.new(['Admin', t(".#{action_name}_title", translation_vars)]).content
rescue NoMethodError
logger.error "#{controller_path}:#{action_name} has an issue with the page title"
''
Expand Down
2 changes: 1 addition & 1 deletion app/controllers/admin/locales_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ def new
end

def edit
@title = admin_title(@locale, %i[id source_path])
@title = admin_title(@locale, %i[id name abbreviation name_in_english])
end

def create
Expand Down
20 changes: 0 additions & 20 deletions app/controllers/application_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -80,16 +80,6 @@ def current_resource_name
end
helper_method :current_resource_name

# TODO: move to meta helper
def page_title
if @title.present?
t(:site_name) + prepend_admin_if_needed + @title
else
t(:site_name)
end
end
helper_method :page_title

def title_for *page_keys
pieces = []

Expand All @@ -106,16 +96,6 @@ def title_for *page_keys
pieces.flatten.join ' : '
end

# TODO: move to meta helper
def prepend_admin_if_needed
if controller_path.match(%r{\Aadmin\/.*\z}).present?
" #{t('admin.title_prepend')} : "
else
' : '
end
end
helper_method :prepend_admin_if_needed

# TODO: move to a helper
def author
t(:site_author)
Expand Down
6 changes: 3 additions & 3 deletions app/controllers/article_archives_controller.rb
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
class ArticleArchivesController < ApplicationController
def index
@html_id = 'page'
@body_id = 'article-archives'
@page_title = 'Articles'
@html_id = 'page'
@body_id = 'article-archives'
@title = PageTitle.new 'Articles'

@article_archive = ArticleArchive.new(year: params[:year],
month: params[:month],
Expand Down
4 changes: 3 additions & 1 deletion app/controllers/articles_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,9 @@ def show
# redirect to proper URL, chomping /feed off of the end
return redirect_to @article.path if request.path.ends_with? '/feed'

@title = @article.name
# Page title
@title = PageTitle.new @article.name

@live_blog = @article.collection_root?

@previous_article = Article.previous(@article).first
Expand Down
2 changes: 1 addition & 1 deletion app/controllers/auth/sessions_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ class SessionsController < Admin::AdminController

# /signin
def new
@page_title = 'Sign In'
@title = PageTitle.new 'Sign In'
@body_id = 'signin'
# TODO: why doesn't this prevent a signed in user going to /signin
return redirect_to(root_url) if signed_in?
Expand Down
10 changes: 5 additions & 5 deletions app/controllers/books_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ def index
@html_id = 'page'
@body_id = 'tools'
@type = 'books'
@title = title_for :books
@title = PageTitle.new title_for(:books)

@bullet_books = BOOK_SLUGS.map { |slug| Book.find_by(slug: slug) }
end
Expand All @@ -32,28 +32,28 @@ def show
@body_id = 'tools'
@type = 'books'
@editable = @book
@title = title_for :books, @book.slug.underscore
@title = PageTitle.new title_for(:books, @book.slug.underscore)
@tool = @book
end

def extras
@html_id = 'page'
@body_id = 'tools'
@title = title_for :books, @book.slug.underscore, :extras
@title = PageTitle.new title_for(:books, @book.slug.underscore, :extras)

render "#{Theme.name}/books/extras"
end

def lit_kit
@html_id = 'page'
@body_id = 'tools'
@title = title_for :books, :lit_kit
@title = PageTitle.new title_for(:books, :lit_kit)
end

def into_libraries
@html_id = 'page'
@body_id = 'tools'
@title = title_for :books, :into_libraries
@title = PageTitle.new title_for(:books, :into_libraries)
end

private
Expand Down
5 changes: 2 additions & 3 deletions app/controllers/categories_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,13 @@ class CategoriesController < ApplicationController
def show
@html_id = 'page'
@body_id = 'category'
@title = title_for :categories, @category.name
end

def index
@html_id = 'page'
@body_id = 'categories'
@categories = Category.all
@title = title_for :categories
@title = PageTitle.new title_for(:categories)
end

def feed
Expand All @@ -37,7 +36,7 @@ def set_category
end

def set_title
@title = @category.name
@title = PageTitle.new title_for(:categories, @category.name)
end

def set_articles
Expand Down
4 changes: 2 additions & 2 deletions app/controllers/episodes_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,14 @@ def show
@html_id = 'page'
@body_id = 'podcast'
@editable = @episode
@title = title_for :podcasts, @episode.name, :transcript
@title = PageTitle.new title_for :podcasts, @episode.name, :transcript
end

def transcript
@html_id = 'page'
@body_id = 'podcast'
@editable = @episode
@title = title_for :podcasts, @episode.name, :transcript
@title = PageTitle.new title_for :podcasts, @episode.name, :transcript

render 'episodes/show'
end
Expand Down
2 changes: 2 additions & 0 deletions app/controllers/issues_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ def show
@issue = Issue.find_by(issue: params[:issue_number], journal_id: journal.id)
@tool = @issue
@editable = @issue

@title = PageTitle.new title_for :journals, journal.name, @issue.issue
render 'books/show'
end
end
4 changes: 2 additions & 2 deletions app/controllers/logos_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ def index
@html_id = 'page'
@body_id = 'tools'
@tools = Logo.live.published.page(params[:page]).per(100)
@title = title_for :logos
@title = PageTitle.new title_for :logos
end

def show
Expand All @@ -14,6 +14,6 @@ def show
@body_id = 'tools'
@type = 'logos'
@editable = @tool
@title = title_for :zines, @tool.name
@title = PageTitle.new title_for :logos, @tool.name
end
end
7 changes: 4 additions & 3 deletions app/controllers/pages_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,23 +5,24 @@ class PagesController < ApplicationController

def show
@editable = @page
@title = PageTitle.new I18n.t("page_titles.about.#{@page.slug}")

# no layout
render html: @page.content.html_safe, layout: false if @page.content_in_html?
end

def library
@title = I18n.t('page_titles.about.library')
@title = PageTitle.new I18n.t('page_titles.about.library')
end

def post_order_success
@title = title_for I18n.t('page_titles.about.store'), I18n.t('page_titles.about.post_order_success')
@title = PageTitle.new title_for I18n.t('page_titles.about.store'), I18n.t('page_titles.about.post_order_success')
@order_id = params[:ordernum]
end

# TODO: make this view localizable
def submission_guidelines
@title = I18n.t('page_titles.about.submission_guidelines')
@title = PageTitle.new I18n.t('page_titles.about.submission_guidelines')
end

private
Expand Down
4 changes: 2 additions & 2 deletions app/controllers/podcast_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,14 @@ def index
@body_id = 'podcast'
@podcasts = Podcast.all.sort_by { |podcast| podcast.latest_episode.published_at }.reverse
@editable = @podcasts.first
@title = title_for :podcasts
@title = PageTitle.new title_for :podcasts
end

def show
@html_id = 'page'
@body_id = 'podcast'
@podcast = Podcast.find_by(slug: params[:slug])
@title = title_for @podcast.name
@title = PageTitle.new title_for @podcast.name
end

def feed; end
Expand Down
4 changes: 2 additions & 2 deletions app/controllers/search_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,13 @@ def index
@results = @search.perform.page(params[:page]).per(15)
end

@title = title_for :search, :results, "“#{@query}”"
@title = PageTitle.new title_for :search, :results, "“#{@query}”"
end

def advanced
@html_id = 'page'
@body_id = 'search'
@title = title_for :search, :advanced
@title = PageTitle.new title_for :search, :advanced

@advanced_search = AdvancedSearch.new(params[:q])
end
Expand Down
6 changes: 3 additions & 3 deletions app/controllers/support_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ class SupportController < ApplicationController
def new
@html_id = 'page'
@body_id = 'support'
@title = t('views.support.new.heading')
@title = PageTitle.new t('views.support.new.heading')
end

def create
Expand All @@ -24,7 +24,7 @@ def create
def thanks
@html_id = 'page'
@body_id = 'support'
@title = t('views.support.thanks.heading')
@title = PageTitle.new t('views.support.thanks.heading')
end

def create_session
Expand Down Expand Up @@ -55,7 +55,7 @@ def create_session
def edit
@html_id = 'page'
@body_id = 'support-edit'
@title = t('views.support.edit.heading')
@title = PageTitle.new t('views.support.edit.heading')

@support_session = SupportSession.find_by token: params[:token]

Expand Down
2 changes: 1 addition & 1 deletion app/controllers/tags_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ def set_tag
end

def set_title
@title = @tag.name
@title = PageTitle.new @tag.name
end

def set_articles
Expand Down
2 changes: 1 addition & 1 deletion app/controllers/tools_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ def set_tool
end

def set_title
@title = title_for [@type.capitalize.to_sym, @tool&.name].compact
@title = PageTitle.new title_for [@type.capitalize.to_sym, @tool&.name].compact
end

def set_editable
Expand Down
4 changes: 2 additions & 2 deletions app/controllers/videos_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ class VideosController < ApplicationController
def index
@html_id = 'page'
@body_id = 'watch'
@title = title_for :videos
@title = PageTitle.new title_for :videos
@videos = Video.live.published.page(params[:page]).per(20)
end

Expand All @@ -13,6 +13,6 @@ def show
@editable = @video
@html_id = 'page'
@body_id = 'video'
@title = title_for :videos, @video.title
@title = PageTitle.new title_for :videos, @video.title
end
end
10 changes: 10 additions & 0 deletions app/helpers/meta_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -23,4 +23,14 @@ def meta_image thing
t('head.meta_image_url')
end
end

def page_title
if @title.nil?
PageTitle.new.content
elsif @title.is_a? PageTitle
@title.content
else
@title
end
end
end
25 changes: 25 additions & 0 deletions app/lib/page_title.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
require 'rails/html/sanitizer'

class PageTitle
attr_reader :text, :path

def initialize text = nil, path: nil
@text = text
@path = path
end

def content
prefix = I18n.t :site_name
suffix = text || path_pieces(path)

title = [prefix, suffix].flatten.compact.join ' : '

Rails::Html::FullSanitizer.new.sanitize title
end

private

def path_pieces path
String(path).split('/').map(&:capitalize)
end
end
2 changes: 1 addition & 1 deletion app/views/article_archives/index.html.erb
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<% content_for(:head) do %>
<%= rel_next_prev_link_tags(@article_archive.articles) %>
<%= rel_next_prev_link_tags @article_archive.articles %>
<% end %>

<header>
Expand Down
1 change: 1 addition & 0 deletions app/views/articles/_related.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@

<header class="article-titles">
<h2 class="p-x-title"><%= link_to article.title, article.path %></h2>

<% if article.subtitle.present? %>
<h3 class="p-x-subtitle"><%= link_to article.subtitle, article.path %></h3>
<% end %>
Expand Down
2 changes: 1 addition & 1 deletion app/views/articles/index.atom.erb
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@

<link rel="alternate" type="text/html" href="<%= strip_subdomain(request.base_url) + article.path %>" />

<title><%= article.name %></title>
<title><%= strip_tags article.name %></title>
<summary><%= article.summary %></summary>

<% article.categories.each do |category| %>
Expand Down
Loading

0 comments on commit 5a4e4c9

Please sign in to comment.