FIX: Broken GitHub folder onebox logic (#15612)

1. `html_doc.css('.Box.md')` always returns a truthy value (e.g. `[]`) so the second branch of the if-elsif never ran
2. `node&.css('text()')` was invalid code that would raise an error
3. Matching on h3 elements is no longer correct with the current html structure returned by GitHub
This commit is contained in:
Jarek Radosz 2022-01-17 18:32:07 +01:00 committed by GitHub
parent 59d78dcfcc
commit 31b27b3712
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 2406 additions and 6 deletions

View File

@ -26,14 +26,10 @@ module Onebox
if fragment
fragment = Addressable::URI.unencode(fragment)
if html_doc.css('.Box.md')
# For links to markdown docs
# For links to markdown and rdoc
if html_doc.css(".Box.md, .Box.rdoc").present?
node = html_doc.css('a.anchor').find { |n| n['href'] == "##{fragment}" }
subtitle = node&.parent&.text
elsif html_doc.css('.Box.rdoc')
# For links to rdoc docs
node = html_doc.css('h3').find { |n| n['id'] == "user-content-#{fragment.downcase}" }
subtitle = node&.css('text()')&.first&.text
end
title = "#{title} - #{subtitle}" if subtitle

File diff suppressed because one or more lines are too long

View File

@ -41,4 +41,17 @@ describe Onebox::Engine::GithubFolderOnebox do
expect(@onebox.to_html).to include("<a href=\"https://github.com/discourse/discourse#setting-up-discourse\" target=\"_blank\" rel=\"noopener\">discourse/discourse - Setting up Discourse</a>")
end
end
context 'with rdoc fragments' do
before do
@link = "https://github.com/ruby/rdoc#description-"
@uri = "https://github.com/ruby/rdoc"
stub_request(:get, @uri).to_return(status: 200, body: onebox_response("githubfolder-rdoc-root"))
@onebox = described_class.new(@link)
end
it "extracts subtitles when linking to docs" do
expect(@onebox.to_html).to include("<a href=\"https://github.com/ruby/rdoc#description-\" target=\"_blank\" rel=\"noopener\">GitHub - ruby/rdoc: RDoc produces HTML and online documentation for Ruby... - Description¶ ↑</a>")
end
end
end