From 1e992d9193647a3bdff913fe0bc12feff288826d Mon Sep 17 00:00:00 2001 From: Bianca Nenciu Date: Fri, 18 Jun 2021 18:55:24 +0300 Subject: [PATCH] FIX: Do not check for duplicate links in Onebox (#13345) If a user posted a URL that appeared inside a Onebox, then the user got a duplicate link notice. This was fixed by skipping those links in Ruby. If a user posted a URL that was Oneboxes and contained other links that appeared in previous posts, then the user got a duplicate link notice. This was fixed by skipping those links in JavaScript. --- .../discourse/app/controllers/composer.js | 10 +++++++++- lib/pretty_text.rb | 7 +++++-- spec/components/pretty_text_spec.rb | 16 ++++++++++++++++ 3 files changed, 30 insertions(+), 3 deletions(-) diff --git a/app/assets/javascripts/discourse/app/controllers/composer.js b/app/assets/javascripts/discourse/app/controllers/composer.js index 0c4be8430c4..12c76c2547a 100644 --- a/app/assets/javascripts/discourse/app/controllers/composer.js +++ b/app/assets/javascripts/discourse/app/controllers/composer.js @@ -456,7 +456,7 @@ export default Controller.extend({ $links.each((idx, l) => { const href = l.href; if (href && href.length) { - // skip links in quotes + // skip links in quotes and oneboxes for (let element = l; element; element = element.parentElement) { if ( element.tagName === "DIV" && @@ -471,6 +471,14 @@ export default Controller.extend({ ) { return true; } + + if ( + element.tagName === "ASIDE" && + element.classList.contains("onebox") && + href !== element.dataset["onebox-src"] + ) { + return true; + } } const [warn, info] = linkLookup.check(post, href); diff --git a/lib/pretty_text.rb b/lib/pretty_text.rb index cb897760c3c..a298ce4f80a 100644 --- a/lib/pretty_text.rb +++ b/lib/pretty_text.rb @@ -324,8 +324,11 @@ module PrettyText links = [] doc = Nokogiri::HTML5.fragment(html) - # remove href inside quotes & elided part - doc.css("aside.quote a, .elided a").each { |a| a["href"] = "" } + # extract onebox links + doc.css("aside.onebox[data-onebox-src]").each { |onebox| links << DetectedLink.new(onebox["data-onebox-src"], false) } + + # remove href inside quotes & oneboxes & elided part + doc.css("aside.quote a, aside.onebox a, .elided a").remove # extract all links doc.css("a").each do |a| diff --git a/spec/components/pretty_text_spec.rb b/spec/components/pretty_text_spec.rb index 3d31c1ae1e3..c2e5a611a54 100644 --- a/spec/components/pretty_text_spec.rb +++ b/spec/components/pretty_text_spec.rb @@ -779,6 +779,22 @@ describe PrettyText do ].sort) end + it "should not extract links inside oneboxes" do + onebox = <<~EOF + + EOF + + expect(PrettyText.extract_links(onebox).map(&:url)).to contain_exactly("https://twitter.com/EDBPostgres/status/1402528437441634306") + end + it "should not preserve tags in code blocks" do expect(PrettyText.excerpt("
<h3>Hours</h3>
", 100)).to eq("<h3>Hours</h3>") end