]> git.openstreetmap.org Git - rails.git/commitdiff
Allow redcarpet to add nofollow to links
authorTom Hughes <tom@compton.nu>
Thu, 24 Mar 2016 08:36:32 +0000 (08:36 +0000)
committerTom Hughes <tom@compton.nu>
Thu, 24 Mar 2016 08:36:32 +0000 (08:36 +0000)
This, using a new feature in redcarpet, allows us to avoid having
to subclass the renderer and implement links ourselves, which in
turn means that redcarpet's link sanitisation works.

lib/rich_text.rb

index 9e43ed29c0c96e3079dc02efacdc9e4128a6f686..83eb235401c6f88cf0a0d6eab46b9ce781dcea2c 100644 (file)
@@ -80,26 +80,12 @@ module RichText
     end
 
     def self.html_renderer
-      @html_renderer ||= Renderer.new(:filter_html => true, :safe_links_only => true)
+      @html_renderer ||= Redcarpet::Render::XHTML.new(:filter_html => true, :safe_links_only => true, :link_attributes => { :rel => "nofollow" })
     end
 
     def self.html_parser
       @html_parser ||= Redcarpet::Markdown.new(html_renderer, :no_intra_emphasis => true, :autolink => true, :space_after_headers => true)
     end
-
-    class Renderer < Redcarpet::Render::XHTML
-      def link(link, _title, alt_text)
-        "<a rel=\"nofollow\" href=\"#{link}\">#{alt_text}</a>"
-      end
-
-      def autolink(link, link_type)
-        if link_type == :email
-          "<a rel=\"nofollow\" href=\"mailto:#{link}\">#{link}</a>"
-        else
-          "<a rel=\"nofollow\" href=\"#{link}\">#{link}</a>"
-        end
-      end
-    end
   end
 
   class Text < Base