File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change 22
33.main class =(config.sidebar ? ' with-sidebar' : ' without-sidebar' )
44 form .search-form action =" /_search" method =" get"
5- input .search-field type =" text" name =" q" value =" #{params[:q ]}" placeholder =" Search" autofocus =true
5+ input .search-field type =" text" name =" q" value =" #{params[:q ]}" placeholder =" Search (use quotes for exact match) " autofocus =true
66 input type =" submit" value =" Search" style =' display:none'
77
88 hr
Original file line number Diff line number Diff line change 1+ require 'shellwords'
2+
13module Madness
24 class Search
35 include ServerHelper
@@ -13,14 +15,19 @@ def index
1315
1416 def search ( query )
1517 query = query . downcase
18+ words = Shellwords . split query
19+ word_count = words . count
1620 result = { }
21+ return result if words . empty?
1722
1823 index . each do |file , content |
1924 file = file . remove ( "#{ @path } /" ) [ 0 ...-3 ]
2025 url = file_url file
2126 label = file_label file
22- next unless content . include? query
23- result [ label ] = url
27+ found = 0
28+ words . each { |word | found += 1 if content . include? word }
29+ next unless found == word_count
30+ result [ label ] = url
2431 end
2532
2633 result
Original file line number Diff line number Diff line change 3737 results = subject . search ( 'file' ) . keys
3838 expect ( results ) . to eq [ "With Sorting / File 1" , "With Sorting / File 2" , "I Do Not Belong" ]
3939 end
40+
41+ context "with a quoted query" do
42+ it "returns only pages that include the exact phrase" do
43+ expect ( subject . search ( 'the jedi' ) . count ) . to be > 1
44+ expect ( subject . search ( '"the jedi"' ) . count ) . to eq 1
45+ end
46+ end
4047 end
4148
4249end
You can’t perform that action at this time.
0 commit comments