Last active
December 29, 2015 11:29
-
-
Save redox/7664465 to your computer and use it in GitHub Desktop.
Blazing fast search engine showing relevant results after the first keystroke in your Jekyll blog.
Based on Algolia Search + Typeahead.js <3
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
algolia: | |
index_name: blog_posts | |
application_id: ********* | |
search_only_api_key: ************************ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
gem 'jekyll' | |
gem 'github-pages' | |
gem 'algoliasearch' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<input type="text" name="q" class="search-input" placeholder="Search" id="blog-search-input" /> | |
<script src="/assets/algoliasearch.min"></script> | |
<script src="/assets/typeahead.min"></script> <!-- see Algolia's fork: https://github.com/algolia/algoliasearch-client-js#quick-start --> | |
<script> | |
var apiClient = new AlgoliaSearch('{{ site.algolia.application_id }}', '{{ site.algolia.search_only_api_key }}'); | |
var idx = apiClient.initIndex('{{ site.algolia.index_name }}'); | |
function go() { | |
idx.search($('#blog-search-input').val(), function(success, content) { | |
if (success && content.hits.length > 0) { | |
// for now, go to the first match | |
window.location.href = content.hits[0].url; | |
} | |
}); | |
} | |
$('#blog-search-input').typeahead({ | |
name: 'blogposts', | |
remote: idx.getTypeaheadTransport(), | |
valueKey: 'title', | |
}).on('keydown', function(e) { | |
if (e.which == 13) { | |
go(); | |
} | |
}).on('typeahead:selected', function(e) { | |
go(); | |
}).focus(); | |
</script> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require "rubygems" | |
require "tmpdir" | |
require "bundler/setup" | |
require "jekyll" | |
require "algoliasearch" | |
GITHUB_REPONAME = "username/project" | |
namespace :site do | |
jekyll_config = Jekyll.configuration(source: '.', destination: '_site') | |
jekyll_site = Jekyll::Site.new(jekyll_config) | |
desc "Generate blog files" | |
task :generate do | |
jekyll_site.process | |
end | |
desc "Generate, index and publish blog to gh-pages" | |
task :publish, [:algolia_api_key] => :generate do |t, args| | |
raise "missing algolia_api_key argument" if args[:algolia_api_key].nil? | |
# send all title/urls to Algolia's indexing API | |
Algolia.init application_id: jekyll_config['algolia']['application_id'], api_key: args[:algolia_api_key] | |
idx = Algolia::Index.new(jekyll_config['algolia']['index_name']) | |
idx.set_settings attributesToIndex: ['title', 'unordered(url)'] | |
idx.clear! rescue "not fatal" | |
idx.add_objects jekyll_site.posts.map { |post| { title: post.title, url: post.url } } | |
# publish to github | |
Dir.mktmpdir do |tmp| | |
cp_r "_site/.", tmp | |
Dir.chdir tmp | |
system "git init" | |
system "git add ." | |
message = "Site updated at #{Time.now.utc}" | |
system "git commit -m #{message.inspect}" | |
system "git remote add origin [email protected]:#{GITHUB_REPONAME}.git" | |
system "git push origin master:refs/heads/gh-pages --force" | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment