From 3b37f2515ffbb4e61ce7fb2afa969dff10129cf9 Mon Sep 17 00:00:00 2001 From: Shaisolaris <66793913+Shaisolaris@users.noreply.github.com> Date: Mon, 12 Oct 2026 04:56:32 -0700 Subject: [PATCH] Keep the port in rdoc-image URLs An image link whose URL has a port, such as ![logo](http://localhost:8080/logo.png) in Markdown or rdoc-image:http://localhost:8080/logo.png, was split at the colon before the port, so the image src became http://localhost and the rest of the URL went into the alt text. Split the URL and the alt text after the optional scheme, host and port instead of at the first colon not followed by a slash. --- lib/rdoc/markup/to_html.rb | 10 +++++++++- test/rdoc/markup/to_html_test.rb | 16 ++++++++++++++++ 2 files changed, 25 insertions(+), 1 deletion(-) diff --git a/lib/rdoc/markup/to_html.rb b/lib/rdoc/markup/to_html.rb index 6477f98910..5897fa0cbf 100644 --- a/lib/rdoc/markup/to_html.rb +++ b/lib/rdoc/markup/to_html.rb @@ -154,6 +154,13 @@ def initialize(pipe: false, output_decoration: true) UNSAFE_URL_SCHEME_REGEXP = /\A(?:javascript|vbscript|data|file):/i + ## + # Splits the part of an rdoc-image link after "rdoc-image:" into the + # image URL and the alt text. A port number after the host is part of + # the URL. + + IMAGE_URL_AND_ALT_REGEXP = %r{\A((?:[A-Za-z][A-Za-z0-9+.\-]*://[^/:?#]*(?::\d+(?=[/?#:]|\z))?)?[^:]*)(?::(.*))?\z}m + # :nodoc: URL_CHARACTERS_REGEXP_STR = /[A-Za-z0-9\-._~:\/\?#\[\]@!$&'\(\)*+,;%=]/.source @@ -203,7 +210,8 @@ def handle_RDOCLINK(url) # :nodoc: # Split the string after "rdoc-image:" into url and alt. # "path/to/image.jpg:alt text" => ["path/to/image.jpg", "alt text"] # "http://example.com/path/to/image.jpg:alt text" => ["http://example.com/path/to/image.jpg", "alt text"] - url, alt = $'.split(/:(?!\/)/, 2) + # "http://example.com:8080/image.jpg:alt text" => ["http://example.com:8080/image.jpg", "alt text"] + url, alt = $'.match(IMAGE_URL_AND_ALT_REGEXP).captures if alt && !alt.empty? %[#{CGI.escapeHTML(alt)}] else diff --git a/test/rdoc/markup/to_html_test.rb b/test/rdoc/markup/to_html_test.rb index 6d0107602c..c1c3e220f6 100644 --- a/test/rdoc/markup/to_html_test.rb +++ b/test/rdoc/markup/to_html_test.rb @@ -903,6 +903,22 @@ def test_convert_TIDYLINK_image_external assert_equal expected, result end + def test_convert_TIDYLINK_image_external_with_port + result = + @to.convert '{rdoc-image:http://example.com:8080/path/to/image.jpg:alt text}[http://example.com]' + + expected = + "\n

\"alt

\n" + + assert_equal expected, result + end + + def test_convert_RDOCLINK_image_external_with_port_without_alt + result = @to.convert 'rdoc-image:http://localhost:8080/image.png' + + assert_equal "\n

\n", result + end + def test_convert_TIDYLINK_image_external_with_alt result = @to.convert '{rdoc-image:http://example.com/path/to/image.jpg:alt text}[http://example.com]'