Skip to content

Commit ac7bcf5

Browse files
towandanicolas-fricke
authored andcommitted
Export the image element href attribute as a link (#139)
If the image element has an href attribute, we nest the figure tag of the image inside a link.
1 parent 61f6fcc commit ac7bcf5

File tree

2 files changed

+49
-5
lines changed

2 files changed

+49
-5
lines changed

lib/article_json/export/common/html/elements/image.rb

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,24 +7,38 @@ module Image
77
include ArticleJSON::Export::Common::HTML::Elements::Shared::Caption
88
include ArticleJSON::Export::Common::HTML::Elements::Shared::Float
99

10-
# Generate the `<figure>` node containing the image and caption
10+
# Generate the `<figure>` node containing the image and caption or
11+
# an `<a>` node containing the `<figure>` node if href is present.
1112
# @return [Nokogiri::XML::Element]
1213
def export
14+
figure_node
15+
end
16+
17+
private
18+
19+
# @return [Nokogiri::XML::NodeSet]
20+
def figure_node
1321
create_element(:figure, node_opts) do |figure|
14-
figure.add_child(image_node)
22+
node = @element.href.present? ? href_node : image_node
23+
figure.add_child(node)
1524
if @element.caption&.any?
1625
figure.add_child(caption_node(:figcaption))
1726
end
1827
end
1928
end
2029

21-
private
22-
2330
# @return [Nokogiri::XML::NodeSet]
2431
def image_node
2532
create_element(:img, src: @element.source_url)
2633
end
2734

35+
# @return [Nokogiri::XML::NodeSet]
36+
def href_node
37+
create_element(:a, href: @element.href) do |a|
38+
a.add_child(image_node)
39+
end
40+
end
41+
2842
# @return [Hash]
2943
def node_opts
3044
return if floating_class.nil?

spec/article_json/export/html/elements/image_spec.rb

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,12 @@
55
ArticleJSON::Elements::Image.new(
66
source_url: '/foo/bar.jpg',
77
caption: caption,
8-
float: float
8+
float: float,
9+
href: url
910
)
1011
end
1112
let(:float) { nil }
13+
let(:url) { nil }
1214
let(:caption) { [ArticleJSON::Elements::Text.new(content: 'Foo Bar')] }
1315

1416
describe '#export' do
@@ -45,5 +47,33 @@
4547
let(:expected_html) { '<figure><img src="/foo/bar.jpg"></figure>' }
4648
it { should eq expected_html }
4749
end
50+
51+
context 'when the image has an href' do
52+
let(:caption) { [] }
53+
let(:url) { 'http://devex.com' }
54+
let(:expected_html) do
55+
'<figure><a href="http://devex.com">' \
56+
'<img src="/foo/bar.jpg"></a></figure>'
57+
end
58+
it { should eq expected_html }
59+
end
60+
61+
context 'when the image has an href and a caption with a link' do
62+
let(:caption) do
63+
[
64+
ArticleJSON::Elements::Text.new(
65+
content: 'Foo Bar',
66+
href: 'http://foo.io'
67+
)
68+
]
69+
end
70+
let(:url) { 'http://devex.com' }
71+
let(:expected_html) do
72+
'<figure><a href="http://devex.com">' \
73+
'<img src="/foo/bar.jpg"></a>' \
74+
'<figcaption><a href="http://foo.io">Foo Bar</a></figcaption></figure>'
75+
end
76+
it { should eq expected_html }
77+
end
4878
end
4979
end

0 commit comments

Comments
 (0)