Skip to content

Commit 2d74d02

Browse files
committed
Preserve # prefix for unresolved cross-references
When `#name` doesn't resolve to a method, the cross-reference handler was stripping the `#` and returning just the name. Now the original text including `#` is restored when the lookup fails. This fixes rendering of text like `#no-space-heading` in Markdown paragraphs, where the `#` was silently dropped in the final HTML.
1 parent bd0e544 commit 2d74d02

File tree

2 files changed

+17
-1
lines changed

2 files changed

+17
-1
lines changed

lib/rdoc/markup/to_html_crossref.rb

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@ def init_link_notation_regexp_handlings
6161
def cross_reference(name, text = nil, code = true, rdoc_ref: false)
6262
lookup = name
6363

64+
display_name = name
6465
name = name[1..-1] unless @show_hash if name[0, 1] == '#'
6566

6667
if !name.end_with?('+@', '-@') && match = name.match(/(.*[^#:])?@(.*)/)
@@ -73,7 +74,15 @@ def cross_reference(name, text = nil, code = true, rdoc_ref: false)
7374
text ||= name
7475
end
7576

76-
link lookup, text, code, rdoc_ref: rdoc_ref
77+
result = link lookup, text, code, rdoc_ref: rdoc_ref
78+
79+
# If the cross-reference didn't resolve to a link, restore the original
80+
# text including the '#' prefix that was stripped above.
81+
if result == text && display_name != name && !text.start_with?('#')
82+
display_name
83+
else
84+
result
85+
end
7786
end
7887

7988
##

test/rdoc/markup/to_html_crossref_test.rb

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -435,6 +435,13 @@ def hyper(reference)
435435
"rdoc-ref:#{reference}"
436436
end
437437

438+
def test_handle_regexp_CROSSREF_hash_preserved_for_unresolved
439+
@to.show_hash = false
440+
441+
# #no should not lose its '#' when it doesn't resolve to a method
442+
assert_equal "#no", REGEXP_HANDLING('#no')
443+
end
444+
438445
def tidy(reference)
439446
"{tidy}[rdoc-ref:#{reference}]"
440447
end

0 commit comments

Comments
 (0)