A platform for community discussion. Free, open, simple.
FEATURE: Render emojis on GitHub labels when oneboxing an issue. (#13531)
Roman Rizzifa4e5e8dad2a
diff --git a/app/assets/stylesheets/common/base/onebox.scss b/app/assets/stylesheets/common/base/onebox.scssindex c40dd3e2dd4..fdaf1f6a2b6 100644--- a/app/assets/stylesheets/common/base/onebox.scss+++ b/app/assets/stylesheets/common/base/onebox.scss@@ -505,6 +505,11 @@ pre.onebox code {color: var(--secondary) !important;padding: 2px 4px !important;}++ .emoji {+ max-height: 15px;+ margin: 0.2em;+ }}.onebox.githubactions {diff --git a/app/helpers/emoji_helper.rb b/app/helpers/emoji_helper.rbindex fcf0462f349..3e3e59c7787 100644--- a/app/helpers/emoji_helper.rb+++ b/app/helpers/emoji_helper.rb@@ -2,21 +2,6 @@module EmojiHelperdef emoji_codes_to_img(str)- return if str.blank?-- str = str.gsub(/:([\w\-+]*(?::t\d)?):/) do |name|- code = $1-- if code && Emoji.custom?(code)- emoji = Emoji[code]- "<img src=\"#{emoji.url}\" title=\"#{code}\" class=\"emoji\" alt=\"#{code}\">"- elsif code && Emoji.exists?(code)- "<img src=\"#{Emoji.url_for(code)}\" title=\"#{code}\" class=\"emoji\" alt=\"#{code}\">"- else- name- end- end-- raw(str)+ raw(Emoji.codes_to_img(str))endenddiff --git a/app/models/emoji.rb b/app/models/emoji.rbindex 4a9012c3071..f755a7eaadf 100644--- a/app/models/emoji.rb+++ b/app/models/emoji.rb@@ -231,4 +231,20 @@ class Emoji@unicode_replacements_json ||= unicode_replacements.to_jsonend+ def self.codes_to_img(str)+ return if str.blank?++ str = str.gsub(/:([\w\-+]*(?::t\d)?):/) do |name|+ code = $1++ if code && Emoji.custom?(code)+ emoji = Emoji[code]+ "<img src=\"#{emoji.url}\" title=\"#{code}\" class=\"emoji\" alt=\"#{code}\">"+ elsif code && Emoji.exists?(code)+ "<img src=\"#{Emoji.url_for(code)}\" title=\"#{code}\" class=\"emoji\" alt=\"#{code}\">"+ else+ name+ end+ end+ endenddiff --git a/lib/onebox/engine/github_issue_onebox.rb b/lib/onebox/engine/github_issue_onebox.rbindex 0bb26b70ac2..4d387f47995 100644--- a/lib/onebox/engine/github_issue_onebox.rb+++ b/lib/onebox/engine/github_issue_onebox.rb@@ -31,19 +31,23 @@ module Oneboxbody, excerpt = compute_body(raw['body'])ulink = URI(link)+ labels = raw['labels'].map do |l|+ { name: Emoji.codes_to_img(l['name']) }+ end+{link: @url,- title: raw["title"],+ title: raw['title'],body: body,excerpt: excerpt,- labels: raw["labels"],+ labels: labels,user: raw['user'],- created_at: created_at.strftime("%I:%M%p - %d %b %y %Z"),- created_at_date: created_at.strftime("%F"),- created_at_time: created_at.strftime("%T"),- closed_at: closed_at&.strftime("%I:%M%p - %d %b %y %Z"),- closed_at_date: closed_at&.strftime("%F"),- closed_at_time: closed_at&.strftime("%T"),+ created_at: created_at.strftime('%I:%M%p - %d %b %y %Z'),+ created_at_date: created_at.strftime('%F'),+ created_at_time: created_at.strftime('%T'),+ closed_at: closed_at&.strftime('%I:%M%p - %d %b %y %Z'),+ closed_at_date: closed_at&.strftime('%F'),+ closed_at_time: closed_at&.strftime('%T'),closed_by: raw['closed_by'],avatar: "https://avatars1.githubusercontent.com/u/#{raw['user']['id']}?v=2&s=96",domain: "#{ulink.host}/#{ulink.path.split('/')[1]}/#{ulink.path.split('/')[2]}",diff --git a/lib/onebox/templates/githubissue.mustache b/lib/onebox/templates/githubissue.mustacheindex f8766736b34..54989581c0f 100644--- a/lib/onebox/templates/githubissue.mustache+++ b/lib/onebox/templates/githubissue.mustache@@ -29,7 +29,9 @@<div class="labels">{{#labels}}- <span style="display:inline-block;margin-top:2px;background-color: #B8B8B8;padding: 2px;border-radius: 4px;color: #fff;margin-left: 3px;">{{name}}</span>+ <span style="display:inline-block;margin-top:2px;background-color: #B8B8B8;padding: 2px;border-radius: 4px;color: #fff;margin-left: 3px;">+ {{{name}}}+ </span>{{/labels}}</div></div>diff --git a/spec/helpers/emoji_helper_spec.rb b/spec/helpers/emoji_helper_spec.rbdeleted file mode 100644index a8b13a9ccbc..00000000000--- a/spec/helpers/emoji_helper_spec.rb+++ /dev/null@@ -1,32 +0,0 @@-# coding: utf-8-# frozen_string_literal: true--require 'rails_helper'--describe EmojiHelper do- before do- Plugin::CustomEmoji.clear_cache- end-- after do- Plugin::CustomEmoji.clear_cache- end-- describe "emoji_codes_to_img" do- it "replaces emoji codes by images" do- Plugin::CustomEmoji.register("xxxxxx", "/public/xxxxxx.png")-- str = "This is a good day :xxxxxx: :woman: :man:t4:"- replaced_str = helper.emoji_codes_to_img(str)-- expect(replaced_str).to eq("This is a good day <img src=\"/public/xxxxxx.png\" title=\"xxxxxx\" class=\"emoji\" alt=\"xxxxxx\"> <img src=\"/images/emoji/twitter/woman.png?v=#{Emoji::EMOJI_VERSION}\" title=\"woman\" class=\"emoji\" alt=\"woman\"> <img src=\"/images/emoji/twitter/man/4.png?v=#{Emoji::EMOJI_VERSION}\" title=\"man:t4\" class=\"emoji\" alt=\"man:t4\">")- end-- it "doesn't replace if code doesn't exist" do- str = "This is a good day :woman: :foo: :bar:t4: :man:t8:"- replaced_str = helper.emoji_codes_to_img(str)-- expect(replaced_str).to eq("This is a good day <img src=\"/images/emoji/twitter/woman.png?v=#{Emoji::EMOJI_VERSION}\" title=\"woman\" class=\"emoji\" alt=\"woman\"> :foo: :bar:t4: :man:t8:")- end- end-enddiff --git a/spec/models/emoji_spec.rb b/spec/models/emoji_spec.rbindex f7f57ae26a0..df309f4bb75 100644--- a/spec/models/emoji_spec.rb+++ b/spec/models/emoji_spec.rb@@ -87,4 +87,24 @@ describe Emoji doendend+ describe '.codes_to_img' do+ before { Plugin::CustomEmoji.clear_cache }+ after { Plugin::CustomEmoji.clear_cache }++ it "replaces emoji codes by images" do+ Plugin::CustomEmoji.register("xxxxxx", "/public/xxxxxx.png")++ str = "This is a good day :xxxxxx: :woman: :man:t4:"+ replaced_str = described_class.codes_to_img(str)++ expect(replaced_str).to eq("This is a good day <img src=\"/public/xxxxxx.png\" title=\"xxxxxx\" class=\"emoji\" alt=\"xxxxxx\"> <img src=\"/images/emoji/twitter/woman.png?v=#{Emoji::EMOJI_VERSION}\" title=\"woman\" class=\"emoji\" alt=\"woman\"> <img src=\"/images/emoji/twitter/man/4.png?v=#{Emoji::EMOJI_VERSION}\" title=\"man:t4\" class=\"emoji\" alt=\"man:t4\">")+ end++ it "doesn't replace if code doesn't exist" do+ str = "This is a good day :woman: :foo: :bar:t4: :man:t8:"+ replaced_str = described_class.codes_to_img(str)++ expect(replaced_str).to eq("This is a good day <img src=\"/images/emoji/twitter/woman.png?v=#{Emoji::EMOJI_VERSION}\" title=\"woman\" class=\"emoji\" alt=\"woman\"> :foo: :bar:t4: :man:t8:")+ end+ endend