简体   繁体   中英

Markdown and external links

I have a Rails 4 app (which uses RedCarpet) and a helper method that looks like:

def markdown(text)
  options = {
    escape_html:     true,
    hard_wrap:       true, 
    link_attributes: { rel: 'nofollow', target: "_blank" },
    space_after_headers: true,
    fenced_code_blocks: true
  }

  extensions = {
    autolink:           true,
    superscript:        true,
    disable_indented_code_blocks: true
  }

  renderer = Redcarpet::Render::HTML.new(options)
  markdown = Redcarpet::Markdown.new(renderer, extensions)

  markdown.render(text).html_safe
end

My problem is that if someone puts in for example [Example here](www.example.com) , then is parsed, and someone clicks on the link, it goes to www.mywebsite.com/www.example.com , instead of www.example.com .

How do I fix this?

For me, running your method with "[Example here](www.example.com)" and RedCarpet 3.0 returns the following:

"<p><a href=\\"www.example.com\\" rel=\\"nofollow\\" target=\\"_blank\\">Example here</a></p>\\n"

So, it appears it is something else in your application. If you're noticing the problem on the client side (ie from clicking on a link in your browser), my guess is it's in your views somewhere in the HTML. I'd search your app's file directory, particularly any HTML, for "www.mywebsite.com".

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