nothingme
nothingme

Reputation: 55

Set target=“_blank” and rel=“nofollow, noindex, noreferrer” to all links

There seems to be a way how to set external links to no-follow per default to avoid losing important link juice through do-follow links to external websites it would be really good and make things much easier if we had automated that.

I have a lot of articles/blog posts in markdown that is being generated upon middleman deployment. It’s a hassle to manually add {:target="_blank" rel="nofollow, noindex, noreferrer"} at the end of each link within the text.

I've researched that I can add

<meta name =”robots” content=”index”>

but I'm guessing there must be a more granular approach is to include the noFollow tag for individual links.

Is there a way to set link attributes in config.rb so that it's set like below?

target="_blank" rel="nofollow, noindex, noreferrer"

Upvotes: 2

Views: 4845

Answers (1)

Alex Suslyakov
Alex Suslyakov

Reputation: 2222

A good approach here is to create your own helper-method under app/helpers and use it in your views

Smth like:

def link_to_new_window(name = nil, options = nil, html_options = {}, &block)
  html_options[:target] = '_blank'
  html_options[:rel] = 'nofollow, noindex, noreferrer'
  helper.link_to(name, options, html_options, &block)
end

UPD I see that you're using middleman. Don't have much experience with it, but you can decorate like this pretty-much any helper method

Note the helper call, it'll allow you to use Rails helpers when they not explicitly included

To decorate the original method you can do:

module LinkToWithNewWindow
  def link_to(name = nil, options = nil, html_options = {}, &block)
    html_options[:target] = '_blank'
    html_options[:rel] = 'nofollow, noindex, noreferrer'
    super(name, options, html_options, &block)
  end
end
::ActionView::Helpers::UrlHelper.prepend LinkToWithNewWindow

Replace ActionView with the helper you use if needed But again, do it at your own risk

Upvotes: 5

Related Questions