Skip to content

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

Exclude audio/video URLs from search result blurbs

Penar Musaraj580a4a827b05
diff --git a/config/locales/server.en.yml b/config/locales/server.en.yml
index d4506b8ded6..4686244be00 100644
--- a/config/locales/server.en.yml
+++ b/config/locales/server.en.yml
@@ -2165,6 +2165,8 @@ en:
topic: "Results"
user: "Users"
results_page: "Search results for '%{term}'"
+ audio: "[audio]"
+ video: "[video]"
sso:
login_error: "Login Error"
diff --git a/lib/search/grouped_search_results.rb b/lib/search/grouped_search_results.rb
index 23a605f2cfe..eb3454c9759 100644
--- a/lib/search/grouped_search_results.rb
+++ b/lib/search/grouped_search_results.rb
@@ -76,6 +76,14 @@ class Search
blurb = nil
cooked = SearchIndexer.scrub_html_for_search(cooked)
+ urls = Set.new
+ cooked.scan(URI.regexp(%w{http https})) { urls << $& }
+
+ urls.each do |url|
+ cooked.gsub!(url, I18n.t("search.video")) if url.match(/.(mov|mp4|webm|ogv)/)
+ cooked.gsub!(url, I18n.t("search.audio")) if url.match(/.(mp3|ogg|wav|m4a)/)
+ end
+
if term
terms = term.split(/\s+/)
phrase = terms.first
diff --git a/spec/lib/search_spec.rb b/spec/lib/search_spec.rb
index 5108b82f1db..e78f75faf44 100644
--- a/spec/lib/search_spec.rb
+++ b/spec/lib/search_spec.rb
@@ -15,4 +15,32 @@ describe Search do
end
end
+ context "#GroupedSearchResults.blurb_for" do
+ it "strips audio and video URLs from search blurb" do
+ cooked = <<~RAW
+ link to an external page: https://google.com/?u=bar
+
+ link to an audio file: https://somesite.com/content/file123.m4a
+
+ link to a video file: https://somesite.com/content/somethingelse.mov
+ RAW
+ result = Search::GroupedSearchResults.blurb_for(cooked)
+ expect(result).to eq("link to an external page: https://google.com/?u=bar link to an audio file: #{I18n.t("search.audio")} link to a video file: #{I18n.t("search.video")}")
+ end
+
+ it "strips URLs correctly when blurb is" do
+ cooked = <<~RAW
+ Here goes a test cooked with enough characters to hit the blurb limit.
+
+ Something is very interesting about this audio file.
+
+ http://localhost/uploads/default/original/1X/90adc0092b30c04b761541bc0322d0dce3d896e7.m4a
+ RAW
+
+ result = Search::GroupedSearchResults.blurb_for(cooked)
+ expect(result).to eq("Here goes a test cooked with enough characters to hit the blurb limit. Something is very interesting about this audio file. #{I18n.t("search.audio")}")
+ end
+
+ end
+
end