Add nofollow when host is missing

This commit is contained in:
Carl Chenet 2017-05-20 15:30:51 +02:00
parent 19ac5369c5
commit 31b9ec672f
2 changed files with 14 additions and 1 deletions

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

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