-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
麦丽素
committed
Aug 4, 2021
1 parent
c91427b
commit 0dfd181
Showing
1 changed file
with
62 additions
and
0 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
<!-- Including InstantSearch.js library and styling --> | ||
<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/instantsearch.min.js"></script> | ||
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/[email protected]/dist/instantsearch.min.css"> | ||
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/[email protected]/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 hightlight = hit._highlightResult; | ||
const title = hightlight.title && hightlight.title.value || ""; | ||
const content = hightlight.html && hightlight.html.value || ""; | ||
|
||
return ` | ||
<div class="list__item"> | ||
<article class="archive__item" itemscope itemtype="https://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, | ||
empty: '{{ site.data.ui-text[site.locale].search_algolia_no_results | default: "No results" }}', | ||
} | ||
}) | ||
); | ||
|
||
// Starting the search only when toggle is clicked | ||
$(document).ready(function () { | ||
$(".search__toggle").on("click", function() { | ||
if(!search.started) { | ||
search.start(); | ||
} | ||
}); | ||
}); | ||
</script> |