Skip to content

Commit

Permalink
Added short_domains argument to initialize.
Browse files Browse the repository at this point in the history
Added short_domains argument to initialize method to accept Booleans.
This is false by default, and if you set it to true, Rinku::AUTOLINK_SHORT_DOMAINS will be set to the flags argument of Rinku.auto_link.
  • Loading branch information
rhiroe committed Feb 28, 2022
1 parent e74ea5d commit 0c426e8
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 2 deletions.
9 changes: 7 additions & 2 deletions lib/auto_html/link.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,14 @@
module AutoHtml
# Link filter
class Link
def initialize(target: nil, rel: nil)
def initialize(target: nil, rel: nil, short_domains: false)
@target = target
@rel = rel
@short_domains = short_domains
end

def call(text)
Rinku.auto_link(text, :all, attributes)
Rinku.auto_link(text, :all, attributes, nil, flags)
end

private
Expand All @@ -29,5 +30,9 @@ def rel_attr
def target_attr
%(target="#{@target}") if @target
end

def flags
@short_domains ? Rinku::AUTOLINK_SHORT_DOMAINS : 0
end
end
end
11 changes: 11 additions & 0 deletions spec/auto_html/link_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,11 @@
expect(result).to eq '<a href="http://www.google.com/#q=nikola+tesla&ct=tesla09&oi=ddle&fp=Xmf0jJ9P_V0">http://www.google.com/#q=nikola+tesla&ct=tesla09&oi=ddle&fp=Xmf0jJ9P_V0</a>'
end

it 'not transforms short domain URL' do
result = subject.call('http://localhost:3000')
expect(result).to eq 'http://localhost:3000'
end

it 'transforms with target options' do
filter = described_class.new(target: '_blank')
result = filter.call('http://rors.org')
Expand All @@ -45,6 +50,12 @@
expect(result).to eq '<a href="http://rors.org" rel="nofollow">http://rors.org</a>'
end

it 'transforms with short_domains options' do
filter = described_class.new(short_domains: true)
result = filter.call('http://localhost:3000')
expect(result).to eq '<a href="http://localhost:3000">http://localhost:3000</a>'
end

it 'transforms with target and rel options' do
filter = described_class.new(target: '_blank', rel: 'nofollow')
result = filter.call('http://rors.org')
Expand Down

0 comments on commit 0c426e8

Please sign in to comment.