Add rel="nofollow" to links in comments (#3304)

* Add rel="nofollow" to links in comments

* Improve code quality and Add tests
This commit is contained in:
Bolarinwa Balogun 2019-06-26 09:02:11 -04:00 committed by Ben Halpern
parent b52d8e931d
commit dbc4abc587
3 changed files with 10 additions and 3 deletions

View file

@ -6,8 +6,9 @@ class MarkdownParser
@content = content
end
def finalize
renderer = Redcarpet::Render::HTMLRouge.new(hard_wrap: true, filter_html: false)
def finalize(link_attributes: {})
options = { hard_wrap: true, filter_html: false, link_attributes: link_attributes }
renderer = Redcarpet::Render::HTMLRouge.new(options)
markdown = Redcarpet::Markdown.new(renderer, REDCARPET_CONFIG)
catch_xss_attempts(@content)
escaped_content = escape_liquid_tags_in_codeblock(@content)

View file

@ -213,7 +213,7 @@ class Comment < ApplicationRecord
def evaluate_markdown
fixed_body_markdown = MarkdownFixer.fix_for_comment(body_markdown)
parsed_markdown = MarkdownParser.new(fixed_body_markdown)
self.processed_html = parsed_markdown.finalize
self.processed_html = parsed_markdown.finalize(link_attributes: { rel: "nofollow" })
wrap_timestamps_if_video_present!
shorten_urls!
end

View file

@ -79,6 +79,12 @@ RSpec.describe Comment, type: :model do
expect(comment.processed_html.include?(">1:52:30</a>")).to eq(false)
end
it "adds rel=nofollow to links" do
comment.body_markdown = "this is a comment with a link: http://dev.to"
comment.save
expect(comment.processed_html.include?('rel="nofollow"')).to eq(true)
end
it "adds a mention url if user is mentioned like @mention" do
comment.body_markdown = "Hello @#{user.username}, you are cool."
comment.save