mirror of
https://github.com/alvierahman90/gronk.git
synced 2024-11-22 07:19:53 +00:00
Clean up code, limit search results to 15
This commit is contained in:
parent
d6c9a8adab
commit
1386679c49
74
search.js
74
search.js
@ -1,5 +1,10 @@
|
|||||||
|
const HEADERS = "headers"
|
||||||
|
const PATH = "path"
|
||||||
|
const TAGS = "tags"
|
||||||
|
const TITLE = "title"
|
||||||
|
|
||||||
const fuse = new Fuse(data, {
|
const fuse = new Fuse(data, {
|
||||||
keys: ['path', 'title', 'tags', 'headers'],
|
keys: [ HEADERS, PATH, TAGS, TITLE ],
|
||||||
includeMatches: true
|
includeMatches: true
|
||||||
})
|
})
|
||||||
|
|
||||||
@ -7,63 +12,52 @@ const searchBar = document.getElementById('search')
|
|||||||
const results = document.getElementById('results')
|
const results = document.getElementById('results')
|
||||||
|
|
||||||
function callback() {
|
function callback() {
|
||||||
console.log("called")
|
|
||||||
console.log(searchBar.value)
|
|
||||||
results.innerHTML = ''
|
results.innerHTML = ''
|
||||||
fuse.search(searchBar.value).forEach(r => {
|
fuse.search(searchBar.value).slice(0,15).forEach(r => {
|
||||||
console.log(r)
|
|
||||||
wrapper = document.createElement('div')
|
wrapper = document.createElement('div')
|
||||||
wrapper.className = "article"
|
wrapper.className = "article"
|
||||||
|
|
||||||
extra_info = document.createElement('p')
|
display_matches = {}
|
||||||
extra_info.className = "smallText"
|
display_matches[HEADERS] = []
|
||||||
extra_info.innerHTML = "tags: "
|
display_matches[PATH] = []
|
||||||
if (r.item.tags == null) {
|
display_matches[TAGS] = []
|
||||||
extra_info.innerHTML += "none"
|
display_matches[TITLE] = []
|
||||||
} else {
|
|
||||||
extra_info.innerHTML += "[" + r.item.tags.join(', ') + ']'
|
|
||||||
}
|
|
||||||
extra_info.innerHTML += ' path: ' + r.item.path
|
|
||||||
|
|
||||||
header_matches_p = document.createElement('p')
|
|
||||||
header_matches_p.className = "smallText"
|
|
||||||
header_matches = []
|
|
||||||
|
|
||||||
r.matches.every(match => {
|
r.matches.every(match => {
|
||||||
if (header_matches.length > 3) {
|
if (display_matches[match.key].length > 3) {
|
||||||
header_matches.push('...')
|
display_matches[match.key].push('...')
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
|
|
||||||
if (match.key === "headers") {
|
display_match = match.value
|
||||||
display_match = match.value
|
if (match.indices.length >= 1) {
|
||||||
if (match.indices.length >= 1) {
|
match.indices.sort((a, b) => (b[1]-b[0])-(a[1]-a[0]))
|
||||||
match.indices.sort((a, b) => (b[1]-b[0])-(a[1]-a[0]))
|
indexPair = match.indices[0]
|
||||||
indexPair = match.indices[0]
|
matching_slice = match.value.slice(indexPair[0], indexPair[1]+1)
|
||||||
matching_slice = match.value.slice(indexPair[0], indexPair[1]+1)
|
display_match = match.value.replace(
|
||||||
console.log(matching_slice)
|
matching_slice,
|
||||||
console.log(display_match)
|
'<span class="matchHighlight">' + matching_slice + '</span>'
|
||||||
display_match = display_match.replace(
|
)
|
||||||
matching_slice,
|
|
||||||
'<span class="matchHighlight">' + matching_slice + '</span>'
|
|
||||||
)
|
|
||||||
}
|
|
||||||
header_matches.push(display_match)
|
|
||||||
}
|
}
|
||||||
|
display_matches[match.key].push(display_match)
|
||||||
|
|
||||||
return true
|
return true
|
||||||
})
|
})
|
||||||
|
|
||||||
header_matches_p.innerHTML += "header_matches: [" + header_matches.join(', ') + ']'
|
|
||||||
|
|
||||||
content = document.createElement('a')
|
content = document.createElement('a')
|
||||||
content.innerHTML = r.item.title
|
content.innerHTML = r.item.title
|
||||||
content.href = r.item.path
|
content.href = r.item.path
|
||||||
|
|
||||||
wrapper.appendChild(content)
|
wrapper.appendChild(content)
|
||||||
wrapper.appendChild(extra_info)
|
|
||||||
if (header_matches.length > 0) {
|
Object.keys(display_matches).forEach(key => {
|
||||||
wrapper.appendChild(header_matches_p)
|
if (display_matches[key].length < 1) return
|
||||||
}
|
|
||||||
|
p = document.createElement('p')
|
||||||
|
p.className = "smallText"
|
||||||
|
p.innerHTML += key + ": [" + display_matches[key].join(', ') + ']'
|
||||||
|
wrapper.appendChild(p)
|
||||||
|
})
|
||||||
|
|
||||||
results.appendChild(wrapper)
|
results.appendChild(wrapper)
|
||||||
})
|
})
|
||||||
|
Loading…
Reference in New Issue
Block a user