Skip to content

A platform for community discussion. Free, open, simple.

FEATURE: Enhance onebox descriptions with URL fragment/anchor context (#36536)

Ian Butterworthe230468537cc
diff --git a/lib/onebox/engine/standard_embed.rb b/lib/onebox/engine/standard_embed.rb
index 5c0c8fa45a5..5cb80740451 100644
--- a/lib/onebox/engine/standard_embed.rb
+++ b/lib/onebox/engine/standard_embed.rb
@@ -51,6 +51,7 @@ module Onebox
set_favicon_data_on_raw
set_description_on_raw
enhance_title_with_anchor
+ enhance_description_with_anchor
@raw
end
@@ -202,6 +203,57 @@ module Onebox
end
end
+ def enhance_description_with_anchor
+ return unless html_doc
+
+ fragment = extract_url_fragment
+ return if fragment.blank?
+
+ section_description = find_section_description(fragment)
+ return if section_description.blank?
+
+ cleaned_description = clean_section_description(section_description)
+ return if cleaned_description.blank?
+ return if @raw[:description].present? && @raw[:description].include?(cleaned_description)
+
+ if @raw[:description].present?
+ @raw[:description] = "#{cleaned_description} | #{@raw[:description]}"
+ else
+ @raw[:description] = cleaned_description
+ end
+ end
+
+ def find_section_description(fragment)
+ target = find_anchor_target(fragment)
+ return nil unless target
+
+ extract_description_from_target(target)
+ end
+
+ def find_anchor_target(fragment)
+ html_doc.at_xpath("//*[@id='#{fragment.gsub("'", "\\'")}']") ||
+ html_doc.at_xpath("//a[@name='#{fragment.gsub("'", "\\'")}']") ||
+ html_doc.at_css("##{CSS.escape(fragment)}")
+ end
+
+ def extract_description_from_target(target)
+ parent_article = target.ancestors("article, section, details, .docstring").first
+ search_context = parent_article || target.parent || target
+
+ paragraph = search_context.at_css("p:not(.admonition-header)")
+ return paragraph.text.strip if paragraph&.text&.strip.present?
+
+ next_p = target.at_xpath("following-sibling::p[1]") || target.at_xpath("following::p[1]")
+ return next_p.text.strip if next_p&.text&.strip.present?
+
+ nil
+ end
+
+ def clean_section_description(text)
+ cleaned = text.gsub(/\s+/, " ").strip
+ cleaned.truncate(300, separator: " ", omission: "…")
+ end
+
def enhance_title_with_anchor
return unless html_doc
return if @raw[:title].blank?
@@ -230,10 +282,7 @@ module Onebox
end
def find_section_title(fragment)
- target =
- html_doc.at_xpath("//*[@id='#{fragment.gsub("'", "\\'")}']") ||
- html_doc.at_xpath("//a[@name='#{fragment.gsub("'", "\\'")}']") ||
- html_doc.at_css("##{CSS.escape(fragment)}")
+ target = find_anchor_target(fragment)
return nil unless target
return target.text.strip if target.name =~ /^h[1-6]$/i
diff --git a/spec/lib/onebox/engine/standard_embed_spec.rb b/spec/lib/onebox/engine/standard_embed_spec.rb
index ba3f5d5f223..fa2d8e94407 100644
--- a/spec/lib/onebox/engine/standard_embed_spec.rb
+++ b/spec/lib/onebox/engine/standard_embed_spec.rb
@@ -220,6 +220,151 @@ RSpec.describe Onebox::Engine::StandardEmbed do
end
end
+ describe "#enhance_description_with_anchor" do
+ let(:anchor_class) do
+ Class.new do
+ include Onebox::Engine::StandardEmbed
+
+ attr_accessor :url, :options, :html_doc_override
+
+ def initialize(url)
+ @url = url
+ @options = {}
+ end
+
+ def html_doc
+ @html_doc_override
+ end
+ end
+ end
+
+ it "extracts description from paragraph within anchored section" do
+ html = <<~HTML
+ <html><body>
+ <article>
+ <summary id="my-function">
+ <code>my_function(x)</code>
+ </summary>
+ <div>
+ <p>Returns the sum of all elements in x.</p>
+ </div>
+ </article>
+ </body></html>
+ HTML
+ embed = anchor_class.new("https://example.com/page#my-function")
+ embed.html_doc_override = Nokogiri.HTML(html)
+ embed.instance_variable_set(:@raw, { description: "Generic page description." })
+
+ embed.send(:enhance_description_with_anchor)
+
+ expect(embed.instance_variable_get(:@raw)[:description]).to eq(
+ "Returns the sum of all elements in x. | Generic page description.",
+ )
+ end
+
+ it "extracts description from Julia docs docstring structure" do
+ html = <<~HTML
+ <html><body>
+ <article>
+ <details class="docstring">
+ <summary id="Base.pkgversion-Tuple{Module}">
+ <code>Base.pkgversion</code>
+ </summary>
+ <div>
+ <pre><code>pkgversion(m::Module)</code></pre>
+ <p>Return the version of the package that imported module m.</p>
+ </div>
+ </details>
+ </article>
+ </body></html>
+ HTML
+ embed = anchor_class.new("https://example.com/page#Base.pkgversion-Tuple%7BModule%7D")
+ embed.html_doc_override = Nokogiri.HTML(html)
+ embed.instance_variable_set(:@raw, { description: "Documentation for The Julia Language." })
+
+ embed.send(:enhance_description_with_anchor)
+
+ expect(embed.instance_variable_get(:@raw)[:description]).to eq(
+ "Return the version of the package that imported module m. | Documentation for The Julia Language.",
+ )
+ end
+
+ it "sets description when none exists" do
+ html = <<~HTML
+ <html><body>
+ <section id="getting-started">
+ <h2>Getting Started</h2>
+ <p>This guide will help you get started with our library.</p>
+ </section>
+ </body></html>
+ HTML
+ embed = anchor_class.new("https://example.com/page#getting-started")
+ embed.html_doc_override = Nokogiri.HTML(html)
+ embed.instance_variable_set(:@raw, {})
+
+ embed.send(:enhance_description_with_anchor)
+
+ expect(embed.instance_variable_get(:@raw)[:description]).to eq(
+ "This guide will help you get started with our library.",
+ )
+ end
+
+ it "does not duplicate description when section text already appears" do
+ html = <<~HTML
+ <html><body>
+ <div id="intro">
+ <p>Welcome to our documentation.</p>
+ </div>
+ </body></html>
+ HTML
+ embed = anchor_class.new("https://example.com/page#intro")
+ embed.html_doc_override = Nokogiri.HTML(html)
+ embed.instance_variable_set(:@raw, { description: "Welcome to our documentation." })
+
+ embed.send(:enhance_description_with_anchor)
+
+ expect(embed.instance_variable_get(:@raw)[:description]).to eq(
+ "Welcome to our documentation.",
+ )
+ end
+
+ it "truncates very long descriptions" do
+ long_text = "A" * 400
+ html = <<~HTML
+ <html><body>
+ <div id="section">
+ <p>#{long_text}</p>
+ </div>
+ </body></html>
+ HTML
+ embed = anchor_class.new("https://example.com/page#section")
+ embed.html_doc_override = Nokogiri.HTML(html)
+ embed.instance_variable_set(:@raw, {})
+
+ embed.send(:enhance_description_with_anchor)
+
+ expect(embed.instance_variable_get(:@raw)[:description].length).to be <= 301
+ end
+
+ it "finds paragraph following the anchor element" do
+ html = <<~HTML
+ <html><body>
+ <h2 id="features">Features</h2>
+ <p>Here are the main features of our product.</p>
+ </body></html>
+ HTML
+ embed = anchor_class.new("https://example.com/page#features")
+ embed.html_doc_override = Nokogiri.HTML(html)
+ embed.instance_variable_set(:@raw, {})
+
+ embed.send(:enhance_description_with_anchor)
+
+ expect(embed.instance_variable_get(:@raw)[:description]).to eq(
+ "Here are the main features of our product.",
+ )
+ end
+ end
+
private
def mocked_html_doc(twitter_data: nil, favicon_url: nil)