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

Adds twitter card for collections #375

Merged
merged 2 commits into from
Dec 22, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions app/controllers/tags_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ def show
.order(created_at: :desc)
.paginate(page: params[:citations_page], per_page: 10)

@username = User.find_by_id(@tag.user_id).username
end

def new
Expand Down
12 changes: 12 additions & 0 deletions app/helpers/posts_helper.rb
Original file line number Diff line number Diff line change
@@ -1,10 +1,22 @@
module PostsHelper
def meta_description(post)
if post.abstract
"#{meta_tag_sanitize_and_truncate(post.abstract)}"
end
end

def meta_authors(post)
if post.authors
"#{post.authors.truncate(120)}"
end
end

def meta_title(post)
if post.title.present?
"#{meta_tag_sanitize_and_truncate(post.title)}"
end
end

def sanitize_title(title)
Sanitize.fragment(title, :elements => ['b', 'sup', 'sub', 'em', 'code'])
end
Expand Down
9 changes: 9 additions & 0 deletions app/helpers/tags_helper.rb
Original file line number Diff line number Diff line change
@@ -1,2 +1,11 @@
module TagsHelper
def tag_description(username)
"A collection created by @#{@username}"
end

def tag_text(tag)
if tag.text
"#{meta_tag_sanitize_and_truncate(@tag.text)}"
end
end
end
6 changes: 4 additions & 2 deletions app/views/layouts/_head.html.slim
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
ruby:
meta_description = content_for?(:meta_description) ? yield(:meta_description) : "Do The Science"
meta_twitter_creator = content_for?(:meta_twitter_creator) ? yield(:meta_twitter_creator) : "@jellypbc"
meta_title = content_for?(:title) ? yield(:title) : "Untitled"
meta_twitter_description = content_for?(:meta_twitter_description) ? yield(:meta_twitter_description) : "Anonymous"

if content_for?(:meta_project_image_url)
meta_image_url = yield(:meta_project_image_url)
Expand Down Expand Up @@ -35,8 +37,8 @@ head
meta name="twitter:domain" content="jellypbc.com"
meta name='twitter:site' content='@jellypbc'
meta name='twitter:widgets:csp' content='on'
meta name="twitter:title" content=yield(:title)
meta name="twitter:description" content=meta_tag_sanitize_and_truncate(meta_description)
meta name="twitter:title" content=meta_tag_sanitize_and_truncate(meta_title)
meta name="twitter:description" content=meta_tag_sanitize_and_truncate(meta_twitter_description)
meta name="twitter:image" content=meta_image_url
meta name="twitter:card" content="summary"

Expand Down
3 changes: 2 additions & 1 deletion app/views/posts/show.html.slim
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
- content_for(:title) { "#{meta_tag_sanitize_and_truncate(@post.title)}" } if @post.title
- content_for(:meta_description) { meta_description(@post) }
- content_for(:title) { meta_title(@post) }
- content_for(:meta_twitter_description) { meta_authors(@post) }
- guest_user = { data: { attributes: {name: "Anonymous", avatar_url: User.default_avatar_url } } }

#post.container [email protected]
Expand Down
3 changes: 3 additions & 0 deletions app/views/tags/show.html.slim
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
- content_for(:title) { tag_text(@tag) }
- content_for(:meta_twitter_description) { tag_description(@username) }

.users
.container.my-5
header.row.my-5
Expand Down