2024-01-02 18:22:15 +00:00
|
|
|
/*
|
|
|
|
* search.js expects an array `data` containing objects with the following keys:
|
|
|
|
* headers: [string]
|
|
|
|
* path: string
|
|
|
|
* tags: [string]
|
|
|
|
* title: string
|
|
|
|
* uuid: string
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
2021-08-21 09:11:56 +00:00
|
|
|
const HEADERS = "headers"
|
|
|
|
const PATH = "path"
|
|
|
|
const TAGS = "tags"
|
|
|
|
const TITLE = "title"
|
|
|
|
|
2023-02-06 13:42:45 +00:00
|
|
|
const SEARCH_TIMEOUT_MS = 100
|
|
|
|
var SEARCH_TIMEOUT_ID = -1
|
|
|
|
|
2024-01-02 18:22:15 +00:00
|
|
|
const fuse = new Fuse(search_data, {
|
2021-11-17 14:44:11 +00:00
|
|
|
keys: [
|
|
|
|
{
|
|
|
|
name: HEADERS,
|
2024-01-02 18:22:15 +00:00
|
|
|
weight: 1
|
2021-11-17 14:44:11 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
name: PATH,
|
2023-02-06 11:51:27 +00:00
|
|
|
weight: 0.1
|
2021-11-17 14:44:11 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
name: TAGS,
|
2024-01-02 18:22:15 +00:00
|
|
|
weight: 0.5
|
2021-11-17 14:44:11 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
name: TITLE,
|
2024-01-02 18:22:15 +00:00
|
|
|
weight: 2
|
2021-11-17 14:44:11 +00:00
|
|
|
}
|
|
|
|
],
|
2023-02-06 13:02:00 +00:00
|
|
|
includeMatches: true,
|
|
|
|
useExtendedSearch: true,
|
|
|
|
ignoreLocation: true,
|
|
|
|
threshhold: 0.4,
|
2021-08-19 13:43:42 +00:00
|
|
|
})
|
|
|
|
|
2023-02-06 13:02:00 +00:00
|
|
|
const RESULTS_MAX = 5
|
|
|
|
|
2021-08-19 13:43:42 +00:00
|
|
|
const searchBar = document.getElementById('search')
|
2021-08-24 13:53:25 +00:00
|
|
|
const resultsDiv = document.getElementById('results')
|
2021-08-19 13:43:42 +00:00
|
|
|
|
2021-08-24 13:53:25 +00:00
|
|
|
var results = []
|
|
|
|
|
2023-02-06 13:42:45 +00:00
|
|
|
function updateResultsWithTimeout() {
|
|
|
|
console.log("clearing timeout")
|
|
|
|
if (SEARCH_TIMEOUT_ID) SEARCH_TIMEOUT_ID = clearTimeout(SEARCH_TIMEOUT_ID)
|
|
|
|
SEARCH_TIMEOUT_ID = setTimeout(updateResults, SEARCH_TIMEOUT_MS)
|
|
|
|
}
|
|
|
|
|
2021-08-24 13:53:25 +00:00
|
|
|
function updateResults() {
|
2023-02-06 13:42:45 +00:00
|
|
|
console.log("updating results")
|
2021-08-24 13:53:25 +00:00
|
|
|
resultsDiv.innerHTML = ''
|
2023-02-06 13:02:00 +00:00
|
|
|
results = fuse.search(searchBar.value, { limit: RESULTS_MAX })
|
2021-08-24 13:53:25 +00:00
|
|
|
results.forEach(r => {
|
2021-08-19 13:43:42 +00:00
|
|
|
wrapper = document.createElement('div')
|
2021-08-19 14:31:28 +00:00
|
|
|
wrapper.className = "article"
|
|
|
|
|
2021-08-21 09:11:56 +00:00
|
|
|
display_matches = {}
|
|
|
|
display_matches[HEADERS] = []
|
|
|
|
display_matches[PATH] = []
|
|
|
|
display_matches[TAGS] = []
|
|
|
|
display_matches[TITLE] = []
|
2021-08-20 13:31:34 +00:00
|
|
|
|
|
|
|
r.matches.every(match => {
|
2021-08-21 09:11:56 +00:00
|
|
|
if (display_matches[match.key].length > 3) {
|
|
|
|
display_matches[match.key].push('...')
|
2021-08-20 13:31:34 +00:00
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2021-08-21 09:11:56 +00:00
|
|
|
display_match = match.value
|
|
|
|
if (match.indices.length >= 1) {
|
|
|
|
match.indices.sort((a, b) => (b[1]-b[0])-(a[1]-a[0]))
|
|
|
|
indexPair = match.indices[0]
|
|
|
|
matching_slice = match.value.slice(indexPair[0], indexPair[1]+1)
|
|
|
|
display_match = match.value.replace(
|
|
|
|
matching_slice,
|
|
|
|
'<span class="matchHighlight">' + matching_slice + '</span>'
|
|
|
|
)
|
2021-08-20 13:31:34 +00:00
|
|
|
}
|
2021-08-21 09:11:56 +00:00
|
|
|
display_matches[match.key].push(display_match)
|
|
|
|
|
2021-08-20 13:31:34 +00:00
|
|
|
return true
|
|
|
|
})
|
|
|
|
|
2021-08-19 13:43:42 +00:00
|
|
|
content = document.createElement('a')
|
|
|
|
content.innerHTML = r.item.title
|
|
|
|
content.href = r.item.path
|
2021-08-19 14:31:28 +00:00
|
|
|
|
2021-08-19 13:43:42 +00:00
|
|
|
wrapper.appendChild(content)
|
2021-08-21 09:11:56 +00:00
|
|
|
|
|
|
|
Object.keys(display_matches).forEach(key => {
|
|
|
|
if (display_matches[key].length < 1) return
|
|
|
|
|
|
|
|
p = document.createElement('p')
|
|
|
|
p.className = "smallText"
|
|
|
|
p.innerHTML += key + ": [" + display_matches[key].join(', ') + ']'
|
|
|
|
wrapper.appendChild(p)
|
|
|
|
})
|
2021-08-19 14:31:28 +00:00
|
|
|
|
2021-08-24 13:53:25 +00:00
|
|
|
resultsDiv.appendChild(wrapper)
|
2021-08-19 13:43:42 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2021-08-24 13:53:25 +00:00
|
|
|
searchBar.addEventListener('keyup', e => {
|
2021-08-24 14:36:03 +00:00
|
|
|
console.log(e)
|
2021-08-24 13:53:25 +00:00
|
|
|
// if user pressed enter
|
|
|
|
if (e.keyCode === 13) {
|
2021-08-24 14:36:03 +00:00
|
|
|
if (e.shiftKey) {
|
|
|
|
window.open(results[0].item.path, '_blank')
|
|
|
|
} else {
|
2021-08-24 13:53:25 +00:00
|
|
|
window.location.href = results[0].item.path
|
2021-08-24 14:36:03 +00:00
|
|
|
}
|
|
|
|
return
|
2021-08-24 13:53:25 +00:00
|
|
|
}
|
2023-02-06 13:42:45 +00:00
|
|
|
updateResultsWithTimeout()
|
2021-08-24 13:53:25 +00:00
|
|
|
})
|
2021-12-03 20:22:39 +00:00
|
|
|
|
2023-02-06 13:42:45 +00:00
|
|
|
searchBar.addEventListener('change', updateResultsWithTimeout)
|
2021-12-03 20:22:39 +00:00
|
|
|
|
|
|
|
const searchParams = new URL(window.location.href).searchParams;
|
|
|
|
searchBar.value = searchParams.get('q');
|
|
|
|
updateResults();
|
|
|
|
|
|
|
|
console.log(results)
|
|
|
|
|
|
|
|
if (searchParams.has('lucky')) {
|
|
|
|
window.location.href = results[0].item.path;
|
|
|
|
}
|