FEATURE: highlight search results
This commit is contained in:
parent
d4f08f442c
commit
9c29c1c072
3 changed files with 8 additions and 13 deletions
|
@ -8,10 +8,6 @@ class Search
|
||||||
|
|
||||||
class TextHelper
|
class TextHelper
|
||||||
extend ActionView::Helpers::TextHelper
|
extend ActionView::Helpers::TextHelper
|
||||||
def self.sanitize(text)
|
|
||||||
# we run through sanitize at the end so it does not matter
|
|
||||||
text
|
|
||||||
end
|
|
||||||
end
|
end
|
||||||
|
|
||||||
attr_reader :type_filter,
|
attr_reader :type_filter,
|
||||||
|
@ -33,15 +29,14 @@ class Search
|
||||||
cooked = SearchObserver::HtmlScrubber.scrub(post.cooked).squish
|
cooked = SearchObserver::HtmlScrubber.scrub(post.cooked).squish
|
||||||
terms = @term.split(/\s+/)
|
terms = @term.split(/\s+/)
|
||||||
blurb = TextHelper.excerpt(cooked, terms.first, radius: 100)
|
blurb = TextHelper.excerpt(cooked, terms.first, radius: 100)
|
||||||
|
|
||||||
# TODO highlight term
|
|
||||||
# terms.each do |term|
|
|
||||||
# blurb = TextHelper.highlight(blurb, term)
|
|
||||||
# end
|
|
||||||
|
|
||||||
blurb = TextHelper.truncate(cooked, length: 200) if blurb.blank?
|
blurb = TextHelper.truncate(cooked, length: 200) if blurb.blank?
|
||||||
|
blurb = Sanitize.clean(blurb)
|
||||||
|
|
||||||
Sanitize.clean(blurb)
|
terms.each do |term|
|
||||||
|
blurb.gsub!(Regexp.new("(#{Regexp.escape(term)})", Regexp::IGNORECASE), "<span class='highlighted'>\\1</span>")
|
||||||
|
end
|
||||||
|
|
||||||
|
blurb
|
||||||
end
|
end
|
||||||
|
|
||||||
def add(object)
|
def add(object)
|
||||||
|
|
|
@ -145,7 +145,7 @@ describe Search do
|
||||||
p = result.posts[0]
|
p = result.posts[0]
|
||||||
p.topic.id.should == topic.id
|
p.topic.id.should == topic.id
|
||||||
p.id.should == reply.id
|
p.id.should == reply.id
|
||||||
result.blurb(p).should == "this reply has no quotes"
|
result.blurb(p).should == "this reply has no <span class='highlighted'>quotes</span>"
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
|
@ -15,7 +15,7 @@ describe SearchController do
|
||||||
response.should be_success
|
response.should be_success
|
||||||
data = JSON.parse(response.body)
|
data = JSON.parse(response.body)
|
||||||
data['posts'][0]['id'].should == my_post.id
|
data['posts'][0]['id'].should == my_post.id
|
||||||
data['posts'][0]['blurb'].should == 'this is my really awesome post'
|
data['posts'][0]['blurb'].should == 'this is my really <span class=\'highlighted\'>awesome</span> post'
|
||||||
data['topics'][0]['id'].should == my_post.topic_id
|
data['topics'][0]['id'].should == my_post.topic_id
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
Reference in a new issue