Skip to content

Instantly share code, notes, and snippets.

@redox
Last active December 29, 2015 11:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save redox/7664465 to your computer and use it in GitHub Desktop.
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
algolia:
index_name: blog_posts
application_id: *********
search_only_api_key: ************************
gem 'jekyll'
gem 'github-pages'
gem 'algoliasearch'
<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>
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 git@github.com:#{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