mirror of
https://github.com/davegallant/davegallant.github.io.git
synced 2025-08-13 20:00:20 +00:00
Switch themes to minimo
This commit is contained in:
47
themes/minimo/src/scripts/search/algolia.js
Normal file
47
themes/minimo/src/scripts/search/algolia.js
Normal file
@@ -0,0 +1,47 @@
|
||||
import algoliasearch from 'algoliasearch/lite'
|
||||
|
||||
import {
|
||||
appendResults,
|
||||
getUrlSearchParam,
|
||||
setSearchingIndicator
|
||||
} from './helpers'
|
||||
|
||||
const { appId, indexName, searchApiKey } = window.algolia
|
||||
|
||||
const client = algoliasearch(appId, searchApiKey)
|
||||
|
||||
const index = client.initIndex(
|
||||
`${indexName}${window.location.pathname.replace('/search/', '')}`
|
||||
)
|
||||
|
||||
const doSearch = (term, resultsBlock) => {
|
||||
setSearchingIndicator(resultsBlock)
|
||||
|
||||
if (!term) {
|
||||
appendResults([], resultsBlock)
|
||||
} else {
|
||||
index.search(
|
||||
term,
|
||||
{ attributesToRetrieve: ['title', 'href'], hitsPerPage: 10 },
|
||||
(err, content) => {
|
||||
if (err) console.error(err)
|
||||
else appendResults(content.hits, resultsBlock)
|
||||
}
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
const searchForm = document.getElementById('search-form')
|
||||
const searchInputBox = document.getElementById('search-term')
|
||||
const resultsBlock = document.getElementById('search-results')
|
||||
|
||||
let term = getUrlSearchParam('q')
|
||||
searchInputBox.value = term
|
||||
searchInputBox.focus()
|
||||
doSearch(term, resultsBlock)
|
||||
|
||||
searchForm.addEventListener('submit', e => {
|
||||
e.preventDefault()
|
||||
|
||||
doSearch(searchInputBox.value, resultsBlock)
|
||||
})
|
Reference in New Issue
Block a user