Daniel May Daniel May - 3 months ago 29
HTML Question

How to truncate hyperlink text but not hyperlink itself in Ruby on Rails?

Happily using

Rinku
gem to autolink my text. This is helpful when someone pastes in a URL in a comment -
Rinku
will autolink that URL.

However, really long URLs are messing with the page layout. It would be helpful for every hyperlink encountered:


  • Shorten the hyperlink text

  • Keep the underlying hyperlink



e.g.
http://www.yahoo.com
may be displayed as
http://www.ya...
but in the underlying HTML, the hyperlink is
http://www.yahoo.com
. Twitter does this with tweets.

Have been searching high and low for any existing gems or prior experience on this. Haven't come up with anything so far.

Answer

Looks like Rinku supports customization of the link text:

auto_link(text) do |url|
    url.truncate :length => 15
end
Comments