Reputation: 14277
So https://github.com/github/gemoji maps stuff like :cats:
and :dogs:
into emojis.
Example from Forem:
def forem_emojify(content)
h(content).to_str.gsub(/:([a-z0-9\+\-_]+):/) do |match|
if Emoji.names.include?($1)
'<img alt="' + $1 + '" height="20" src="' + asset_path("emoji/#{$1}.png") + '" style="vertical-align:middle" width="20" />'
else
match
end
end.html_safe if content.present?
end
How does one extend this to also map stuff like :)
, ;)
etc.?
Upvotes: 0
Views: 820
Reputation: 717
I faced this same limitation. Ultimately wrote a parser extension for gemoji with emoticon support. See gemoji-parser.
Upvotes: 1
Reputation: 58
How about this?
def forem_emojify(content)
h(content).to_str.gsub(':)', ':smile:').gsub(';)', ':wink:').gsub(/:([a-z0-9\+\-_]+):/) do |match|
if Emoji.names.include?($1)
'<img alt="' + $1 + '" height="20" src="' + asset_path("emoji/#{$1}.png") + '" style="vertical-align:middle" width="20" />'
else
match
end
end.html_safe if content.present?
end
Edit with more information:
Why can't we just use the edit_emoji
and add_alias
methods?
Well, we can, like this:
Emoji.edit_emoji( Emoji.find_by_alias('smile') ) do |emoji|
emoji.add_alias(":)")
end
that totally works! Now you can do this:
emoji = Emoji.find_by_alias(":)") #=> #<Emoji::Character:smile(1f604)>
emoji.name #=> "smile"
emoji.aliases #=> ["smile", ":)"]
emoji.raw #=> 😄
but here's the problem: when you have unknown input like a comment, you have to find the aliases within the comment.
content = "I'm so happy :grinning: lol I just wanna :) but I'm :grimacing:"
emoji_pattern = /:([a-z0-9\+\-_]+):/
content.scan(emoji_pattern) #=> [["grinning"], ["grimacing"]]
you can see that when you have a string and you use that regular expression from the code sample. it only finds substrings that begin and end with a colon. You can also see that it extracts the alias from the colons, and then uses the name-without-colons to look up the emoji by alias. Our custom alias :) doesn't fit that pattern -- it begins with a colon but it doesn't end with one...
So what can you do? Sorry, but I dunno! There's a good reason GitHub suggests using a character to delimit your aliases -- it's pretty hard to find significant substrings in a big chunk of text. You can try improving the regular expression to also look for your aliases maybe?
Upvotes: 1