Correctly implement OpenGraph tags

Just defining og:site_name indicates that we support the
OpenGraph protocol, but then we didn't set the required
properties.
This commit is contained in:
Jonne Haß 2014-12-08 01:32:05 +01:00
parent 60a5d2a5ba
commit 6836af89ad
6 changed files with 60 additions and 27 deletions

View file

@ -1,43 +1,71 @@
module OpenGraphHelper
def og_title(post)
meta_tag_with_property('og:title', post_page_title(post, :length => 140))
def og_title(title)
meta_tag_with_property('og:title', title)
end
def og_type(post)
meta_tag_with_property('og:type', 'article')
end
def og_url(post)
meta_tag_with_property('og:url', post_url(post))
def og_url(url)
meta_tag_with_property('og:url', url)
end
def og_image(post)
tags = post.photos.map{|x| meta_tag_with_property('og:image', x.url(:thumb_large))}
def og_image(post=nil)
tags = []
tags = post.photos.map{|x| meta_tag_with_property('og:image', x.url(:thumb_large))} if post
tags << meta_tag_with_property('og:image', default_image_url) if tags.empty?
tags.join(' ')
end
def og_description(post)
meta_tag_with_property('og:description', post_page_title(post, :length => 1000))
def og_description(description)
meta_tag_with_property('og:description', description)
end
def og_type
meta_tag_with_property('og:type', og_namespace('frame'))
def og_type(type='website')
meta_tag_with_property('og:type', type)
end
def og_namespace(object)
namespace = AppConfig.services.facebook.open_graph_namespace.present? ? AppConfig.services.facebook.open_graph_namespace : 'joindiaspora'
"#{namespace}:frame"
def og_namespace
AppConfig.services.facebook.open_graph_namespace
end
def og_page_specific_tags(post)
[og_title(post), og_type,
og_url(post), og_image(post),
og_description(post)].join(' ').html_safe
def og_site_name
meta_tag_with_property('og:site_name', AppConfig.settings.pod_name)
end
def og_common_tags
[og_site_name]
end
def og_general_tags
[
*og_common_tags,
og_type,
og_title('diaspora* social network'),
og_image,
og_url(AppConfig.environment.url),
og_description('diaspora* is the online social world where you are in control.')
].join("\n").html_safe
end
def og_page_post_tags(post)
[
*og_common_tags,
og_type("#{og_namespace}:frame"),
og_title(post_page_title(post, :length => 140)),
og_url(post_url(post)),
og_image(post),
og_description(post.message.plain_text_without_markdown truncate: 1000)
].join("\n").html_safe
end
def og_prefix
"og: http://ogp.me/ns# #{og_namespace}: https://diasporafoundation.org/ns/joindiaspora#"
end
def meta_tag_with_property(name, content)
content_tag(:meta, '', :property => name, :content => content)
tag(:meta, :property => name, :content => content)
end
def og_html(cache)

View file

@ -0,0 +1,5 @@
- if @post.present?
%link{:rel => 'alternate', :type => "application/json+oembed", :href => "#{oembed_url(:url => post_url(@post))}"}
= og_page_post_tags(@post)
- else
= og_general_tags

View file

@ -4,7 +4,7 @@
!!!
%html{:lang => I18n.locale.to_s, :dir => (rtl?) ? 'rtl' : 'ltr'}
%head
%head{:prefix => og_prefix}
%title
= page_title yield(:page_title)
@ -12,11 +12,12 @@
%meta{"http-equiv"=>"Content-Type", :content=>"text/html; charset=utf-8"}/
%meta{:name => "description", :content => "diaspora*"}/
%meta{:name => "author", :content => "Diaspora, Inc."}/
%meta{:property => "og:site_name", :content => "#{AppConfig.settings.pod_name}"}/
%link{:rel => 'shortcut icon', :href => "#{image_path('favicon.png')}" }
%link{:rel => 'apple-touch-icon', :href => "#{image_path('apple-touch-icon.png')}"}
= render 'layouts/open_graph'
= chartbeat_head_block
= include_mixpanel
@ -46,9 +47,6 @@
= yield(:head)
= csrf_meta_tag
- if @post.present?
%link{:rel => 'alternate', :type => "application/json+oembed", :href => "#{oembed_url(:url => post_url(@post))}"}
= og_page_specific_tags(@post)
= include_gon(:camel_case => true)

View file

@ -4,14 +4,13 @@
!!!
%html{:lang => I18n.locale.to_s, :dir => (rtl?) ? 'rtl' : 'ltr'}
%head
%head{:prefix => og_prefix}
%title
= pod_name
%meta{:name => "description", :content => "diaspora* mobile"}/
%meta{:name => "author", :content => "Diaspora, Inc."}/
%meta{:charset => 'utf-8'}/
%meta{:property => "og:site_name", :content => "#{AppConfig.settings.pod_name}"}/
/ Viewport scale
%meta{:name =>'viewport', :content => "width=device-width, minimum-scale=1 maximum-scale=1"}/
@ -30,6 +29,8 @@
/%meta{:name => "apple-mobile-web-app-capable", :content => "yes"}
/%link{:rel => "apple-touch-startup-image", :href => "/images/apple-splash.png"}
= render 'layouts/open_graph'
= chartbeat_head_block
/ Stylesheets

View file

@ -139,6 +139,7 @@ defaults:
enable: false
app_id:
secret:
open_graph_namespace: 'joindiaspora'
twitter:
enable: false
key:

View file

@ -60,7 +60,7 @@ module Rack
<meta http-equiv="X-UA-Compatible" content="chrome=1">
HEAD
body.gsub!('<head>', "<head>\n" + head )
body.gsub!(/<head(.*)>/, "<head\\1>\n" + head )
body
end