Skip to content

Commit 330a0f5

Browse files
ybiquitouskou
andauthored
Fix image tag with external source (#1348)
When `rdoc-image` contains an external source, such as `https://example.com/path/to/image.svg`, the rendered `<img>` tag is broken. This PR fixes the bug. For example, I found the problem on the IRB website, where a Markdown image is present as a source. Rendered HTML on the website <https://ruby.github.io/irb/>: ```html <a href="https://badge.fury.io/rb/irb"><img src="https" alt="//badge.fury.io/rb/irb.svg:Gem Version"></a> ``` Screenshot: <img width="359" alt="image" src="https://github.com/user-attachments/assets/bf138616-4296-4e30-9eaa-5ee7301e8681" /> Markdown source: <https://github.com/ruby/irb/blob/6349b03f64f173a9d31bc9042430e1134f3e1689/doc/Index.md?plain=1#L3> ```markdown [![Gem Version](https://badge.fury.io/rb/irb.svg)](https://badge.fury.io/rb/irb) ``` Probably, this bug was introduced in the commit 894b2f1, i.e., <https://github.com/ruby/rdoc/releases/tag/v6.13.0>. --------- Co-authored-by: Sutou Kouhei <[email protected]>
1 parent 9774b48 commit 330a0f5

File tree

2 files changed

+34
-1
lines changed

2 files changed

+34
-1
lines changed

lib/rdoc/markup/to_html.rb

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,10 @@ def handle_RDOCLINK(url) # :nodoc:
9898

9999
gen_url CGI.escapeHTML(url), CGI.escapeHTML(text)
100100
when /^rdoc-image:/
101-
url, alt = $'.split(":", 2) # Split the string after "rdoc-image:" into url and alt
101+
# Split the string after "rdoc-image:" into url and alt.
102+
# "path/to/image.jpg:alt text" => ["path/to/image.jpg", "alt text"]
103+
# "http://example.com/path/to/image.jpg:alt text" => ["http://example.com/path/to/image.jpg", "alt text"]
104+
url, alt = $'.split(/:(?!\/)/, 2)
102105
if alt && !alt.empty?
103106
%[<img src="#{CGI.escapeHTML(url)}" alt="#{CGI.escapeHTML(alt)}">]
104107
else

test/rdoc/test_rdoc_markup_to_html.rb

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -749,6 +749,36 @@ def test_convert_TIDYLINK_image
749749
assert_not_include result, "<script>"
750750
end
751751

752+
def test_convert_TIDYLINK_image_with_alt
753+
result =
754+
@to.convert '{rdoc-image:path/to/image.jpg:alt text}[http://example.com]'
755+
756+
expected =
757+
"\n<p><a href=\"http://example.com\"><img src=\"path/to/image.jpg\" alt=\"alt text\"></a></p>\n"
758+
759+
assert_equal expected, result
760+
end
761+
762+
def test_convert_TIDYLINK_image_external
763+
result =
764+
@to.convert '{rdoc-image:http://example.com/path/to/image.jpg}[http://example.com]'
765+
766+
expected =
767+
"\n<p><a href=\"http://example.com\"><img src=\"http://example.com/path/to/image.jpg\"></a></p>\n"
768+
769+
assert_equal expected, result
770+
end
771+
772+
def test_convert_TIDYLINK_image_external_with_alt
773+
result =
774+
@to.convert '{rdoc-image:http://example.com/path/to/image.jpg:alt text}[http://example.com]'
775+
776+
expected =
777+
"\n<p><a href=\"http://example.com\"><img src=\"http://example.com/path/to/image.jpg\" alt=\"alt text\"></a></p>\n"
778+
779+
assert_equal expected, result
780+
end
781+
752782
def test_convert_TIDYLINK_rdoc_label
753783
result = @to.convert '{foo}[rdoc-label:foottext-1]'
754784

0 commit comments

Comments
 (0)