From f68f59c24ff2532c02e29ccc3b8023a91692d9d9 Mon Sep 17 00:00:00 2001 From: Sam Saffron Date: Mon, 11 Feb 2013 18:58:19 +1100 Subject: [PATCH] setting to exclude rel nofollow from particular domains --- app/models/site_setting.rb | 3 +-- config/locales/en.yml | 1 + lib/pretty_text.rb | 7 ++++++- spec/components/pretty_text_spec.rb | 9 +++++++++ 4 files changed, 17 insertions(+), 3 deletions(-) diff --git a/app/models/site_setting.rb b/app/models/site_setting.rb index 5dcf86d25db..37ec0810eab 100644 --- a/app/models/site_setting.rb +++ b/app/models/site_setting.rb @@ -30,8 +30,6 @@ class SiteSetting < ActiveRecord::Base client_setting(:max_topic_title_length, 255) client_setting(:flush_timings_secs, 5) - - # settings only available server side setting(:auto_track_topics_after, 60000) setting(:long_polling_interval, 15000) @@ -92,6 +90,7 @@ class SiteSetting < ActiveRecord::Base setting(:allow_duplicate_topic_titles, false) setting(:add_rel_nofollow_to_user_content, true) + setting(:exclude_rel_nofollow_domains, '') setting(:post_excerpt_maxlength, 300) setting(:post_onebox_maxlength, 500) setting(:best_of_score_threshold, 15) diff --git a/config/locales/en.yml b/config/locales/en.yml index 8c4ab9945db..26dae763371 100644 --- a/config/locales/en.yml +++ b/config/locales/en.yml @@ -220,6 +220,7 @@ en: category_featured_topics: "number of topics displayed in the category list" popup_delay: "Length of time in ms before popups appear on the screen" add_rel_nofollow_to_user_content: "Add rel nofollow to all submitted user content, except for internal links (including parent domains) changing this requires you update all your baked markdown" + exclude_rel_nofollow_domains: "A comma delimited list of domains where nofollow is not added (tld.com will automatically allow sub.tld.com as well)" post_excerpt_maxlength: "Maximum length in chars of a post's excerpt." post_onebox_maxlength: "Maximum length of a oneboxed discourse post." category_post_template: "The post template that appears once you create a category" diff --git a/lib/pretty_text.rb b/lib/pretty_text.rb index cb28ebac680..87adc35baf2 100644 --- a/lib/pretty_text.rb +++ b/lib/pretty_text.rb @@ -180,6 +180,11 @@ module PrettyText end def self.add_rel_nofollow_to_user_content(html) + whitelist = [] + l = SiteSetting.exclude_rel_nofollow_domains + if l.present? + whitelist = l.split(",") + end site_uri = nil doc = Nokogiri::HTML.fragment(html) doc.css("a").each do |l| @@ -188,7 +193,7 @@ module PrettyText uri = URI(href) site_uri ||= URI(Discourse.base_url) - if !uri.host.present? || uri.host.ends_with?(site_uri.host) + if !uri.host.present? || uri.host.ends_with?(site_uri.host) || whitelist.any?{|u| uri.host.ends_with?(u)} # we are good no need for nofollow else l["rel"] = "nofollow" diff --git a/spec/components/pretty_text_spec.rb b/spec/components/pretty_text_spec.rb index 430b8ac62a3..5aaa9330ede 100644 --- a/spec/components/pretty_text_spec.rb +++ b/spec/components/pretty_text_spec.rb @@ -79,6 +79,7 @@ test describe "rel nofollow" do before do SiteSetting.stubs(:add_rel_nofollow_to_user_content).returns(true) + SiteSetting.stubs(:exclude_rel_nofollow_domains).returns("foo.com,bar.com") end it "should inject nofollow in all user provided links" do @@ -92,6 +93,14 @@ test it "should not inject nofollow in all subdomain links" do (PrettyText.cook("cnn") !~ /nofollow/).should be_true end + + it "should not inject nofollow for foo.com" do + (PrettyText.cook("cnn") !~ /nofollow/).should be_true + end + + it "should not inject nofollow for bar.foo.com" do + (PrettyText.cook("cnn") !~ /nofollow/).should be_true + end end describe "Excerpt" do