diff --git a/app/helpers/application_helper.rb b/app/helpers/application_helper.rb index c8e46c5ab86..9e90ef0dede 100644 --- a/app/helpers/application_helper.rb +++ b/app/helpers/application_helper.rb @@ -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 diff --git a/app/views/layouts/application.html.erb b/app/views/layouts/application.html.erb index 4214d196d1d..739fa52d0ae 100644 --- a/app/views/layouts/application.html.erb +++ b/app/views/layouts/application.html.erb @@ -12,13 +12,10 @@ > <%= javascript_include_tag "preload_store" %> - - <%= render :partial => "common/special_font_face" %> <%= render :partial => "common/discourse_stylesheet" %> - - <%=csrf_meta_tags%> + <%= csrf_meta_tags %> <%= yield :head %> diff --git a/app/views/topics/show.html.erb b/app/views/topics/show.html.erb index 5f513c312a9..c6cea4fa945 100644 --- a/app/views/topics/show.html.erb +++ b/app/views/topics/show.html.erb @@ -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 %> diff --git a/lib/topic_view.rb b/lib/topic_view.rb index e8018c26ff6..f25fe43e6bb 100644 --- a/lib/topic_view.rb +++ b/lib/topic_view.rb @@ -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 diff --git a/spec/components/topic_view_spec.rb b/spec/components/topic_view_spec.rb index a2a203ffbf9..40ae99720fc 100644 --- a/spec/components/topic_view_spec.rb +++ b/spec/components/topic_view_spec.rb @@ -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) }