diff --git a/lib/rdoc/markup/to_html.rb b/lib/rdoc/markup/to_html.rb
index 8ae4dd4720..6a8ca8a2d7 100644
--- a/lib/rdoc/markup/to_html.rb
+++ b/lib/rdoc/markup/to_html.rb
@@ -357,8 +357,8 @@ class RDoc::Markup::ToHtml < RDoc::Markup::Formatter
url =~ /\.(gif|png|jpg|jpeg|bmp)$/ then
"
"
else
- if scheme != 'link' and /\.(?:rb|rdoc|md)\z/i =~ url
- url = url.sub(%r%\A([./]*)(.*)\z%) { "#$1#{$2.tr('.', '_')}.html" }
+ if scheme != 'link' and %r%\A([./]*+)(.*?\.)(rb|rdoc|md)(?=\z|#)%i =~ url
+ url = "#$1#{$2.tr('.', '_')}#$3.html#{$'}"
end
text = text.sub %r%^#{scheme}:/*%i, ''
diff --git a/test/rdoc/test_rdoc_markup_to_html.rb b/test/rdoc/test_rdoc_markup_to_html.rb
index 29da968abc..f7887f081b 100644
--- a/test/rdoc/test_rdoc_markup_to_html.rb
+++ b/test/rdoc/test_rdoc_markup_to_html.rb
@@ -743,6 +743,10 @@ EXPECTED
@to.gen_url('doc/example.rdoc', 'example')
assert_equal 'example',
@to.gen_url('../ex.doc/example.rdoc', 'example')
+ assert_equal 'example',
+ @to.gen_url('doc/example.rdoc#label-one', 'example')
+ assert_equal 'example',
+ @to.gen_url('../ex.doc/example.rdoc#label-two', 'example')
end
def test_gen_url_md_file
@@ -750,6 +754,10 @@ EXPECTED
@to.gen_url('doc/example.md', 'example')
assert_equal 'example',
@to.gen_url('../ex.doc/example.md', 'example')
+ assert_equal 'example',
+ @to.gen_url('doc/example.md#label-one', 'example')
+ assert_equal 'example',
+ @to.gen_url('../ex.doc/example.md#label-two', 'example')
end
def test_gen_url_rb_file
@@ -757,6 +765,10 @@ EXPECTED
@to.gen_url('doc/example.rb', 'example')
assert_equal 'example',
@to.gen_url('../ex.doc/example.rb', 'example')
+ assert_equal 'example',
+ @to.gen_url('doc/example.rb#label-one', 'example')
+ assert_equal 'example',
+ @to.gen_url('../ex.doc/example.rb#label-two', 'example')
end
def test_handle_regexp_HYPERLINK_link