Skip to content

Commit

Permalink
Merge branch 'daniel-rikowski:master'
Browse files Browse the repository at this point in the history
Pull in ExecJS based approach from this pull request:

manastech#24
  • Loading branch information
heathd committed Nov 7, 2017
2 parents ed9e525 + 3565770 commit 19df757
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 30 deletions.
57 changes: 28 additions & 29 deletions lib/middleman-search/search-index-resource.rb
Original file line number Diff line number Diff line change
Expand Up @@ -32,46 +32,42 @@ def render(opts={}, locs={})

def build_index
# Build js context
context = V8::Context.new
context.load(lunr_resource('lunr.js'))

libs = []
libs << lunr_resource('lunr.js')
if @language != 'en' # English is the default
context.load(lunr_resource("lunr.stemmer.support.js"))
context.load(lunr_resource("lunr.#{@language}.js"))
lunr_lang = context.eval("lunr.#{@language}")
libs << lunr_resource("lunr.stemmer.support.js")
libs << lunr_resource("lunr.#{@language}.js")
end

context.eval('lunr.Index.prototype.indexJson = function () {return JSON.stringify(this.toJSON());}')
source = libs.map { |lib| File.read(lib, mode: "rb:UTF-8") }
source << "lunr.Index.prototype.indexJson = function () {return JSON.stringify(this.toJSON());};"

# Register pipeline functions
pipeline = context.eval('lunr.Pipeline')
@pipeline.each do |name, function|
context[name] = context.eval("(#{function})")
pipeline.registerFunction(context[name], name)
source << "lunr.Pipeline.registerFunction((#{function}), '#{name}');"
end

# Build lunr based on config
lunr = context.eval('lunr')
lunr_conf = proc do |this|
source << "lunr.middlemanSearchIndex = lunr(function () {"

# Use autogenerated id field as reference
this.ref('id')
# Use autogenerated id field as reference
source << "this.ref('id');"

# Add functions to pipeline (just registering them isn't enough)
@pipeline.each do |name, function|
this.pipeline.add(context[name])
end
# Add functions to pipeline (just registering them isn't enough)
@pipeline.each do |name, function|
source << "this.pipeline.add(lunr.Pipeline.registeredFunctions.#{name});"
end

# Define fields with boost
this.use(lunr_lang) if @language != 'en'
@fields.each do |field, opts|
next if opts[:index] == false
this.field(field, {:boost => opts[:boost]})
end
# Use language if set
source << "this.use(lunr.#{@language});" if @language != 'en'

# Define fields with boost
@fields.each do |field, opts|
next if opts[:index] == false
source << "this.field('#{field}', { boost: #{opts[:boost]}});"
end

# Get lunr index
index = lunr.call(lunr_conf)
source << "});"


# Ref to resource map
store = Hash.new
Expand All @@ -95,7 +91,8 @@ def build_index

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

index.add(to_index.merge(id: id))
source << "lunr.middlemanSearchIndex.add(#{to_index.merge(id: id).to_json});"

store[id] = to_store
end
rescue => ex
Expand All @@ -104,7 +101,9 @@ def build_index
end

# Generate JSON output
"{\"index\": #{index.indexJson()}, \"docs\": #{store.to_json}}"
context = ExecJS.compile(source.join("\n"))
json = context.eval('lunr.middlemanSearchIndex.indexJson()')
"{\"index\": #{json}, \"docs\": #{store.to_json}}"
end

def binary?
Expand Down
2 changes: 1 addition & 1 deletion middleman-search.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ Gem::Specification.new do |spec|
spec.require_paths = ["lib"]

spec.add_dependency "middleman-core", [">= 3.2"]
spec.add_dependency "therubyracer", ["~> 0.12.2"]
spec.add_dependency "execjs", ["~> 2.6"]
spec.add_dependency "nokogiri", ["~> 1.6"]

spec.add_development_dependency "bundler", "~> 1.5"
Expand Down

0 comments on commit 19df757

Please sign in to comment.