From 49c9b5bc878502548fa90f0b535ef2013bc7b25a Mon Sep 17 00:00:00 2001 From: Juan Manuel Ramallo Date: Mon, 28 Jan 2019 18:28:59 -0300 Subject: [PATCH] [Emojis in Markdown] (#1653) - Adds the ability to add emojis using colons :joy: :tada: - The emoji conversion is added to the markdown parser labor - When no emoji is found with the alias, same text is returned - No assets added (no emoji images), so this will only be supported when the browser supports emojis (https://blog.getemoji.com/post/57054354336/which-browsers-support-emoji) - Emoji conversion logic is out of markdown parser labor - Adds its own spec --- Gemfile | 1 + Gemfile.lock | 2 ++ app/labor/emoji_converter.rb | 19 +++++++++++++++++++ app/labor/markdown_parser.rb | 1 + spec/labor/emoji_converter_spec.rb | 19 +++++++++++++++++++ 5 files changed, 42 insertions(+) create mode 100644 app/labor/emoji_converter.rb create mode 100644 spec/labor/emoji_converter_spec.rb diff --git a/Gemfile b/Gemfile index 227ae29ba..85517dd7e 100644 --- a/Gemfile +++ b/Gemfile @@ -46,6 +46,7 @@ gem "feedjira", "~> 2.2" gem "figaro", "~> 1.1" gem "fog", "~> 1.41" gem "front_matter_parser", "~> 0.2" +gem "gemoji", "~> 3.0.0" gem "gibbon", "~> 2.2" gem "google-api-client", "~> 0.27" gem "honeycomb-rails" diff --git a/Gemfile.lock b/Gemfile.lock index 87e9860d4..4bc409277 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -450,6 +450,7 @@ GEM fugit (1.1.6) et-orbi (~> 1.1, >= 1.1.6) raabro (~> 1.1) + gemoji (3.0.0) get_process_mem (0.2.3) gibbon (2.2.5) faraday (>= 0.9.1) @@ -964,6 +965,7 @@ DEPENDENCIES fix-db-schema-conflicts! fog (~> 1.41) front_matter_parser (~> 0.2) + gemoji (~> 3.0.0) gibbon (~> 2.2) google-api-client (~> 0.27) guard (~> 2.15) diff --git a/app/labor/emoji_converter.rb b/app/labor/emoji_converter.rb new file mode 100644 index 000000000..46b1882b5 --- /dev/null +++ b/app/labor/emoji_converter.rb @@ -0,0 +1,19 @@ +class EmojiConverter + attr_reader :html + + def self.call(html) + new(html).convert + end + + def initialize(html) + @html = html + end + + def convert + html.gsub!(/:([\w+-]+):/) do |match| + emoji = Emoji.find_by_alias($1) + emoji.present? ? emoji.raw : match + end + html + end +end diff --git a/app/labor/markdown_parser.rb b/app/labor/markdown_parser.rb index cba4ad79e..3ec064de2 100644 --- a/app/labor/markdown_parser.rb +++ b/app/labor/markdown_parser.rb @@ -24,6 +24,7 @@ class MarkdownParser html = wrap_all_images_in_links(html) html = wrap_all_tables(html) html = remove_empty_paragraphs(html) + html = EmojiConverter.call(html) wrap_mentions_with_links!(html) end diff --git a/spec/labor/emoji_converter_spec.rb b/spec/labor/emoji_converter_spec.rb new file mode 100644 index 000000000..a046614c5 --- /dev/null +++ b/spec/labor/emoji_converter_spec.rb @@ -0,0 +1,19 @@ +require "rails_helper" + +RSpec.describe EmojiConverter do + def convert_emoji(html) + EmojiConverter.call(html) + end + + describe "#convert" do + it "converts emoji names wrapped in colons into unicode" do + joy_emoji_unicode = Emoji.find_by_alias("joy").raw + expect(convert_emoji(":joy:")).to include(joy_emoji_unicode) + end + + it "leaves original text between colons when no emoji is found" do + emoji_text = ":no_one_will_ever_create_an_emoji_with_this_alias:" + expect(convert_emoji(emoji_text)).to include(emoji_text) + end + end +end