简体   繁体   中英

Wrap links in text within hyperlinks - Ruby On Rails

I have a description for a news. To display the news text, I used simple_format which wraps the new lines into <p></p> tags.

However, the news text may contain links. Now I need to wrap the links within the text into <a></a> tags.

It would be very helpful if someone could give insights on how to do that.

Sample Text:

Lorem ipsum dolor sit amet, consectetur adipisicing elit. Ad, ea esse magni obcaecati quasi quis soluta velit. Accusamus doloribus ea et ex officia, pariatur quia saepe sed tempore temporibus totam https://stackoverflow.com/posts/36058019/edit

Desired Output:

Lorem ipsum dolor sit amet, consectetur adipisicing elit. Ad, ea esse magni obcaecati quasi quis soluta velit. Accusamus doloribus ea et ex officia, pariatur quia saepe sed tempore temporibus totam? <a src="https://stackoverflow.com/posts/36058019/edit">https://stackoverflow.com/posts/36058019/edit</a>

My Approach

For now I solved it by following function. First I split the string for newlines to ensure simple_format will insert appropriate p tags. Then, I match the texts with a url regex and then wrap it within a tag.

def parse_text(text)
    text.split("\n").map do |t|
      t.split(' ').map {|x| x.match(/https?:\/\/[\S]+/) ? "<a href=#{x}>#{x}</a>" : x }.join(' ')
    end.join("\n").html_safe
  end

as @devanand commented it's better if you wrap your text in desired tags and use html_safe

But as you required to wrap all urls from text within <a> tag.

> str = "Lorem ipsum dolor sit amet, consectetur adipisicing elit. Ad, ea esse magni obcaecati quasi quis soluta velit. Accusamus doloribus ea et ex officia, pariatur quia saepe sed tempore temporibus totam https://stackoverflow.com/posts/36058019/edit"
# extract all urls from string
> urls = URI.extract(str)
# wrap url text within <a> tag
> urls.each{|u| str.gsub!(u, "<a src='#{u}'>#{u}</a>")}
> puts str
#=> "Lorem ipsum dolor sit amet, consectetur adipisicing elit. Ad, ea esse magni obcaecati quasi quis soluta velit. Accusamus doloribus ea et ex officia, pariatur quia saepe sed tempore temporibus totam <a src='https://stackoverflow.com/posts/36058019/edit'>https://stackoverflow.com/posts/36058019/edit</a>"

Demo

Note: as RSB commented URI.extract won't work if there's no spaces at the beginning and end of the URL

I can suggest you a couple of ways to do this, the first and straight forward way is to use wysiwyg editor, eg CKEditor or Tinymce , this would be the best solution when it comes to writing articles.

Secondly, you can add some marker at the beginning and end of the URL to make parsing easier. eg

str = "Lorem ipsum dolor sit amet, consectetur adipisicing elit. Ad, ea esse magni obcaecati quasi quis soluta velit. Accusamus doloribus ea et ex officia, pariatur quia saepe sed tempore temporibus totam `http://stackoverflow.com/posts/36058019/edit`"

But this too will not be that much accurate. Thirdly you can extract URLs from the string as Gagan mentioned in his answer, but that too will fail when there's no space between the url or when the same URL is repeated multiple times.

So, I think it will be good to use any WYSIWYG editor to solve this problem for every case.

Hope that helps!

Functionality similar to this used to be part of Rails, but was removed in Rails 3.1. The Rails auto_link gem was extracted and made available for people upgrading. https://rubygems.org/gems/rails_autolink

This should enable you to convert links, then simple format the paragraphs:

simple_format(auto_link("..."))

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM