Skip to content

Commit

Permalink
Implement feature mmistakes#1652 google custom search support. For no…
Browse files Browse the repository at this point in the history
…w we only support the look and feel "results only" and the engine must be set to use that layout. Add a google section to the search providers and search_form and search layout.
  • Loading branch information
user414 committed May 11, 2018
1 parent ea37611 commit ff29fea
Show file tree
Hide file tree
Showing 5 changed files with 52 additions and 3 deletions.
6 changes: 4 additions & 2 deletions _config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -60,13 +60,15 @@ atom_feed:
path : # blank (default) uses feed.xml
search : # true, false (default)
search_full_content : # true, false (default)
search_provider : # lunr (default), algolia
search_provider : # lunr (default), algolia, google
algolia:
application_id : # YOUR_APPLICATION_ID
index_name : # YOUR_INDEX_NAME
search_only_api_key : # YOUR_SEARCH_ONLY_API_KEY
powered_by : # true (default), false

google:
search_engine_id: # YOUR_SEARCH_ENGINE_ID
instant_search: # false (default), true
# SEO Related
google_site_verification :
bing_site_verification :
Expand Down
4 changes: 3 additions & 1 deletion _includes/scripts.html
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,12 @@
{%- case search_provider -%}
{%- when "lunr" -%}
{% include search/lunr-search-scripts.html %}
{%- when "google" -%}
{% include search/google-search-scripts.html %}
{%- when "algolia" -%}
{% include search/algolia-search-scripts.html %}
{%- endcase -%}
{% endif %}

{% include analytics.html %}
{% include /comments-providers/scripts.html %}
{% include /comments-providers/scripts.html %}
31 changes: 31 additions & 0 deletions _includes/search/google-search-scripts.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<script>
(function () {
var cx = '{{ site.google.search_engine_id }}';
var gcse = document.createElement('script');
gcse.type = 'text/javascript';
gcse.async = true;
gcse.src = 'https://cse.google.com/cse.js?cx=' + cx;
var s = document.getElementsByTagName('script')[0];
s.parentNode.insertBefore(gcse, s);
})();

function executeQuery() {
var input = document.getElementById('cse-search-input-box-id');
var element = google.search.cse.element.getElement('searchresults-only0');
if (input.value == '') {
element.clearAllResults();
} else {
element.execute(input.value);
}
return false;
}

{% if site.google.instant_search %}
$(document).ready(function () {
$('input#cse-search-input-box-id').on('keyup', function () {
executeQuery();
}
);
});
{% endif %}
</script>
7 changes: 7 additions & 0 deletions _includes/search/search_form.html
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,13 @@
{%- when "lunr" -%}
<input type="text" id="search" class="search-input" tabindex="-1" placeholder="{{ site.data.ui-text[site.locale].search_placeholder_text | default: 'Enter your search term...' }}" />
<div id="results" class="results"></div>
{%- when "google" -%}
<form onsubmit="return executeQuery();" id="cse-search-box-form-id">
<input type="text" id="cse-search-input-box-id" class="search-input" tabindex="-1" placeholder="{{ site.data.ui-text[site.locale].search_placeholder_text | default: 'Enter your search term...' }}" />
</form>
<div id="results" class="results">
<gcse:searchresults-only></gcse:searchresults-only>
</div>
{%- when "algolia" -%}
<div class="search-searchbar"></div>
<div class="search-hits"></div>
Expand Down
7 changes: 7 additions & 0 deletions _layouts/search.html
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,13 @@ <h1 id="page-title" class="page__title">{{ page.title }}</h1>
{%- when "lunr" -%}
<input type="text" id="search" class="search-input" tabindex="-1" placeholder="{{ site.data.ui-text[site.locale].search_placeholder_text | default: 'Enter your search term...' }}" />
<div id="results" class="results"></div>
{%- when "google" -%}
<form onsubmit="return executeQuery();" id="cse-search-box-form-id">
<input type="text" id="cse-search-input-box-id" class="search-input" tabindex="-1" placeholder="{{ site.data.ui-text[site.locale].search_placeholder_text | default: 'Enter your search term...' }}" />
</form>
<div id="results" class="results">
<gcse:searchresults-only></gcse:searchresults-only>
</div>
{%- when "algolia" -%}
<div class="search-searchbar"></div>
<div class="search-hits"></div>
Expand Down

0 comments on commit ff29fea

Please sign in to comment.