Skip to content
This repository was archived by the owner on Sep 18, 2019. It is now read-only.

Commit 4eae035

Browse files
authored
Merge pull request #126 from tonyroberts/execjs
Use ExecJS instead of therubyracer
2 parents 78bf559 + 842f0b4 commit 4eae035

File tree

5 files changed

+44
-63
lines changed

5 files changed

+44
-63
lines changed

build/jekyll_lunr_js_search.rb

Lines changed: 22 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,15 @@
22
require 'net/http'
33
require 'json'
44
require 'uri'
5-
require 'v8'
5+
require 'execjs'
66

77
module Jekyll
88
module LunrJsSearch
99
class Indexer < Jekyll::Generator
1010
def initialize(config = {})
1111
super(config)
1212

13-
lunr_config = {
13+
@lunr_config = {
1414
'excludes' => [],
1515
'strip_index_html' => false,
1616
'min_length' => 3,
@@ -24,30 +24,23 @@ def initialize(config = {})
2424
'js_dir' => 'js'
2525
}.merge!(config['lunr_search'] || {})
2626

27-
@js_dir = lunr_config['js_dir']
27+
@js_dir = @lunr_config['js_dir']
2828
gem_lunr = File.join(File.dirname(__FILE__), "../../build/lunr.min.js")
2929
@lunr_path = File.exist?(gem_lunr) ? gem_lunr : File.join(@js_dir, File.basename(gem_lunr))
3030
raise "Could not find #{@lunr_path}" if !File.exist?(@lunr_path)
3131

32-
ctx = V8::Context.new
33-
ctx.load(@lunr_path)
34-
ctx['indexer'] = proc do |this|
35-
this.ref('id')
36-
lunr_config['fields'].each_pair do |name, boost|
37-
this.field(name, { 'boost' => boost })
38-
end
39-
end
40-
@index = ctx.eval('lunr(indexer)')
32+
lunr_src = open(@lunr_path).read
33+
ctx = ExecJS.compile(lunr_src)
4134
@lunr_version = ctx.eval('lunr.version')
4235
@docs = {}
43-
@excludes = lunr_config['excludes']
36+
@excludes = @lunr_config['excludes']
4437

4538
# if web host supports index.html as default doc, then optionally exclude it from the url
46-
@strip_index_html = lunr_config['strip_index_html']
39+
@strip_index_html = @lunr_config['strip_index_html']
4740

4841
# stop word exclusion configuration
49-
@min_length = lunr_config['min_length']
50-
@stopwords_file = lunr_config['stopwords']
42+
@min_length = @lunr_config['min_length']
43+
@stopwords_file = @lunr_config['stopwords']
5144
end
5245

5346
# Index all pages except pages matching any value in config['lunr_excludes'] or with date['exclude_from_search']
@@ -61,6 +54,13 @@ def generate(site)
6154
content_renderer = PageRenderer.new(site)
6255
index = []
6356

57+
index_js = open(@lunr_path).read
58+
index_js << 'var idx = lunr(function() {});'
59+
index_js << 'idx.ref(\'id\');';
60+
@lunr_config['fields'].each_pair do |name, boost|
61+
index_js << "idx.field('#{name}', {'boost': #{boost}});"
62+
end
63+
6464
items.each_with_index do |item, i|
6565
entry = SearchEntry.create(item, content_renderer)
6666

@@ -78,7 +78,7 @@ def generate(site)
7878
"body" => entry.body
7979
}
8080

81-
@index.add(doc)
81+
index_js << 'idx.add(' << ::JSON.generate(doc, quirks_mode: true) << ');'
8282
doc.delete("body")
8383
@docs[i] = doc
8484

@@ -88,9 +88,12 @@ def generate(site)
8888
FileUtils.mkdir_p(File.join(site.dest, @js_dir))
8989
filename = File.join(@js_dir, 'index.json')
9090

91+
ctx = ExecJS.compile(index_js)
92+
index = ctx.eval('JSON.stringify(idx)')
93+
9194
total = {
9295
"docs" => @docs,
93-
"index" => @index.to_hash
96+
"index" => ::JSON.parse(index)
9497
}
9598

9699
filepath = File.join(site.dest, filename)
@@ -145,18 +148,6 @@ def pages_to_index(site)
145148
end
146149
end
147150
end
148-
require "v8"
149-
require "json"
150-
151-
class V8::Object
152-
def to_json
153-
@context['JSON']['stringify'].call(self)
154-
end
155-
156-
def to_hash
157-
JSON.parse(to_json, :max_nesting => false)
158-
end
159-
end
160151
require 'nokogiri'
161152

162153
module Jekyll
@@ -170,7 +161,7 @@ def initialize(site)
170161
def prepare(item)
171162
layout = item.data["layout"]
172163
begin
173-
item.data.delete("layout")
164+
item.data["layout"] = nil
174165

175166
if item.is_a?(Jekyll::Document)
176167
output = Jekyll::Renderer.new(@site, item).run

jekyll-lunr-js-search.gemspec

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ Gem::Specification.new do |s|
1616

1717
s.add_runtime_dependency 'nokogiri', '~> 1.7'
1818
s.add_runtime_dependency 'json', '~> 2.0'
19-
s.add_runtime_dependency 'therubyracer', '~> 0.12'
19+
s.add_runtime_dependency 'execjs', '~> 2.7'
2020

2121
s.add_development_dependency 'rake', '~> 12.0'
2222
s.add_development_dependency 'uglifier', '~> 3.0'

lib/jekyll-lunr-js-search.rb

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
require 'jekyll_lunr_js_search/version'
22
require 'jekyll_lunr_js_search/indexer'
3-
require 'jekyll_lunr_js_search/javascript'
43
require 'jekyll_lunr_js_search/page_renderer'
54
require 'jekyll_lunr_js_search/search_entry'
65
require 'jekyll_lunr_js_search/search_index_file'

lib/jekyll_lunr_js_search/indexer.rb

Lines changed: 21 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,15 @@
22
require 'net/http'
33
require 'json'
44
require 'uri'
5-
require 'v8'
5+
require 'execjs'
66

77
module Jekyll
88
module LunrJsSearch
99
class Indexer < Jekyll::Generator
1010
def initialize(config = {})
1111
super(config)
1212

13-
lunr_config = {
13+
@lunr_config = {
1414
'excludes' => [],
1515
'strip_index_html' => false,
1616
'min_length' => 3,
@@ -24,30 +24,23 @@ def initialize(config = {})
2424
'js_dir' => 'js'
2525
}.merge!(config['lunr_search'] || {})
2626

27-
@js_dir = lunr_config['js_dir']
27+
@js_dir = @lunr_config['js_dir']
2828
gem_lunr = File.join(File.dirname(__FILE__), "../../build/lunr.min.js")
2929
@lunr_path = File.exist?(gem_lunr) ? gem_lunr : File.join(@js_dir, File.basename(gem_lunr))
3030
raise "Could not find #{@lunr_path}" if !File.exist?(@lunr_path)
3131

32-
ctx = V8::Context.new
33-
ctx.load(@lunr_path)
34-
ctx['indexer'] = proc do |this|
35-
this.ref('id')
36-
lunr_config['fields'].each_pair do |name, boost|
37-
this.field(name, { 'boost' => boost })
38-
end
39-
end
40-
@index = ctx.eval('lunr(indexer)')
32+
lunr_src = open(@lunr_path).read
33+
ctx = ExecJS.compile(lunr_src)
4134
@lunr_version = ctx.eval('lunr.version')
4235
@docs = {}
43-
@excludes = lunr_config['excludes']
36+
@excludes = @lunr_config['excludes']
4437

4538
# if web host supports index.html as default doc, then optionally exclude it from the url
46-
@strip_index_html = lunr_config['strip_index_html']
39+
@strip_index_html = @lunr_config['strip_index_html']
4740

4841
# stop word exclusion configuration
49-
@min_length = lunr_config['min_length']
50-
@stopwords_file = lunr_config['stopwords']
42+
@min_length = @lunr_config['min_length']
43+
@stopwords_file = @lunr_config['stopwords']
5144
end
5245

5346
# Index all pages except pages matching any value in config['lunr_excludes'] or with date['exclude_from_search']
@@ -61,6 +54,13 @@ def generate(site)
6154
content_renderer = PageRenderer.new(site)
6255
index = []
6356

57+
index_js = open(@lunr_path).read
58+
index_js << 'var idx = lunr(function() {});'
59+
index_js << 'idx.ref(\'id\');';
60+
@lunr_config['fields'].each_pair do |name, boost|
61+
index_js << "idx.field('#{name}', {'boost': #{boost}});"
62+
end
63+
6464
items.each_with_index do |item, i|
6565
entry = SearchEntry.create(item, content_renderer)
6666

@@ -78,7 +78,7 @@ def generate(site)
7878
"body" => entry.body
7979
}
8080

81-
@index.add(doc)
81+
index_js << 'idx.add(' << ::JSON.generate(doc, quirks_mode: true) << ');'
8282
doc.delete("body")
8383
@docs[i] = doc
8484

@@ -88,9 +88,12 @@ def generate(site)
8888
FileUtils.mkdir_p(File.join(site.dest, @js_dir))
8989
filename = File.join(@js_dir, 'index.json')
9090

91+
ctx = ExecJS.compile(index_js)
92+
index = ctx.eval('JSON.stringify(idx)')
93+
9194
total = {
9295
"docs" => @docs,
93-
"index" => @index.to_hash
96+
"index" => ::JSON.parse(index)
9497
}
9598

9699
filepath = File.join(site.dest, filename)

lib/jekyll_lunr_js_search/javascript.rb

Lines changed: 0 additions & 12 deletions
This file was deleted.

0 commit comments

Comments
 (0)