algoliasearch-jekyll 0.4.3 → 0.5.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: bf63cc151be2bbb9c5dea8881f110703cc5ab95f
4
- data.tar.gz: 9cdd47f72d3446b48df5492b1a68f405e7607ffb
3
+ metadata.gz: be7a946ef88f83e9dd443b3bb1cf0192b9809366
4
+ data.tar.gz: 613b7ae7df5b1ea5d23393225c9bf9953cddcfd7
5
5
  SHA512:
6
- metadata.gz: 01fc966406a3e1f7866e7dd045182cd0c343b44f0f64201026b13401fb0544a992f3202c9504481f44eca7ce4f20b517f9afc4e34c5e6662f2f8561ffa98765f
7
- data.tar.gz: b92a131ce8b29c1b691669f01881d9853446cdc103dffa88fc6c777eb568a363b951d5b4cafc4b49187df0a8c24a9a3f6ec2d7f6a6b7faf8929ff2f1133bbf04
6
+ metadata.gz: 94285229cd205e237c10562c8c15060f69d7986d2074e6267192099aba31d033d31459f36a35f1b699d6b2c6504f7be74474af5ed387106975d9ad17fa6d1a9e
7
+ data.tar.gz: 4aefd0b4d45a2d28b4e7ed4b8f56af28c2296cc3ce423894b854b4f97a1ef05e05db11f951c6e23384eac8e149693d60936ae244fa7bc4f42717a024e4a53e77
data/README.md CHANGED
@@ -1,9 +1,9 @@
1
1
  # Algolia Jekyll Plugin
2
2
 
3
- [![Gem Version](https://badge.fury.io/rb/algoliasearch-jekyll.svg)](http://badge.fury.io/rb/algoliasearch-jekyll)
4
- [![Build Status](https://travis-ci.org/algolia/algoliasearch-jekyll.svg?branch=master)](https://travis-ci.org/algolia/algoliasearch-jekyll)
5
- [![Coverage Status](https://coveralls.io/repos/algolia/algoliasearch-jekyll/badge.svg?branch=master&service=github)](https://coveralls.io/github/algolia/algoliasearch-jekyll?branch=master)
6
- [![Code Climate](https://codeclimate.com/github/algolia/algoliasearch-jekyll/badges/gpa.svg)](https://codeclimate.com/github/algolia/algoliasearch-jekyll)
3
+ [![Gem Version][1]](http://badge.fury.io/rb/algoliasearch-jekyll)
4
+ [![Build Status][2]](https://travis-ci.org/algolia/algoliasearch-jekyll)
5
+ [![Coverage Status][3]](https://coveralls.io/github/algolia/algoliasearch-jekyll?branch=master)
6
+ [![Code Climate][4]](https://codeclimate.com/github/algolia/algoliasearch-jekyll)
7
7
 
8
8
  Jekyll plugin to automatically index your Jekyll posts and pages into an
9
9
  Algolia index by simply running `jekyll algolia push`.
@@ -28,10 +28,10 @@ minimum content to get your started.
28
28
  ```ruby
29
29
  source 'https://rubygems.org'
30
30
 
31
- gem 'jekyll', '>=2.5.3'
31
+ gem 'jekyll', '~> 2.5.3'
32
32
 
33
33
  group :jekyll_plugins do
34
- gem 'algoliasearch-jekyll'
34
+ gem 'algoliasearch-jekyll', '~> 0.4.3'
35
35
  end
36
36
  ```
37
37
 
@@ -59,13 +59,13 @@ algolia:
59
59
  index_name: 'your_index_name'
60
60
  ```
61
61
 
62
- You admin api key will be read from the `ALGOLIA_API_KEY` environment variable.
62
+ You write api key will be read from the `ALGOLIA_API_KEY` environment variable.
63
63
  You can define it on the same line as your command, allowing you to type
64
- `ALGOLIA_API_KEY='your_admin_api_key' jekyll algolia push`.
64
+ `ALGOLIA_API_KEY='your_write_api_key' jekyll algolia push`.
65
65
 
66
66
  ### ⚠ Other, unsecure, method ⚠
67
67
 
68
- You can also store your admin api key in a file named `_algolia_api_key`, in
68
+ You can also store your write api key in a file named `_algolia_api_key`, in
69
69
  your source directory. If you do this we __very, very, very strongly__ encourage
70
70
  you to make sure the file is not tracked in your versioning system.
71
71
 
@@ -100,8 +100,10 @@ algolia:
100
100
 
101
101
  #### `settings`
102
102
 
103
- Here you can pass any custom settings you would like to push to your Algolia
104
- index.
103
+ Here you can pass any specific [index settings][5] to your Algolia index. All
104
+ the settings supported by the API can be passed here.
105
+
106
+ ##### Examples
105
107
 
106
108
  If you want to activate `distinct` and some snippets for example, you would do:
107
109
 
@@ -113,6 +115,28 @@ algolia:
113
115
  attributesToSnippet: ['text:20']
114
116
  ```
115
117
 
118
+ If you want to search in other fields than the default ones, you'll have to edit
119
+ the `attributesToIndex` (default is `%w(title h1 h2 h3 h4 h5 h6 unordered(text)
120
+ unordered(tags))`
121
+
122
+ ```yml
123
+ algolia:
124
+ settings:
125
+ attributesToIndex:
126
+ - title
127
+ - h1
128
+ - h2
129
+ - h3
130
+ - h4
131
+ - h5
132
+ - h6
133
+ - unordered(text)
134
+ - unordered(tags)
135
+ - your_custom_attribute_1
136
+ - your_custom_attribute_2
137
+ - ...
138
+ ```
139
+
116
140
  ### Hooks
117
141
 
118
142
  The `AlgoliaSearchRecordExtractor` contains two methods (`custom_hook_each` and
@@ -169,20 +193,86 @@ push` command:
169
193
 
170
194
  The `algoliasearch-jekyll` plugin works for versions of Jekyll starting from
171
195
  2.5, with a version of Ruby of at least 2.0. You also need
172
- [Bundler](http://bundler.io/) to easily add the gem as a dependency to Jekyll.
196
+ [Bundler][6] to easily add the gem as a dependency to Jekyll.
173
197
 
174
198
  ## Searching
175
199
 
176
200
  This plugin will only index your data in your Algolia index. Adding search
177
- capabilities is quite easy. You can follow [our tutorials][1] or use our forked
178
- version of the popular [Hyde theme][2].
201
+ capabilities is quite easy. You can follow [our tutorials][7] or use our forked
202
+ version of the popular [Hyde theme][8].
179
203
 
180
204
  ## GitHub Pages
181
205
 
182
- Unfortunatly, GitHub does not allow custom plugins to be run on GitHub Pages.
183
- This mean that you will have to manually run `jekyll algolia push` before
184
- pushing your content to GitHub.
206
+ GitHub does not allow custom plugins to be run on GitHub Pages. This means that
207
+ you'll either have to run `jekyll algolia push` manually, or configure TravisCI
208
+ to do it for you.
209
+
210
+ [Travis CI][9] is an hosted continuous integration
211
+ service, and it's free for open-source projects. Properly configured, it can
212
+ automatically reindex your data whenever you push to `gh-pages`.
213
+
214
+ For it to work, you'll have 3 steps to perform.
215
+
216
+ ### 1. Create a `.travis.yml` file
217
+
218
+ Create a file named `.travis.yml` at the root of your project, with the
219
+ following content:
220
+
221
+ ```yml
222
+ language: ruby
223
+ cache: bundler
224
+ branches:
225
+ only:
226
+ - gh-pages
227
+ script:
228
+ - bundle exec jekyll algolia push
229
+ rvm:
230
+ - 2.2
231
+ ```
232
+
233
+ This file will be read by Travis and instruct it to fetch all dependencies
234
+ defined in the `Gemfile`, then run `jekyll algolia push`. This will only be
235
+ triggered when data is pushed to the `gh-pages` branch.
236
+
237
+ ### 2. Update your `_config.yml` file to exclude `vendor`
238
+
239
+ Travis will download all you `Gemfile` dependencies into a directory named
240
+ `vendor`. You have to tell Jekyll to ignore this directory, otherwise Jekyll
241
+ will try to parse it (and fail).
242
+
243
+ Doing so is easy, just add the following line to your `_config.yml` file:
244
+
245
+ ```yml
246
+ exclude: [vendor]
247
+ ```
248
+
249
+ ### 3. Configure Travis
250
+
251
+ In order for Travis to be able to push data to your index on your behalf, you
252
+ have to give it your write API Key. This is achieved by defining an
253
+ `ALGOLIA_API_KEY` [environment variable][10] in Travis settings.
254
+
255
+ You should also uncheck the "Build pull requests" option, otherwise any pull
256
+ request targeting `gh-pages` will trigger the reindexing.
257
+
258
+ ![Travis Configuration][11]
259
+
260
+ ### Done
261
+
262
+ Commit all the changes to the files, and then push to `gh-pages`. Travis will
263
+ catch the event and trigger your indexing for you. You can follow the Travis job
264
+ execution directly on [their website][12].
185
265
 
186
266
 
187
- [1]: https://www.algolia.com/doc/javascript
188
- [2]: https://github.com/algolia/hyde
267
+ [1]: https://badge.fury.io/rb/algoliasearch-jekyll.svg
268
+ [2]: https://travis-ci.org/algolia/algoliasearch-jekyll.svg?branch=master
269
+ [3]: https://coveralls.io/repos/algolia/algoliasearch-jekyll/badge.svg?branch=master&service=github
270
+ [4]: https://codeclimate.com/github/algolia/algoliasearch-jekyll/badges/gpa.svg
271
+ [5]: https://www.algolia.com/doc/ruby#indexing-parameters
272
+ [6]: http://bundler.io/
273
+ [7]: https://www.algolia.com/doc/javascript
274
+ [8]: https://github.com/algolia/hyde
275
+ [9]: https://travis-ci.org/
276
+ [10]: http://docs.travis-ci.com/user/environment-variables/
277
+ [11]: /docs/travis-settings.png
278
+ [12]: https://travis-ci.org
@@ -2,16 +2,16 @@
2
2
  # DO NOT EDIT THIS FILE DIRECTLY
3
3
  # Instead, edit Jeweler::Tasks in Rakefile, and run 'rake gemspec'
4
4
  # -*- encoding: utf-8 -*-
5
- # stub: algoliasearch-jekyll 0.4.3 ruby lib
5
+ # stub: algoliasearch-jekyll 0.5.0 ruby lib
6
6
 
7
7
  Gem::Specification.new do |s|
8
8
  s.name = "algoliasearch-jekyll"
9
- s.version = "0.4.3"
9
+ s.version = "0.5.0"
10
10
 
11
11
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
12
12
  s.require_paths = ["lib"]
13
13
  s.authors = ["Tim Carry"]
14
- s.date = "2015-07-29"
14
+ s.date = "2015-11-03"
15
15
  s.description = "Index all your pages and posts to an Algolia index with `jekyll algolia push`"
16
16
  s.email = "tim@pixelastic.com"
17
17
  s.extra_rdoc_files = [
@@ -29,6 +29,7 @@ Gem::Specification.new do |s|
29
29
  "README.md",
30
30
  "Rakefile",
31
31
  "algoliasearch-jekyll.gemspec",
32
+ "docs/travis-settings.png",
32
33
  "lib/algoliasearch-jekyll.rb",
33
34
  "lib/credential_checker.rb",
34
35
  "lib/push.rb",
Binary file
@@ -135,10 +135,9 @@ class AlgoliaSearchRecordExtractor
135
135
  node.css_path.gsub('html > body > ', '')
136
136
  end
137
137
 
138
- # Returns a custom numeric value representing how relevant to its hierarchy
139
- # this record is. This value can be used in the custom ranking to display more
140
- # relevant records first.
141
- def weight(data)
138
+ # The more words are in common between this node and its parent heading, the
139
+ # higher the score
140
+ def weight_heading_relevance(data)
142
141
  # Get list of unique words in headings
143
142
  title_words = %i(title h1 h2 h3 h4 h5 h6)
144
143
  .select { |title| data.key?(title) }
@@ -152,13 +151,30 @@ class AlgoliaSearchRecordExtractor
152
151
  (title_words & text_words).size
153
152
  end
154
153
 
154
+ # Returns a weight based on the tag_name
155
+ def weight_tag_name(item)
156
+ tag_name = item[:tag_name]
157
+ # No a heading, no weight
158
+ return 0 unless %w(h1 h2 h3 h4 h5 h6).include?(tag_name)
159
+ # h1: 100, h2: 90, ..., h6: 50
160
+ 100 - (tag_name.gsub('h', '').to_i - 1) * 10
161
+ end
162
+
163
+ # Returns an object of all weights
164
+ def weight(item, index)
165
+ {
166
+ tag_name: weight_tag_name(item),
167
+ heading_relevance: weight_heading_relevance(item),
168
+ position: index
169
+ }
170
+ end
171
+
155
172
  def extract
156
173
  items = []
157
174
  html_nodes.each_with_index do |node, index|
158
175
  next unless node.text.size > 0
159
176
 
160
177
  item = metadata.clone
161
- item[:objectID] = "#{item[:slug]}_#{index}"
162
178
  item.merge!(node_hierarchy(node))
163
179
  item[:tag_name] = node.name
164
180
  item[:raw_html] = node_raw_html(node)
@@ -166,7 +182,7 @@ class AlgoliaSearchRecordExtractor
166
182
  item[:unique_hierarchy] = unique_hierarchy(item)
167
183
  item[:css_selector] = node_css_selector(node)
168
184
  item[:css_selector_parent] = node_css_selector(node_heading_parent(node))
169
- item[:weight] = weight(item)
185
+ item[:weight] = weight(item, index)
170
186
 
171
187
  # We pass item through the user defined custom hook
172
188
  item = custom_hook_each(item, node)
@@ -316,7 +316,7 @@ describe(AlgoliaSearchRecordExtractor) do
316
316
  end
317
317
  end
318
318
 
319
- describe 'weight' do
319
+ describe 'weight_heading_relevance' do
320
320
  it 'gets the number of words in text also in the title' do
321
321
  # Given
322
322
  data = {
@@ -325,7 +325,7 @@ describe(AlgoliaSearchRecordExtractor) do
325
325
  }
326
326
 
327
327
  # When
328
- actual = page_file.weight(data)
328
+ actual = page_file.weight_heading_relevance(data)
329
329
 
330
330
  # Then
331
331
  expect(actual).to eq 2
@@ -341,7 +341,7 @@ describe(AlgoliaSearchRecordExtractor) do
341
341
  }
342
342
 
343
343
  # When
344
- actual = page_file.weight(data)
344
+ actual = page_file.weight_heading_relevance(data)
345
345
 
346
346
  # Then
347
347
  expect(actual).to eq 3
@@ -357,7 +357,7 @@ describe(AlgoliaSearchRecordExtractor) do
357
357
  }
358
358
 
359
359
  # When
360
- actual = page_file.weight(data)
360
+ actual = page_file.weight_heading_relevance(data)
361
361
 
362
362
  # Then
363
363
  expect(actual).to eq 2
@@ -372,7 +372,7 @@ describe(AlgoliaSearchRecordExtractor) do
372
372
  }
373
373
 
374
374
  # When
375
- actual = page_file.weight(data)
375
+ actual = page_file.weight_heading_relevance(data)
376
376
 
377
377
  # Then
378
378
  expect(actual).to eq 2
@@ -386,7 +386,7 @@ describe(AlgoliaSearchRecordExtractor) do
386
386
  }
387
387
 
388
388
  # When
389
- actual = page_file.weight(data)
389
+ actual = page_file.weight_heading_relevance(data)
390
390
 
391
391
  # Then
392
392
  expect(actual).to eq 1
@@ -407,13 +407,71 @@ describe(AlgoliaSearchRecordExtractor) do
407
407
  }
408
408
 
409
409
  # When
410
- actual = page_file.weight(data)
410
+ actual = page_file.weight_heading_relevance(data)
411
411
 
412
412
  # Then
413
413
  expect(actual).to eq 2
414
414
  end
415
415
  end
416
416
 
417
+ describe 'weight_tag_name' do
418
+ it 'gives a score of 0 to non-headings' do
419
+ # Given
420
+ data = {
421
+ tag_name: 'p'
422
+ }
423
+
424
+ # When
425
+ actual = page_file.weight_tag_name(data)
426
+
427
+ # Then
428
+ expect(actual).to eq 0
429
+ end
430
+ it 'gives a score of 100 to h1' do
431
+ # Given
432
+ data = {
433
+ tag_name: 'h1'
434
+ }
435
+
436
+ # When
437
+ actual = page_file.weight_tag_name(data)
438
+
439
+ # Then
440
+ expect(actual).to eq 100
441
+ end
442
+ it 'gives a score of 40 to h6' do
443
+ # Given
444
+ data = {
445
+ tag_name: 'h6'
446
+ }
447
+
448
+ # When
449
+ actual = page_file.weight_tag_name(data)
450
+
451
+ # Then
452
+ expect(actual).to eq 50
453
+ end
454
+ end
455
+
456
+ describe 'weight' do
457
+ it 'returns an object with all weights' do
458
+ # Given
459
+ item = {
460
+ tag_name: 'p'
461
+ }
462
+ allow(page_file).to receive(:weight_tag_name) { 10 }
463
+ allow(page_file).to receive(:weight_heading_relevance) { 20 }
464
+
465
+ # When
466
+ actual = page_file.weight(item, 42)
467
+
468
+ # Then
469
+ expect(actual).to include(tag_name: 10)
470
+ expect(actual).to include(heading_relevance: 20)
471
+ expect(actual).to include(position: 42)
472
+ end
473
+ end
474
+
417
475
  describe 'custom_hook_each' do
418
476
  it 'let the user call a custom hook to modify a record' do
419
477
  # Given
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: algoliasearch-jekyll
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.4.3
4
+ version: 0.5.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Tim Carry
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2015-07-29 00:00:00.000000000 Z
11
+ date: 2015-11-03 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: algoliasearch
@@ -211,6 +211,7 @@ files:
211
211
  - README.md
212
212
  - Rakefile
213
213
  - algoliasearch-jekyll.gemspec
214
+ - docs/travis-settings.png
214
215
  - lib/algoliasearch-jekyll.rb
215
216
  - lib/credential_checker.rb
216
217
  - lib/push.rb