middleman-search 0.0.1 → 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 2ff1f3b4297260b66d44ad76238814ad83ff6de0
4
- data.tar.gz: 4f904d1c14504ab484f218a9c3cc0a0eaf8f1623
3
+ metadata.gz: babd4c65f48bfaea3d9e56804730085f3da0d61d
4
+ data.tar.gz: dde7fcfe03b538ecf9ff05d2aba74afd26346f9b
5
5
  SHA512:
6
- metadata.gz: 7c5b09f58427c7afd0e35ac3390bc0dc68d23d3b009346cdca6a37b98ef75e4c19d00c74b3e754e9b2f969141e97716b617a57080aca1d2e94b05bbe74132309
7
- data.tar.gz: 59713cd375a9e56b813d7b6be60a0efeefedf1bc2ee93ec4db5f6d08b889362a54063955d688fbfed935640af1c57baf7d3bfc42bd1e4e26c11e8c8017901f21
6
+ metadata.gz: c852610b5e5e3a81842b26426196fe3c83918442d4044ff8adef2033273e1619476c8e3291a3a06299a3ef4222e625a9650a6a2ab8332f5a62e3b1a235b0010a
7
+ data.tar.gz: 0d085e9ae20acb842672f77fa8beb3ac3d7ecb840a8726ec1ec0f451b67ccb0f34127bed4089b4dabd6ecde5df87a342aab32f34d6f3f9045f2987079197379e
data/README.md CHANGED
@@ -18,29 +18,51 @@ Or install it yourself as:
18
18
 
19
19
  ## Usage
20
20
 
21
- > **Notice**: this gem is a work in progress. While it already _works_, it still has lots of configurations hardcoded. You should monkey-patch the gem if its configuration doesn't suit you, or - even better - refactor it and send a pull request :)
22
-
23
21
  You need to activate the module in your `config.rb`, telling the extension how to index your resources:
24
22
 
25
23
  ```ruby
26
24
  activate :search do
27
25
  search.resources = ['blog/', 'index.html', 'contactus/index.html']
26
+ search.index_path = 'search/lunr-index.json' # defaults to `search.json`
28
27
  search.fields = {
29
- title: {boost: 100, store: true, required: true},
30
- body: {boost: 50},
31
- url: {index: false, store: true}
28
+ title: {boost: 100, store: true, required: true},
29
+ content: {boost: 50},
30
+ url: {index: false, store: true}
32
31
  }
33
32
  end
34
33
  ```
35
34
 
36
- `resources` is a list of the beginning of the URL of the resources to index (tested with `String#start_with?`). `fields` is a hash with one entry for each field to be indexed, and a hash of options associated.
35
+ Where `resources` is a list of the beginning of the URL of the resources to index (tested with `String#start_with?`), `index_path` is the relative path of the generated index file in your site, and `fields` is a hash with one entry for each field to be indexed, with a hash of options associated:
36
+
37
+ - `boost` Specifies lunr relevance boost when searching this field
38
+ - `store` Whether to store this field in the document map (see below), defaults to false
39
+ - `index` Whether to index this field, defaults to true
40
+ - `required` The resource will not be indexed if a field marked as required has an empty or null value
41
+
42
+ Note that a special field `id` is included automatically, with an autogenerated identifier to be used as the `ref` for the document.
43
+
44
+ All fields values are retrieved from the resource `data` (ie its frontmatter), or from a `data` hash inside its `options` in `resource.metadata`, except for:
45
+ - `url` which is the actual resource url
46
+ - `content` the text extracted from the rendered resource, without including its layout
47
+
48
+ You should also `require` the `lunr.min.js` file to your `all.js` file:
49
+
50
+ ```javascript
51
+ //= require lunr.min
52
+ ```
53
+
54
+ ## Index file
55
+
56
+ The generated index file contains a JSON object with two properties:
57
+ - `index` contains the serialised lunr.js index, which you can load via `lunr.Index.load(lunrData.index)`
58
+ - `docs` is a map from the autogenerated document ids to an object that contains the attributes configured for storage
37
59
 
38
- For each field, you can specify it's relevance `boost`, and whether to `index` that field's content or not.
60
+ You will typically load the `index` into a lunr index instance, and then use the `docs` map to look up the returned value and present it to the user.
39
61
 
40
62
  ## Acknowledgments
41
63
 
42
64
  A big thank you to:
43
- - @Octo-Labs's @jagthedrummer for his [`middleman-alias`](https://github.com/Octo-Labs/middleman-alias) extension, in which we based for developing this one.
44
- - @jnovos and @256dpi, for their [`middleman-lunrjs`](https://github.com/jnovos/middleman-lunrjs) and [`middleman-lunr`](https://github.com/256dpi/middleman-lunr) extensions, which served as inspirations for making this one.
45
- - @olivernn and all [`lunr.js`](http://lunrjs.com/) [contributors](https://github.com/olivernn/lunr.js/graphs/contributors)
65
+ - [Octo-Labs](https://github.com/Octo-Labs)'s [jagthedrummer](https://github.com/jagthedrummer) for his [`middleman-alias`](https://github.com/Octo-Labs/middleman-alias) extension, in which we based for developing this one.
66
+ - [jnovos](https://github.com/jnovos) and [256dpi](https://github.com/256dpi), for their [`middleman-lunrjs`](https://github.com/jnovos/middleman-lunrjs) and [`middleman-lunr`](https://github.com/256dpi/middleman-lunr) extensions, which served as inspirations for making this one.
67
+ - [olivernn](https://github.com/olivernn) and all [`lunr.js`](http://lunrjs.com/) [contributors](https://github.com/olivernn/lunr.js/graphs/contributors)
46
68
  - [The Middleman](https://middlemanapp.com/) [team](https://github.com/orgs/middleman/people) and [contributors](https://github.com/middleman/middleman/graphs/contributors)
@@ -5,10 +5,10 @@ module Middleman
5
5
  class SearchExtension < Middleman::Extension
6
6
  option :resources, [], 'Paths of resources to index'
7
7
  option :fields, {}, 'Fields to index, with their options'
8
+ option :index_path, 'search.json', 'Index file path'
8
9
 
9
10
  def manipulate_resource_list(resources)
10
- index_file_path = 'index.json'
11
- resources.push Middleman::Sitemap::SearchIndexResource.new(@app.sitemap, index_file_path, @options)
11
+ resources.push Middleman::Sitemap::SearchIndexResource.new(@app.sitemap, @options[:index_path], @options)
12
12
  resources
13
13
  end
14
14
  end
@@ -16,15 +16,13 @@ module Middleman
16
16
  end
17
17
 
18
18
  def render
19
- # FIXME: add lunr.min.js as a resource
20
- lunr_source = File.expand_path('../../../vendor/lunr.min.js', __FILE__)
19
+ # Build js context
20
+ context = V8::Context.new
21
+ context.load(File.expand_path('../../../vendor/assets/javascripts/lunr.min.js', __FILE__))
22
+ context.eval('lunr.Index.prototype.indexJson = function () {return JSON.stringify(this);}')
21
23
 
22
- cxt = V8::Context.new
23
- cxt.load(lunr_source)
24
- # add new method to lunr index
25
- cxt.eval('lunr.Index.prototype.indexJson = function () {return JSON.stringify(this);}')
26
- #Get the lunjs object
27
- val = cxt.eval('lunr')
24
+ # Build lunr based on config
25
+ lunr = context.eval('lunr')
28
26
  lunr_conf = proc do |this|
29
27
  this.ref('id')
30
28
  @fields.each do |field, opts|
@@ -33,29 +31,35 @@ module Middleman
33
31
  end
34
32
  end
35
33
 
36
- idx = val.call(lunr_conf)
37
- map = Hash.new
34
+ # Get lunr index
35
+ index = lunr.call(lunr_conf)
38
36
 
39
- @app.sitemap.resources.each_with_index do |resource, index|
40
- next if resource.data['index'] == false
41
- next unless @resources_to_index.any? {|whitelisted| resource.path.start_with? whitelisted }
37
+ # Ref to resource map
38
+ store = Hash.new
42
39
 
43
- # FIXME: Use config to determine required fields
44
- title = resource.data.title || resource.metadata[:options][:title] rescue nil
45
- next if title.blank?
40
+ # Iterate over all resources and build index
41
+ @app.sitemap.resources.each_with_index do |resource, id|
42
+ catch(:required) do
43
+ next if resource.data['index'] == false
44
+ next unless @resources_to_index.any? {|whitelisted| resource.path.start_with? whitelisted }
46
45
 
47
- content = resource.render(layout: false)
48
- text = Nokogiri::HTML(content).xpath("//text()").text
49
- url = resource.url
50
- description = resource.data.description || resource.metadata[:options][:description] rescue nil
51
- tags = Array(resource.data.tags || []).join(", ")
46
+ to_index = Hash.new
47
+ to_store = Hash.new
52
48
 
53
- # FIXME: Use config to determine what fields are indexed and stored
54
- idx.add({title: title, body: text, description: description, tags: tags, id: index})
55
- map[index] = { title: title, url: url }
49
+ @fields.each do |field, opts|
50
+ value = value_for(resource, field, opts)
51
+ throw(:required) if value.blank? && opts[:required]
52
+ to_index[field] = value unless opts[:index] == false
53
+ to_store[field] = value if opts[:store]
54
+ end
55
+
56
+ index.add(to_index.merge(id: id))
57
+ store[id] = to_store
58
+ end
56
59
  end
57
60
 
58
- "{\"index\": #{idx.indexJson()}, \"map\": #{map.to_json}}"
61
+ # Generate JSON output
62
+ "{\"index\": #{index.indexJson()}, \"docs\": #{store.to_json}}"
59
63
  end
60
64
 
61
65
  def binary?
@@ -65,6 +69,18 @@ module Middleman
65
69
  def ignored?
66
70
  false
67
71
  end
72
+
73
+ def value_for(resource, field, opts={})
74
+ case field.to_s
75
+ when 'content'
76
+ Nokogiri::HTML(resource.render(layout: false)).xpath("//text()").text
77
+ when 'url'
78
+ resource.url
79
+ else
80
+ value = resource.data.send(field) || resource.metadata.fetch(:options, {}).fetch(:data, {}).fetch(field, nil)
81
+ value ? Array(value).compact.join(" ") : nil
82
+ end
83
+ end
68
84
  end
69
85
  end
70
86
  end
@@ -1,3 +1,3 @@
1
1
  module MiddlemanSearch
2
- VERSION = "0.0.1"
2
+ VERSION = "0.1.0"
3
3
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: middleman-search
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
4
+ version: 0.1.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Matías García Isaía
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2015-10-17 00:00:00.000000000 Z
12
+ date: 2015-10-20 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: middleman-core
@@ -100,7 +100,7 @@ files:
100
100
  - lib/middleman-search/version.rb
101
101
  - lib/middleman_extension.rb
102
102
  - middleman-search.gemspec
103
- - vendor/lunr.min.js
103
+ - vendor/assets/javascripts/lunr.min.js
104
104
  homepage: http://github.com/manastech/middleman-search
105
105
  licenses:
106
106
  - MIT