Browse Source

Add nofollow when host is missing

master
Carl Chenet 3 years ago
parent
commit
31b9ec672f
2 changed files with 14 additions and 1 deletions
  1. +1
    -1
      extras/markdowner.rb
  2. +13
    -0
      spec/models/markdowner_spec.rb

+ 1
- 1
extras/markdowner.rb View File

@@ -27,7 +27,7 @@ class Markdowner

# make links have rel=nofollow
ng.css("a").each do |h|
h[:rel] = "nofollow" unless h[:href].starts_with?("/")
h[:rel] = "nofollow" unless URI.parse(h[:href]).host.nil?
end

ng.at_css("body").inner_html


+ 13
- 0
spec/models/markdowner_spec.rb View File

@@ -35,4 +35,17 @@ describe Markdowner do
"<p>hi <a href=\"http://example.com/@blahblah/\" rel=\"nofollow\">" <<
"test</a></p>"
end

it "correctly adds nofollow" do
Markdowner.to_html("[ex](http://example.com)").should ==
"<p><a href=\"http://example.com\" rel=\"nofollow\">" <<
"ex</a></p>"

Markdowner.to_html("[ex](//example.com)").should ==
"<p><a href=\"//example.com\" rel=\"nofollow\">" <<
"ex</a></p>"

Markdowner.to_html("[ex](/u/abc)").should ==
"<p><a href=\"/u/abc\">ex</a></p>"
end
end

Loading…
Cancel
Save