Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update to Lunr 2.0.3 #29

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
54 changes: 27 additions & 27 deletions lib/middleman-search/search-index-resource.rb
Original file line number Diff line number Diff line change
Expand Up @@ -45,11 +45,15 @@ def build_index

# Register pipeline functions
pipeline = context.eval('lunr.Pipeline')

@pipeline.each do |name, function|
context[name] = context.eval("(#{function})")
pipeline.registerFunction(context[name], name)
end

# Reference to resource map
store = Hash.new

# Build lunr based on config
lunr = context.eval('lunr')
lunr_conf = proc do |this|
Expand All @@ -68,41 +72,38 @@ def build_index
next if opts[:index] == false
this.field(field, {:boost => opts[:boost]})
end
end

# Get lunr index
index = lunr.call(lunr_conf)
# Iterate over all resources and build index
@app.sitemap.resources.each_with_index do |resource, id|
begin
catch(:skip) do
next if resource.data['index'] == false
next unless @resources_to_index.any? {|whitelisted| resource.path.start_with? whitelisted }

# Ref to resource map
store = Hash.new
to_index = Hash.new
to_store = Hash.new

# Iterate over all resources and build index
@app.sitemap.resources.each_with_index do |resource, id|
begin
catch(:skip) do
next if resource.data['index'] == false
next unless @resources_to_index.any? {|whitelisted| resource.path.start_with? whitelisted }

to_index = Hash.new
to_store = Hash.new

@fields.each do |field, opts|
value = value_for(resource, field, opts)
throw(:skip) if value.blank? && opts[:required]
to_index[field] = value unless opts[:index] == false
to_store[field] = value if opts[:store]
end
@fields.each do |field, opts|
value = value_for(resource, field, opts)
throw(:skip) if value.blank? && opts[:required]
to_index[field] = value unless opts[:index] == false
to_store[field] = value if opts[:store]
end

@callback.call(to_index, to_store, resource) if @callback
@callback.call(to_index, to_store, resource) if @callback

index.add(to_index.merge(id: id))
store[id] = to_store
this.add(to_index.merge(id: id))
store[id] = to_store
end
rescue => ex
@app.logger.warn "Error processing resource for index: #{resource.path}\n#{ex}\n #{ex.backtrace.join("\n ")}"
end
rescue => ex
@app.logger.warn "Error processing resource for index: #{resource.path}\n#{ex}\n #{ex.backtrace.join("\n ")}"
end
end

# Get lunr index
index = lunr.call(lunr_conf)

# Generate JSON output
"{\"index\": #{index.indexJson()}, \"docs\": #{store.to_json}}"
end
Expand All @@ -118,7 +119,6 @@ def ignored?
def value_for(resource, field, opts={})
case field.to_s
when 'content'

html = resource.render( { :layout => false }, { :current_path => resource.path } )
Nokogiri::HTML(html).xpath("//text()").text
when 'url'
Expand Down
Loading