Skip to content

Commit

Permalink
handle stripped tags between exclamation marks and links
Browse files Browse the repository at this point in the history
  • Loading branch information
ehsandarroudi committed Aug 10, 2015
1 parent c254571 commit 966e3d7
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 1 deletion.
8 changes: 7 additions & 1 deletion lib/reverse_markdown/converters/a.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,16 @@ def convert(node)
name
else
link = "[#{name}](#{href}#{title})"
link.prepend(' ') if node.previous_sibling.to_s.end_with?('!')
link.prepend(' ') if prepend_space?(node)
link
end
end

private

def prepend_space?(node)
node.at_xpath("preceding::text()[1]").to_s.end_with?('!')
end
end

register :a, A.new
Expand Down
1 change: 1 addition & 0 deletions spec/assets/anchors.html
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
<a href="http://strong.foobar.com"><strong>Strong foobar</strong></a>
There should be no extra space before and after the anchor (<a href="http://foobar.com">stripped</a>).
Exception: after an !<a href="http://not.an.image.foobar.com">there</a> should be an extra space.
Even with stripped elements inbetween: !<span><a href="http://still.not.an.image.foobar.com">there</a></span> should be an extra space.

ignore <a href="foo.html"> </a> anchor tags with no link text
not ignore <a href="foo.html"><img src="image.png" alt="An Image" /></a> anchor tags with images
Expand Down
1 change: 1 addition & 0 deletions spec/components/anchors_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
it { is_expected.to include ' ![foobar image 2](http://foobar.com/foobar2.png "this is the foobar image 2") ' }
it { is_expected.to include 'no extra space before and after the anchor ([stripped](http://foobar.com)).'}
it { is_expected.to include 'after an ! [there](http://not.an.image.foobar.com) should be an extra space.'}
it { is_expected.to include 'with stripped elements inbetween: ! [there](http://still.not.an.image.foobar.com) should be an extra space.'}

context "links to ignore" do
it { is_expected.to include ' ignore anchor tags with no link text ' }
Expand Down

0 comments on commit 966e3d7

Please sign in to comment.