Detecting if a link is internal and shorten it accordingly
I have been thinking about making a helper method that shortens internal links. For example: if my website is example.com and a user posts a link to http://www.example.com/posts/80 it would be nice to shorten the link text to post#80 and http://www.example.com/comments/5 to comment#5.
would this su开发者_高级运维ffice
url["http://www.example.com/posts/"] = "post#"
Or should I use a regex for this instead?
regexp!
SITE_URL = 'http://www.example.com' # make sure there is no trailing slash /
url = "http://www.example.com/posts/80"
short_url = url.sub( %r{^#{SITE_URL}(.*)$} , '\1')
=> "/posts/80"
精彩评论