~xdavidwu/xdavidwu.link

f814f006b45821809cc6d18dea4206c678707ab5 — Michael Rose 6 years ago e772c94
Add Algolia search provider (#1531)

* Support Lunr and Algolia search providers
* Document search providers and configuration
* Update CHANGELOG and history

close #1416
M CHANGELOG.md => CHANGELOG.md +6 -0
@@ 1,3 1,9 @@
## Unreleased

### Enhancements

* Add support for [Algolia](https://www.algolia.com/) search provider. [#1416](https://github.com/mmistakes/minimal-mistakes/issues/1416)

## [4.9.1](https://github.com/mmistakes/minimal-mistakes/releases/tag/4.9.1)

### Enhancements

M _config.yml => _config.yml +6 -0
@@ 53,6 53,12 @@ atom_feed:
  path                   : # blank (default) uses feed.xml
search                   : # true, false (default)
search_full_content      : # true, false (default)
search_provider          : # lunr (default), algolia
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

# SEO Related
google_site_verification :

M _includes/scripts.html => _includes/scripts.html +7 -10
@@ 13,16 13,13 @@
{% endif %}

{% if site.search == true or page.layout == "search" %}
  {% assign lang = site.locale | slice: 0,2 | default: "en" %}
  {% case lang %}
  {% when "gr" %}
    {% assign lang = "gr" %}
  {% else %}
    {% assign lang = "en" %}
  {% endcase %}
  <script src="{{ '/assets/js/lunr/lunr.min.js' | absolute_url }}"></script>
  <script src="{{ '/assets/js/lunr/lunr-store.js' | absolute_url }}"></script>
  <script src="{{ '/assets/js/lunr/lunr-' | append: lang | append: '.js' | absolute_url }}"></script>
  {%- assign search_provider = site.search_provider | default: "lunr" -%}
  {%- case search_provider -%}
    {%- when "lunr" -%}
      {% include search/lunr-search-scripts.html %}
    {%- when "algolia" -%}
      {% include search/algolia-search-scripts.html %}
  {%- endcase -%}
{% endif %}

{% include analytics.html %}

A _includes/search/algolia-search-scripts.html => _includes/search/algolia-search-scripts.html +54 -0
@@ 0,0 1,54 @@
<!-- Including InstantSearch.js library and styling -->
<script src="https://cdn.jsdelivr.net/npm/instantsearch.js@2.3.3/dist/instantsearch.min.js"></script>
<link rel="stylesheet" type="text/css" href="https://cdn.jsdelivr.net/npm/instantsearch.js@2.3.3/dist/instantsearch.min.css">
<link rel="stylesheet" type="text/css" href="https://cdn.jsdelivr.net/npm/instantsearch.js@2.3.3/dist/instantsearch-theme-algolia.min.css">

<script>
// Instanciating InstantSearch.js with Algolia credentials
const search = instantsearch({
  appId: '{{ site.algolia.application_id }}',
  apiKey: '{{ site.algolia.search_only_api_key }}',
  indexName: '{{ site.algolia.index_name }}',
  searchParameters: {
    restrictSearchableAttributes: [
      'title',
      'content'
    ]
  }
});

const hitTemplate = function(hit) {
  const url = hit.url;
  const title = hit._highlightResult.title.value;
  const content = hit._highlightResult.html.value;

  return `
    <div class="list__item">
      <article class="archive__item" itemscope itemtype="http://schema.org/CreativeWork">
        <h2 class="archive__item-title" itemprop="headline"><a href="{{ site.baseurl }}${url}">${title}</a></h2>
        <div class="archive__item-excerpt" itemprop="description">${content}</div>
      </article>
    </div>
  `;
}

// Adding searchbar and results widgets
search.addWidget(
  instantsearch.widgets.searchBox({
    container: '.search-searchbar',
    {% unless site.algolia.powered_by == false %}poweredBy: true,{% endunless %}
    placeholder: '{{ site.data.ui-text[site.locale].search_placeholder_text | default: "Enter your search term..." }}'
  })
);
search.addWidget(
  instantsearch.widgets.hits({
    container: '.search-hits',
    templates: {
      item: hitTemplate
    }
  })
);

// Starting the search
search.start();
</script>

A _includes/search/lunr-search-scripts.html => _includes/search/lunr-search-scripts.html +10 -0
@@ 0,0 1,10 @@
{% assign lang = site.locale | slice: 0,2 | default: "en" %}
{% case lang %}
{% when "gr" %}
  {% assign lang = "gr" %}
{% else %}
  {% assign lang = "en" %}
{% endcase %}
<script src="{{ '/assets/js/lunr/lunr.min.js' | absolute_url }}"></script>
<script src="{{ '/assets/js/lunr/lunr-store.js' | absolute_url }}"></script>
<script src="{{ '/assets/js/lunr/lunr-' | append: lang | append: '.js' | absolute_url }}"></script>
\ No newline at end of file

R _includes/search_form.html => _includes/search/search_form.html +9 -2
@@ 1,4 1,11 @@
<div class="search-content__inner-wrap">
  <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>
{%- assign search_provider = site.search_provider | default: "lunr" -%}
{%- case search_provider -%}
  {%- 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 "algolia" -%}
    <div class="search-searchbar"></div>
    <div class="search-hits"></div>
{%- endcase -%}
</div>
\ No newline at end of file

M _layouts/default.html => _layouts/default.html +1 -1
@@ 25,7 25,7 @@

    {% if site.search == true %}
      <div class="search-content">
        {% include search_form.html %}
        {% include search/search_form.html %}
      </div>
    {% endif %}


M _layouts/search.html => _layouts/search.html +9 -6
@@ 22,11 22,14 @@ layout: default

    {{ content }}

    <form>
      <input type="input" id="search" class="search-input" placeholder="{{ site.data.ui-text[site.locale].search_placeholder_text | default: 'Enter your search term...' }}" />
    </form>

    <div id="results"></div>

    {%- assign search_provider = site.search_provider | default: "lunr" -%}
    {%- case search_provider -%}
      {%- 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 "algolia" -%}
        <div class="search-searchbar"></div>
        <div class="search-hits"></div>
    {%- endcase -%}
  </div>
</div>

M _sass/minimal-mistakes/_search.scss => _sass/minimal-mistakes/_search.scss +18 -0
@@ 105,3 105,21 @@
    margin-bottom: 0;
  }
}

/* Algolia search */

.ais-search-box {
  max-width: 100% !important;
  margin-bottom: 2em;
}

.archive__item-title .ais-Highlight {
  color: $link-color;
  font-style: normal;
  text-decoration: underline;
}
.archive__item-excerpt .ais-Highlight {
  color: $link-color;
  font-style: normal;
  font-weight: bold;
}