search fix

This commit is contained in:
Jacky Zhao 2021-10-25 15:00:55 -07:00
parent 1835b97a7a
commit f7b89db8ee

View File

@ -1,7 +1,6 @@
<div id="search-container"> <div id="search-container">
<div id="search-space"> <div id="search-space">
<input autoComplete="off" id="search-bar" name="search" type="text" aria-label="Search" <input autocomplete="off" id="search-bar" name="search" type="text" aria-label="Search" placeholder="Search for something...">
placeholder="Search for something...">
<div id="results-container"> <div id="results-container">
</div> </div>
</div> </div>
@ -72,6 +71,7 @@
tokenize: "reverse", tokenize: "reverse",
charset: "latin:extra", charset: "latin:extra",
suggest: true, suggest: true,
cache: 10,
}) })
const scrapedContent = {{$.Site.Data.contentIndex}} const scrapedContent = {{$.Site.Data.contentIndex}}
@ -80,10 +80,10 @@
} }
const highlight = (content, term) => { const highlight = (content, term) => {
const highlightWindow = 15 const highlightWindow = 20
const tokenizedTerm = term.split(/\s+/).filter(t => t !== "") const tokenizedTerm = term.split(/\s+/).filter(t => t !== "")
const splitText = content.split(/\s+/).filter(t => t !== "") const splitText = content.split(/\s+/).filter(t => t !== "")
const includesCheck = (token) => tokenizedTerm.some(term => token.toLowerCase().includes(term.toLowerCase())) const includesCheck = (token) => tokenizedTerm.some(term => token.toLowerCase().startsWith(term.toLowerCase()))
const occurrencesIndices = splitText const occurrencesIndices = splitText
.map(includesCheck) .map(includesCheck)
@ -116,8 +116,7 @@
} }
const resultToHTML = ({url, title, content, term}) => { const resultToHTML = ({url, title, content, term}) => {
const md = content.split("---")[2] const text = removeMarkdown(content)
const text = removeMarkdown(md)
const resultTitle = highlight(title, term) const resultTitle = highlight(title, term)
const resultText = highlight(text, term) const resultText = highlight(text, term)
return `<button class="result-card" id="${url}"> return `<button class="result-card" id="${url}">
@ -142,8 +141,7 @@
source.addEventListener('input', (e) => { source.addEventListener('input', (e) => {
term = e.target.value term = e.target.value
contentIndex.search(term, { contentIndex.search(term, {
limit: 20, limit: 15,
depth: 3,
suggest: true, suggest: true,
}).then(searchResults => { }).then(searchResults => {
const resultIds = [...new Set(searchResults)] const resultIds = [...new Set(searchResults)]
@ -175,7 +173,6 @@
const searchContainer = document.getElementById("search-container") const searchContainer = document.getElementById("search-container")
function openSearch() { function openSearch() {
if (searchContainer.style.display === "none" || searchContainer.style.display === "") { if (searchContainer.style.display === "none" || searchContainer.style.display === "") {
source.value = "" source.value = ""