From 0551b3f5ee2984ecee2a776a89f13d7f21793faa Mon Sep 17 00:00:00 2001 From: Neil Lalonde Date: Wed, 22 Feb 2017 16:24:05 -0500 Subject: [PATCH] FEATURE: replace emoji with unicode in title and description meta tags --- app/helpers/application_helper.rb | 11 +++++++++-- spec/helpers/application_helper_spec.rb | 6 ++++++ 2 files changed, 15 insertions(+), 2 deletions(-) diff --git a/app/helpers/application_helper.rb b/app/helpers/application_helper.rb index 6e32e866bac..a9e4ed40e8e 100644 --- a/app/helpers/application_helper.rb +++ b/app/helpers/application_helper.rb @@ -188,8 +188,9 @@ module ApplicationHelper [:url, :title, :description].each do |property| if opts[property].present? escape = (property != :image) - result << tag(:meta, { property: "og:#{property}", content: opts[property] }, nil, escape) - result << tag(:meta, { name: "twitter:#{property}", content: opts[property] }, nil, escape) + content = (property == :url ? opts[property] : gsub_emoji_to_unicode(opts[property])) + result << tag(:meta, { property: "og:#{property}", content: content }, nil, escape) + result << tag(:meta, { name: "twitter:#{property}", content: content }, nil, escape) end end @@ -217,6 +218,12 @@ module ApplicationHelper content_tag(:script, MultiJson.dump(json).html_safe, type: 'application/ld+json'.freeze) end + def gsub_emoji_to_unicode(str) + if str + str.gsub(/:([\w\-+]*):/) { |name| Emoji.lookup_unicode($1) || name } + end + end + def application_logo_url @application_logo_url ||= (mobile_view? && SiteSetting.mobile_logo_url) || SiteSetting.logo_url end diff --git a/spec/helpers/application_helper_spec.rb b/spec/helpers/application_helper_spec.rb index 7adf0c2baf4..77b43be5942 100644 --- a/spec/helpers/application_helper_spec.rb +++ b/spec/helpers/application_helper_spec.rb @@ -106,4 +106,10 @@ describe ApplicationHelper do end end + describe 'gsub_emoji_to_unicode' do + it "converts all emoji to unicode" do + expect(helper.gsub_emoji_to_unicode('Boat Talk: my :sailboat: boat: why is it so slow? :snail:')).to eq("Boat Talk: my ⛵ boat: why is it so slow? 🐌") + end + end + end