Add opengraph and twitter card support to topics

This commit is contained in:
Robin Ward 2013-03-07 17:31:06 -05:00
parent 0bbcd6f7e4
commit d848a9fedc
5 changed files with 45 additions and 4 deletions

View File

@ -32,4 +32,25 @@ module ApplicationHelper
current_user.try(:admin?)
end
def crawlable_meta_data(url, title, description)
# Image to supply as meta data
image = "#{Discourse.base_url}#{SiteSetting.logo_url}"
# Add opengraph tags
result = tag(:meta, property: 'og:site_name', content: SiteSetting.title) << "\n"
result << tag(:meta, property: 'og:image', content: image) << "\n"
result << tag(:meta, property: 'og:url', content: url) << "\n"
result << tag(:meta, property: 'og:title', content: title) << "\n"
result << tag(:meta, property: 'og:description', content: description) << "\n"
# Add twitter card
result << tag(:meta, property: 'twitter:card', content: "summary") << "\n"
result << tag(:meta, property: 'twitter:url', content: url) << "\n"
result << tag(:meta, property: 'twitter:title', content: title) << "\n"
result << tag(:meta, property: 'twitter:description', content: description) << "\n"
result << tag(:meta, property: 'twitter:image', content: image) << "\n"
result
end
end

View File

@ -12,13 +12,10 @@
<link rel="icon" type="image/png" href=<%=SiteSetting.favicon_url%>>
<%= javascript_include_tag "preload_store" %>
<%= render :partial => "common/special_font_face" %>
<%= render :partial => "common/discourse_stylesheet" %>
<%=csrf_meta_tags%>
<%= csrf_meta_tags %>
<%= yield :head %>
</head>

View File

@ -24,4 +24,5 @@
<% content_for :head do %>
<%= auto_discovery_link_tag(@topic_view, {action: :feed, format: :rss}, title: t('rss_posts_in_topic', topic: @topic_view.title), type: 'application/rss+xml') %>
<%= crawlable_meta_data(@topic_view.absolute_url, @topic_view.title, @topic_view.summary) %>
<% end %>

View File

@ -2,6 +2,7 @@ require_dependency 'guardian'
require_dependency 'topic_query'
class TopicView
include ActionView::Helpers
attr_accessor :topic, :min, :max, :draft, :draft_key, :draft_sequence, :posts
@ -61,6 +62,10 @@ class TopicView
"#{@topic.relative_url}?page=#{next_page}"
end
def absolute_url
"#{Discourse.base_url}#{@topic.relative_url}"
end
def relative_url
@topic.relative_url
end
@ -69,6 +74,15 @@ class TopicView
@topic.title
end
def summary
return nil if posts.blank?
first_post_content = sanitize(posts.first.cooked, tags: [], attributes: [])
first_post_content.gsub!(/\n/, ' ')
return first_post_content if first_post_content.length <= 500
"#{first_post_content[0..500]}..."
end
def filter_posts(opts = {})
if opts[:post_number].present?
# Get posts near a post

View File

@ -26,6 +26,14 @@ describe TopicView do
lambda { TopicView.new(topic.id, nil) }.should raise_error(Discourse::NotLoggedIn)
end
it "provides an absolute url" do
topic_view.absolute_url.should be_present
end
it "provides a summary of the first post" do
topic_view.summary.should be_present
end
describe "#get_canonical_path" do
let(:user) { Fabricate(:user) }
let(:topic) { Fabricate(:topic) }