Skip to content

Add target="_blank" to external links #2446

Open
@jorgegarv

Description

@jorgegarv

Checklist

Is your feature request related to a problem? Please describe

Currently, the theme’s HTML anchors ( tags) do not include target="_blank" for external links, which forces users to leave the site when clicking. This affects:

Social media links in footer/sidebar.

Any external URL in posts/pages.

Describe the solution you'd like

I've developed a lightweight Jekyll plugin (external_blank.rb) that automatically adds:

target="_blank" rel="noopener noreferrer"

to all external links, with:
✅ Zero config – Works out-of-the-box
✅ Selective targeting – Only modifies external links (checks href for http:///https://)
✅ Live tested – Successfully implemented on my blog

Implementation:

Add this to _plugins/external_blank.rb:

# _plugins/external_blank.rb
module Jekyll
  module ExternalLinks
    class Processor
      def process(content)
        return content if !content.is_a?(String)
        
        content.gsub(/<a\s+(.*?)>/m) do |match|
          attrs = $1
          
          next match if attrs.include?('target=') || attrs.include?('href="#') || attrs.include?('mailto:')
          
          if attrs.include?('href="http')
            "<a #{attrs} target=\"_blank\" rel=\"noopener noreferrer\">"
          else
            match
          end
        end
      end
    end
  end
end

Jekyll::Hooks.register :pages, :post_render do |page|
  processor = Jekyll::ExternalLinks::Processor.new
  page.output = processor.process(page.output)
end

Jekyll::Hooks.register :posts, :post_render do |post|
  processor = Jekyll::ExternalLinks::Processor.new
  post.output = processor.process(post.output)
end

Describe alternatives you've considered

No response

Additional context

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or request

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions