Skip to content

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

FEATURE: Enhance onebox titles with URL fragment/anchor context (#36515)

Ian Butterworth8b76491673bf
diff --git a/lib/onebox/engine/standard_embed.rb b/lib/onebox/engine/standard_embed.rb
index 15dbea87fda..5c0c8fa45a5 100644
--- a/lib/onebox/engine/standard_embed.rb
+++ b/lib/onebox/engine/standard_embed.rb
@@ -50,6 +50,7 @@ module Onebox
set_json_ld_data_on_raw
set_favicon_data_on_raw
set_description_on_raw
+ enhance_title_with_anchor
@raw
end
@@ -200,6 +201,68 @@ module Onebox
@raw[:description] = description if description.present?
end
end
+
+ def enhance_title_with_anchor
+ return unless html_doc
+ return if @raw[:title].blank?
+
+ fragment = extract_url_fragment
+ return if fragment.blank?
+
+ section_title = find_section_title(fragment)
+ return if section_title.blank?
+
+ cleaned_title = clean_section_title(section_title)
+ return if cleaned_title.blank?
+ return if @raw[:title].include?(cleaned_title)
+
+ @raw[:title] = "#{cleaned_title} - #{@raw[:title]}"
+ end
+
+ def extract_url_fragment
+ uri = URI.parse(url)
+ fragment = uri.fragment
+ return nil if fragment.blank?
+
+ CGI.unescape(fragment)
+ rescue URI::InvalidURIError
+ nil
+ 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)}")
+ return nil unless target
+
+ return target.text.strip if target.name =~ /^h[1-6]$/i
+
+ code_content = target.at_css("code, .docstring-binding")&.text&.strip
+ return code_content if code_content.present?
+
+ heading = target.at_css("h1, h2, h3, h4, h5, h6")
+ return heading.text.strip if heading
+
+ find_nearest_heading(target)
+ end
+
+ def find_nearest_heading(element)
+ current = element
+ while current
+ current.previous_element&.tap do |prev|
+ return prev.text.strip if prev.name =~ /^h[1-6]$/i
+ end
+ current = current.parent
+ return current.text.strip if current&.name =~ /^h[1-6]$/i
+ end
+ nil
+ end
+
+ def clean_section_title(text)
+ cleaned = text.gsub(/[\u00B6\u00A7#]/, "").gsub(/\s+/, " ").strip
+ cleaned.truncate(80, separator: " ")
+ end
end
end
end
diff --git a/spec/lib/onebox/engine/standard_embed_spec.rb b/spec/lib/onebox/engine/standard_embed_spec.rb
index 55cffc4a9be..ba3f5d5f223 100644
--- a/spec/lib/onebox/engine/standard_embed_spec.rb
+++ b/spec/lib/onebox/engine/standard_embed_spec.rb
@@ -110,6 +110,116 @@ RSpec.describe Onebox::Engine::StandardEmbed do
end
end
+ describe "#enhance_title_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 fragment from URL" do
+ embed = anchor_class.new("https://example.com/page#my-section")
+ expect(embed.send(:extract_url_fragment)).to eq("my-section")
+ end
+
+ it "URL-decodes the fragment" do
+ embed = anchor_class.new("https://example.com/page#Base.pkgversion-Tuple%7BModule%7D")
+ expect(embed.send(:extract_url_fragment)).to eq("Base.pkgversion-Tuple{Module}")
+ end
+
+ it "returns nil for URLs without fragments" do
+ embed = anchor_class.new("https://example.com/page")
+ expect(embed.send(:extract_url_fragment)).to be_nil
+ end
+
+ it "finds section title from heading element with matching ID" do
+ html = <<~HTML
+ <html><body>
+ <h2 id="my-section">My Section Title</h2>
+ <p>Content here</p>
+ </body></html>
+ HTML
+ embed = anchor_class.new("https://example.com/page#my-section")
+ embed.html_doc_override = Nokogiri.HTML(html)
+
+ expect(embed.send(:find_section_title, "my-section")).to eq("My Section Title")
+ end
+
+ it "finds section title from code element within target" do
+ html = <<~HTML
+ <html><body>
+ <div id="Base.pkgversion-Tuple{Module}">
+ <code>pkgversion(m::Module)</code>
+ <p>Returns the version of the package.</p>
+ </div>
+ </body></html>
+ HTML
+ embed = anchor_class.new("https://example.com/page#Base.pkgversion-Tuple{Module}")
+ embed.html_doc_override = Nokogiri.HTML(html)
+
+ expect(embed.send(:find_section_title, "Base.pkgversion-Tuple{Module}")).to eq(
+ "pkgversion(m::Module)",
+ )
+ end
+
+ it "does not duplicate title when section title already appears in page title" do
+ html = <<~HTML
+ <html><body>
+ <h2 id="installation">Installation</h2>
+ </body></html>
+ HTML
+ embed = anchor_class.new("https://example.com/page#installation")
+ embed.html_doc_override = Nokogiri.HTML(html)
+ embed.instance_variable_set(:@raw, { title: "Installation Guide - My Project" })
+
+ embed.send(:enhance_title_with_anchor)
+
+ expect(embed.instance_variable_get(:@raw)[:title]).to eq("Installation Guide - My Project")
+ end
+
+ it "prepends section title to page title" do
+ html = <<~HTML
+ <html><body>
+ <h2 id="getting-started">Getting Started</h2>
+ </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, { title: "My Project Documentation" })
+
+ embed.send(:enhance_title_with_anchor)
+
+ expect(embed.instance_variable_get(:@raw)[:title]).to eq(
+ "Getting Started - My Project Documentation",
+ )
+ end
+
+ it "cleans anchor symbols from section titles" do
+ embed = anchor_class.new("https://example.com/page#test")
+ expect(embed.send(:clean_section_title, "My Section ¶")).to eq("My Section")
+ expect(embed.send(:clean_section_title, "Another § Section")).to eq("Another Section")
+ expect(embed.send(:clean_section_title, "Title #")).to eq("Title")
+ end
+
+ it "truncates long section titles" do
+ embed = anchor_class.new("https://example.com/page#test")
+ long_title = "A" * 100
+ result = embed.send(:clean_section_title, long_title)
+ expect(result.length).to be <= 80
+ end
+ end
+
private
def mocked_html_doc(twitter_data: nil, favicon_url: nil)