Redcarpet custom renderer: Add different link attributes to markdown content

I am trying to make Redcarpet working in a way where I can attach different link attributes depending where the link points to: if this is my other website, don’t add rel="nofollow" (could be an array with a whitelist addresses), if this is another foreign website, add rel="nofollow" attribute.

What will be the easiest way to achieve it? I’ve tried to add custom renderer to Redcarpet and was able to achieve it but it broke my other feature which is table of contents (basically the anchor links to headings were gone) so the custom renderer completely overrides all other Redcarpet settings and I don’t know how can I limit it to the normal links that happen in paragraphs only.

Or perhaps there is a simple way that doesn’t involve custom renderers?

Just make your own helper in /helpers/custom_helpers.rb that you will use instead of <A> tag or link_to helper. Something like:

  def link_to_enhanced(page_url, options={})
    if external_url() # this is your code to check whitelist, etc.
      options.merge!({rel: 'nofollow'})
    end
    return link_to(page_url, options)
  end

Please verify this code, it’s based on something similar, but not exactly what my project has. I’m not sure if the hash {rel: 'nofollow'} is correct for link_to use?