Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Full URLs for displaying Emoji in email #13

Open
justinthiele opened this issue Mar 6, 2014 · 4 comments
Open

Full URLs for displaying Emoji in email #13

justinthiele opened this issue Mar 6, 2014 · 4 comments

Comments

@justinthiele
Copy link

Is there a way to have the full URL used in the image link instead of just the path? I'd like to be able to use Emoji in notification emails. Thanks!

@jordanbyron
Copy link
Contributor

Hi @justinthiele!

This is possible, but you'd have to make a slight modification to the gem's code. If you change this line to use asset_url rather than /assets/emojis/#{file_name} that should do the trick. The end result would look something like:

emoji_url = asset_url("emojis/#{file_name}")

%{<img src="#{emoji_url}" class="emoji" } +
            %{title="#{emoji_code}" alt="#{emoji_code}" #{default_size}>}

Or if you want to go above and beyond, allowing anyone to customize the emoji output, you could add an extra param to the initializer:

markdown = Redcarpet::Markdown.new(MdEmoji::Render, 
  :no_intra_emphasis => true, 
  :replace_emoji => lambda {|emoji, emoji_file_name| "<strong>#{emoji}</strong>" }
)

and then modify MdEmoji::Render#replace_emoji to look something like this:

def replace_emoji(text)
  text.gsub(/:([^\s:])+:/) do |emoji|

    emoji_code = emoji #.gsub("|", "_")
    emoji      = emoji_code.gsub(":", "")

    if MdEmoji::EMOJI.include?(emoji)
      file_name = "#{emoji.gsub('+', 'plus')}.png"
      return @custom_replace_emoji.call(emoji, file_name) if @custom_replace_emoji

      default_size = %{height="20" width="20"}

      %{<img src="/assets/emojis/#{file_name}" class="emoji" } +
        %{title="#{emoji_code}" alt="#{emoji_code}" #{default_size}>}
    else
      emoji_code
    end
  end
end

Just note you'd also need to capture the new argument in MdEmoji::Render#initialize and assign it to @custom_replace_emoji for my above example to work.

Personally I'm leaning towards the last option as it a) doesn't break the existing API and functionality and b) it allows much greater flexibility of the output emoji code. Let me know if you feel like this is something you feel comfortable undertaking. Take a look at our CONTRIBUTING.md file for info on how to submit a patch. Thanks!

@justinthiele
Copy link
Author

Hey @jordanbyron, thanks for the thorough response!

I had trouble getting Option A to work. I get a undefined method asset_url'. I tried ActionController::Base.helpers.asset_url` but that didn't work either.

It sounds like Option B might be the better way to go, unfortunately it's above my novice head.

As an interim (and terribly smelly) solution, I hard coded the URL. 💩

Really appreciate your help anyway! 👍

@jordanbyron
Copy link
Contributor

Ah that's what I get for writing code on my iPad while lounging on the couch 😜

Don't sweat hardcoding the url. Whatever gets the job done. I'll leave this issue open as a reminder for me to implement this feature. Once it's done I'll ping you. ⛵

@justinthiele
Copy link
Author

Awesome, thanks @jordanbyron! :bowtie:

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants