search improvements
This commit is contained in:
		@@ -1,6 +1,7 @@
 | 
			
		||||
<div id="search-container">
 | 
			
		||||
    <div>
 | 
			
		||||
        <input autocomplete="off" id="search-bar" name="search" type="text" aria-label="Search" placeholder="Search for something...">
 | 
			
		||||
    <div id="search-space">
 | 
			
		||||
        <input autoComplete="off" id="search-bar" name="search" type="text" aria-label="Search"
 | 
			
		||||
               placeholder="Search for something...">
 | 
			
		||||
        <div id="results-container">
 | 
			
		||||
        </div>
 | 
			
		||||
    </div>
 | 
			
		||||
@@ -68,11 +69,8 @@
 | 
			
		||||
</script>
 | 
			
		||||
<script>
 | 
			
		||||
    const contentIndex = new FlexSearch.Worker({
 | 
			
		||||
    tokenize: "strict",
 | 
			
		||||
    charset: "latin:advanced",
 | 
			
		||||
    context: true,
 | 
			
		||||
    depth: 3,
 | 
			
		||||
    cache: 10,
 | 
			
		||||
        tokenize: "reverse",
 | 
			
		||||
        charset: "latin:extra",
 | 
			
		||||
        suggest: true,
 | 
			
		||||
    })
 | 
			
		||||
 | 
			
		||||
@@ -81,7 +79,6 @@
 | 
			
		||||
        contentIndex.add(key, value.content)
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
  const stopwords = ['i','me','my','myself','we','our','ours','ourselves','you','your','yours','yourself','yourselves','he','him','his','himself','she','her','hers','herself','it','its','itself','they','them','their','theirs','themselves','what','which','who','whom','this','that','these','those','am','is','are','was','were','be','been','being','have','has','had','having','do','does','did','doing','a','an','the','and','but','if','or','because','as','until','while','of','at','by','for','with','about','against','between','into','through','during','before','after','above','below','to','from','up','down','in','out','on','off','over','under','again','further','then','once','here','there','when','where','why','how','all','any','both','each','few','more','most','other','some','such','no','nor','not','only','own','same','so','than','too','very','s','t','can','will','just','don','should','now']
 | 
			
		||||
    const highlight = (content, term) => {
 | 
			
		||||
        const highlightWindow = 15
 | 
			
		||||
        const tokenizedTerm = term.split(/\s+/).filter(t => t !== "")
 | 
			
		||||
@@ -97,7 +94,7 @@
 | 
			
		||||
        for (let i = 0; i < Math.max(occurrencesIndices.length - highlightWindow, 0); i++) {
 | 
			
		||||
            const window = occurrencesIndices.slice(i, i + highlightWindow)
 | 
			
		||||
            const windowSum = window.reduce((total, cur) => total + cur, 0)
 | 
			
		||||
      if (windowSum > bestSum) {
 | 
			
		||||
            if (windowSum >= bestSum) {
 | 
			
		||||
                bestSum = windowSum
 | 
			
		||||
                bestIndex = i
 | 
			
		||||
            }
 | 
			
		||||
@@ -123,18 +120,25 @@
 | 
			
		||||
        const text = removeMarkdown(md)
 | 
			
		||||
        const resultTitle = highlight(title, term)
 | 
			
		||||
        const resultText = highlight(text, term)
 | 
			
		||||
    return `<div class="result-card" id="${url}">
 | 
			
		||||
        return `<button class="result-card" id="${url}">
 | 
			
		||||
        <h3>${resultTitle}</h3>
 | 
			
		||||
        <p>${resultText}</p>
 | 
			
		||||
    </div>`
 | 
			
		||||
    </button>`
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    const source = document.getElementById('search-bar')
 | 
			
		||||
    const results = document.getElementById("results-container")
 | 
			
		||||
    let term
 | 
			
		||||
    source.addEventListener("keyup", (e) => {
 | 
			
		||||
        if (e.key === "Enter") {
 | 
			
		||||
            const anchor = document.getElementsByClassName("result-card")[0]
 | 
			
		||||
            window.location.href = `${anchor.id}#:~:text=${encodeURIComponent(term)}`
 | 
			
		||||
        }
 | 
			
		||||
    })
 | 
			
		||||
    source.addEventListener('input', (e) => {
 | 
			
		||||
    const term = e.target.value
 | 
			
		||||
        term = e.target.value
 | 
			
		||||
        contentIndex.search(term, {
 | 
			
		||||
      limit: 5,
 | 
			
		||||
            limit: 20,
 | 
			
		||||
            depth: 3,
 | 
			
		||||
            suggest: true,
 | 
			
		||||
        }).then(searchResults => {
 | 
			
		||||
@@ -169,6 +173,7 @@
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
    const searchContainer = document.getElementById("search-container")
 | 
			
		||||
 | 
			
		||||
    function openSearch() {
 | 
			
		||||
        if (searchContainer.style.display === "none" || searchContainer.style.display === "") {
 | 
			
		||||
            source.value = ""
 | 
			
		||||
@@ -203,6 +208,12 @@
 | 
			
		||||
        searchButton.addEventListener('keydown', (evt) => {
 | 
			
		||||
            openSearch()
 | 
			
		||||
        })
 | 
			
		||||
        searchContainer.addEventListener('click', (evt) => {
 | 
			
		||||
            closeSearch()
 | 
			
		||||
        })
 | 
			
		||||
        document.getElementById("search-space").addEventListener('click', (evt) => {
 | 
			
		||||
            evt.stopPropagation()
 | 
			
		||||
        })
 | 
			
		||||
    })
 | 
			
		||||
 | 
			
		||||
</script>
 | 
			
		||||
		Reference in New Issue
	
	Block a user