Set target="_blank" for external links
This commit is contained in:
parent
e6daf1f899
commit
7be19db42f
3 changed files with 14 additions and 1 deletions
|
@ -15,6 +15,9 @@ module Banzai
|
|||
next if link.start_with?(internal_url)
|
||||
|
||||
node.set_attribute('rel', 'nofollow noreferrer')
|
||||
|
||||
# Open external links on a new tab
|
||||
node.set_attribute('target', '_blank')
|
||||
end
|
||||
|
||||
doc
|
||||
|
|
|
@ -173,10 +173,20 @@ describe 'GitLab Markdown', feature: true do
|
|||
expect(link.attr('rel')).to include('noreferrer')
|
||||
end
|
||||
|
||||
it 'adds _blank to target attribute for external links' do
|
||||
link = doc.at_css('a:contains("Google")')
|
||||
expect(link.attr('target')).to match('_blank')
|
||||
end
|
||||
|
||||
it 'ignores internal link' do
|
||||
link = doc.at_css('a:contains("GitLab Root")')
|
||||
expect(link.attr('rel')).not_to match 'nofollow'
|
||||
end
|
||||
|
||||
it 'does not set _blank to target attribute for internal links' do
|
||||
link = doc.at_css('a:contains("GitLab Root")')
|
||||
expect(link.attr('target')).not_to match '_blank'
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
|
|
2
spec/fixtures/markdown.md.erb
vendored
2
spec/fixtures/markdown.md.erb
vendored
|
@ -136,7 +136,7 @@ But it shouldn't autolink text inside certain tags:
|
|||
|
||||
### ExternalLinkFilter
|
||||
|
||||
External links get a `rel="nofollow"` attribute:
|
||||
External links get a `rel="nofollow noreferrer"` and `target="_blank"` attributes:
|
||||
|
||||
- [Google](https://google.com/)
|
||||
- [GitLab Root](<%= Gitlab.config.gitlab.url %>)
|
||||
|
|
Loading…
Reference in a new issue