Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 13 additions & 3 deletions lib/rdoc/markup/to_html_crossref.rb
Original file line number Diff line number Diff line change
Expand Up @@ -156,16 +156,26 @@ def link(name, text, code = true, rdoc_ref: false)
ref = @cross_reference.resolve name, text if name

case ref
when String then
when String
if rdoc_ref && @options.warn_missing_rdoc_ref
puts "#{@from_path}: `rdoc-ref:#{name}` can't be resolved for `#{text}`"
end
ref
else
path = ref ? ref.as_href(@from_path) : +""

if code and RDoc::CodeObject === ref and !(RDoc::TopLevel === ref)
text = "<code>#{CGI.escapeHTML text}</code>"
text = if code && RDoc::TopLevel === ref
# Allow explicit rdoc-ref links to files, but don't wrap in code tags
if rdoc_ref
text
else
# Don't auto-link file paths in backticks
return "<code>#{CGI.escapeHTML text}</code>"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If the README.md content is

README `README`

Two "README" will both trigger link("README", "README", true, false).
One from handle_regexp_CROSSREFcross_referencelink
Another from convert_tt_crossrefcross_referencelink
So if we need to detect backticks, we need to pass a new argument from convert_tt_crossref.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm surprised that README without backticks would also enter here. But I guess it's the same mechanism linking String or Array automatically.
I kinda want to disable this in a breaking change release, perhaps in v8.0. WDYT?

end
elsif code && RDoc::CodeObject === ref
"<code>#{CGI.escapeHTML text}</code>"
else
text
end

if label
Expand Down
17 changes: 17 additions & 0 deletions test/rdoc/markup/to_html_crossref_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -329,6 +329,23 @@ def test_link_class_method_full
@to.link('Parent::m', 'Parent::m')
end

def test_convert_CROSSREF_file_path_not_linked
# Must use hyperlink_all = false for file path pattern to match the right regex
@options.hyperlink_all = false
@to = RDoc::Markup::ToHtmlCrossref.new @options, 'index.html', @c1

file = @store.add_file 'lib/foo/bar.rb'
file.parser = RDoc::Parser::Ruby

# Verify the file IS resolvable via rdoc-ref (sanity check)
result = @to.convert 'rdoc-ref:lib/foo/bar.rb'
assert_equal para('<a href="lib/foo/bar_rb.html">lib/foo/bar.rb</a>'), result

# But file paths inside backticks should NOT be converted to links
result = @to.convert '+lib/foo/bar.rb+'
assert_equal para('<code>lib/foo/bar.rb</code>'), result
end

def para(text)
"\n<p>#{text}</p>\n"
end
Expand Down
Loading