Processor to add `rel=nofollow`

rails app:template LOCATION='https://perron.railsdesigner.com/library/no-follow-processor/template.rb'

This snippet adds a processor for the markdownify helper to add rel=nofollow to your outgoing links, it skips internal links—those starting with a / and # (anchors).

Template source

create_file "app/processors/nofollow_processor.rb", <<~RUBY
  class NofollowProcessor < Perron::HtmlProcessor::Base
    def process
      @html.css("a").each do |link|
        next if skippable? link

        link["rel"] = "nofollow"
      end
    end

    private

    def skippable?(link)
      href = link["href"]

      # when using absolute urls for your internal links, make sure to include
      # those as well e.g., `href.start_with?("https://example.com/")`
      href.blank? ||
        href.start_with?("/", "#", "mailto:") ||
        link["rel"].present?
    end
  end
RUBY