about summary refs log tree commit diff
path: root/app/helpers/emoji_helper.rb
blob: 848c03fce721aac80ed528f5d361bdc142bf0202 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
# frozen_string_literal: true

module EmojiHelper
  def emojify(text)
    return text if text.blank?

    text.gsub(emoji_pattern) do |match|
      emoji = Emoji.instance.unicode($1) # rubocop:disable Style/PerlBackrefs

      if emoji
        emoji
      else
        match
      end
    end
  end

  def emoji_pattern
    @emoji_pattern ||=
      /(?<=[^[:alnum:]:]|\n|^)
      (#{Emoji.instance.names.map { |name| Regexp.escape(name) }.join('|')})
      (?=[^[:alnum:]:]|$)/x
  end
end