Exclude audio/video URLs from search result blurbs

Displays translatable "[audio]" or "[video]" placeholders instead of ugly (and often long) URLs.
This commit is contained in:
Penar Musaraj 2019-10-30 13:07:16 -04:00
parent 965662d215
commit 580a4a827b
3 changed files with 38 additions and 0 deletions

View File

@ -2165,6 +2165,8 @@ en:
topic: "Results"
user: "Users"
results_page: "Search results for '%{term}'"
audio: "[audio]"
video: "[video]"
sso:
login_error: "Login Error"

View File

@ -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

View File

@ -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