Compare commits

..

No commits in common. "c8d70a36619a717c3cd7cac790ac8e8b4f61fd4b" and "d6c9a8adab8bf53a1c4f3edbe39b0596342aa870" have entirely different histories.

3 changed files with 44 additions and 51 deletions

View File

@ -1,71 +1,73 @@
const HEADERS = "headers"
const PATH = "path"
const TAGS = "tags"
const TITLE = "title"
const fuse = new Fuse(data, { const fuse = new Fuse(data, {
keys: [ HEADERS, PATH, TAGS, TITLE ], keys: ['path', 'title', 'tags', 'headers'],
includeMatches: true includeMatches: true
}) })
const searchBar = document.getElementById('search') const searchBar = document.getElementById('search')
const resultsMax = document.getElementById('resultsMax')
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).slice(0, parseInt(resultsMax.value)).forEach(r => { fuse.search(searchBar.value).forEach(r => {
console.log(r)
wrapper = document.createElement('div') wrapper = document.createElement('div')
wrapper.className = "article" wrapper.className = "article"
display_matches = {} extra_info = document.createElement('p')
display_matches[HEADERS] = [] extra_info.className = "smallText"
display_matches[PATH] = [] extra_info.innerHTML = "tags: "
display_matches[TAGS] = [] if (r.item.tags == null) {
display_matches[TITLE] = [] extra_info.innerHTML += "none"
} 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 (display_matches[match.key].length > 3) { if (header_matches.length > 3) {
display_matches[match.key].push('...') header_matches.push('...')
return false return false
} }
display_match = match.value if (match.key === "headers") {
if (match.indices.length >= 1) { display_match = match.value
match.indices.sort((a, b) => (b[1]-b[0])-(a[1]-a[0])) if (match.indices.length >= 1) {
indexPair = match.indices[0] match.indices.sort((a, b) => (b[1]-b[0])-(a[1]-a[0]))
matching_slice = match.value.slice(indexPair[0], indexPair[1]+1) indexPair = match.indices[0]
display_match = match.value.replace( matching_slice = match.value.slice(indexPair[0], indexPair[1]+1)
matching_slice, console.log(matching_slice)
'<span class="matchHighlight">' + matching_slice + '</span>' console.log(display_match)
) 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)
Object.keys(display_matches).forEach(key => { if (header_matches.length > 0) {
if (display_matches[key].length < 1) return wrapper.appendChild(header_matches_p)
}
p = document.createElement('p')
p.className = "smallText"
p.innerHTML += key + ": [" + display_matches[key].join(', ') + ']'
wrapper.appendChild(p)
})
results.appendChild(wrapper) results.appendChild(wrapper)
}) })
} }
searchBar.addEventListener('keyup', callback) searchBar.addEventListener('keyup', callback)
searchBar.addEventListener('change', callback)
resultsMax.addEventListener('keyup', callback)
resultsMax.addEventListener('change', callback)
callback() callback()

View File

@ -73,19 +73,13 @@ blockquote * {
margin: 0; margin: 0;
} }
#searchWrapper > input { #search {
padding: 1em; padding: 1em;
margin: 1em 0.5em 1em 0.5em; margin: 1em 0 1em 0;
font-size: 1em; font-size: 1em;
min-width: 0; width: 100%;
} }
#searchWrapper {
display: flex
}
#search { flex-grow: 9 }
#results { #results {
overflow-x: scroll; overflow-x: scroll;
} }

View File

@ -12,14 +12,11 @@ These are my personal notes. Correctness is not guaranteed.
Browse <a href="/notes">here</a> or by tag <a href="/.tags">here</a>. Browse <a href="/notes">here</a> or by tag <a href="/.tags">here</a>.
</p> </p>
<div id="searchWrapper">
<input placeholder="Search" id="search"> <input placeholder="Search" id="search">
<input type="number" id="resultsMax" min="0" value="5">
</div>
<div id="results"> <div id="results">
</div> </div>
<p class="smallText"> page generated by <a href="https://github.com/alvierahman90/notes2web">notes2web</a></p> <p style="font-size: 0.7em;"> page generated by <a href="https://github.com/alvierahman90/notes2web">notes2web</a></p>
<script src="/fuse.js"> </script> <script src="/fuse.js"> </script>
<script> const data = $data$ </script> <script> const data = $data$ </script>