Skip to content

Commit 3d94ce8

Browse files
committed
Avoid linking file paths inside backticks
1 parent a28af21 commit 3d94ce8

File tree

2 files changed

+30
-3
lines changed

2 files changed

+30
-3
lines changed

lib/rdoc/markup/to_html_crossref.rb

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -156,16 +156,26 @@ def link(name, text, code = true, rdoc_ref: false)
156156
ref = @cross_reference.resolve name, text if name
157157

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

167-
if code and RDoc::CodeObject === ref and !(RDoc::TopLevel === ref)
168-
text = "<code>#{CGI.escapeHTML text}</code>"
167+
text = if code && RDoc::TopLevel === ref
168+
# Allow explicit rdoc-ref links to files, but don't wrap in code tags
169+
if rdoc_ref
170+
text
171+
else
172+
# Don't auto-link file paths in backticks
173+
return "<code>#{CGI.escapeHTML text}</code>"
174+
end
175+
elsif code && RDoc::CodeObject === ref
176+
"<code>#{CGI.escapeHTML text}</code>"
177+
else
178+
text
169179
end
170180

171181
if label

test/rdoc/markup/to_html_crossref_test.rb

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -329,6 +329,23 @@ def test_link_class_method_full
329329
@to.link('Parent::m', 'Parent::m')
330330
end
331331

332+
def test_convert_CROSSREF_file_path_not_linked
333+
# Must use hyperlink_all = false for file path pattern to match the right regex
334+
@options.hyperlink_all = false
335+
@to = RDoc::Markup::ToHtmlCrossref.new @options, 'index.html', @c1
336+
337+
file = @store.add_file 'lib/foo/bar.rb'
338+
file.parser = RDoc::Parser::Ruby
339+
340+
# Verify the file IS resolvable via rdoc-ref (sanity check)
341+
result = @to.convert 'rdoc-ref:lib/foo/bar.rb'
342+
assert_equal para('<a href="lib/foo/bar_rb.html">lib/foo/bar.rb</a>'), result
343+
344+
# But file paths inside backticks should NOT be converted to links
345+
result = @to.convert '+lib/foo/bar.rb+'
346+
assert_equal para('<code>lib/foo/bar.rb</code>'), result
347+
end
348+
332349
def para(text)
333350
"\n<p>#{text}</p>\n"
334351
end

0 commit comments

Comments
 (0)