search improvements
This commit is contained in:
		@@ -1,6 +1,7 @@
 | 
				
			|||||||
<div id="search-container">
 | 
					<div id="search-container">
 | 
				
			||||||
    <div>
 | 
					    <div id="search-space">
 | 
				
			||||||
        <input autocomplete="off" id="search-bar" name="search" type="text" aria-label="Search" placeholder="Search for something...">
 | 
					        <input autoComplete="off" id="search-bar" name="search" type="text" aria-label="Search"
 | 
				
			||||||
 | 
					               placeholder="Search for something...">
 | 
				
			||||||
        <div id="results-container">
 | 
					        <div id="results-container">
 | 
				
			||||||
        </div>
 | 
					        </div>
 | 
				
			||||||
    </div>
 | 
					    </div>
 | 
				
			||||||
@@ -37,7 +38,7 @@
 | 
				
			|||||||
                    .replace(/~~/g, "")
 | 
					                    .replace(/~~/g, "")
 | 
				
			||||||
                    .replace(/`{3}.*\n/g, "");
 | 
					                    .replace(/`{3}.*\n/g, "");
 | 
				
			||||||
            }
 | 
					            }
 | 
				
			||||||
      if(options.preserveLinks) {
 | 
					            if (options.preserveLinks) {
 | 
				
			||||||
                output = output.replace(/\[(.*?)\][\[\(](.*?)[\]\)]/g, "$1 ($2)")
 | 
					                output = output.replace(/\[(.*?)\][\[\(](.*?)[\]\)]/g, "$1 ($2)")
 | 
				
			||||||
            }
 | 
					            }
 | 
				
			||||||
            output = output
 | 
					            output = output
 | 
				
			||||||
@@ -68,11 +69,8 @@
 | 
				
			|||||||
</script>
 | 
					</script>
 | 
				
			||||||
<script>
 | 
					<script>
 | 
				
			||||||
    const contentIndex = new FlexSearch.Worker({
 | 
					    const contentIndex = new FlexSearch.Worker({
 | 
				
			||||||
    tokenize: "strict",
 | 
					        tokenize: "reverse",
 | 
				
			||||||
    charset: "latin:advanced",
 | 
					        charset: "latin:extra",
 | 
				
			||||||
    context: true,
 | 
					 | 
				
			||||||
    depth: 3,
 | 
					 | 
				
			||||||
    cache: 10,
 | 
					 | 
				
			||||||
        suggest: true,
 | 
					        suggest: true,
 | 
				
			||||||
    })
 | 
					    })
 | 
				
			||||||
 | 
					
 | 
				
			||||||
@@ -81,7 +79,6 @@
 | 
				
			|||||||
        contentIndex.add(key, value.content)
 | 
					        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 highlight = (content, term) => {
 | 
				
			||||||
        const highlightWindow = 15
 | 
					        const highlightWindow = 15
 | 
				
			||||||
        const tokenizedTerm = term.split(/\s+/).filter(t => t !== "")
 | 
					        const tokenizedTerm = term.split(/\s+/).filter(t => t !== "")
 | 
				
			||||||
@@ -97,7 +94,7 @@
 | 
				
			|||||||
        for (let i = 0; i < Math.max(occurrencesIndices.length - highlightWindow, 0); i++) {
 | 
					        for (let i = 0; i < Math.max(occurrencesIndices.length - highlightWindow, 0); i++) {
 | 
				
			||||||
            const window = occurrencesIndices.slice(i, i + highlightWindow)
 | 
					            const window = occurrencesIndices.slice(i, i + highlightWindow)
 | 
				
			||||||
            const windowSum = window.reduce((total, cur) => total + cur, 0)
 | 
					            const windowSum = window.reduce((total, cur) => total + cur, 0)
 | 
				
			||||||
      if (windowSum > bestSum) {
 | 
					            if (windowSum >= bestSum) {
 | 
				
			||||||
                bestSum = windowSum
 | 
					                bestSum = windowSum
 | 
				
			||||||
                bestIndex = i
 | 
					                bestIndex = i
 | 
				
			||||||
            }
 | 
					            }
 | 
				
			||||||
@@ -123,18 +120,25 @@
 | 
				
			|||||||
        const text = removeMarkdown(md)
 | 
					        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 `<div class="result-card" id="${url}">
 | 
					        return `<button class="result-card" id="${url}">
 | 
				
			||||||
        <h3>${resultTitle}</h3>
 | 
					        <h3>${resultTitle}</h3>
 | 
				
			||||||
        <p>${resultText}</p>
 | 
					        <p>${resultText}</p>
 | 
				
			||||||
    </div>`
 | 
					    </button>`
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    const source = document.getElementById('search-bar')
 | 
					    const source = document.getElementById('search-bar')
 | 
				
			||||||
    const results = document.getElementById("results-container")
 | 
					    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) => {
 | 
					    source.addEventListener('input', (e) => {
 | 
				
			||||||
    const term = e.target.value
 | 
					        term = e.target.value
 | 
				
			||||||
        contentIndex.search(term, {
 | 
					        contentIndex.search(term, {
 | 
				
			||||||
      limit: 5,
 | 
					            limit: 20,
 | 
				
			||||||
            depth: 3,
 | 
					            depth: 3,
 | 
				
			||||||
            suggest: true,
 | 
					            suggest: true,
 | 
				
			||||||
        }).then(searchResults => {
 | 
					        }).then(searchResults => {
 | 
				
			||||||
@@ -169,6 +173,7 @@
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    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 = ""
 | 
				
			||||||
@@ -203,6 +208,12 @@
 | 
				
			|||||||
        searchButton.addEventListener('keydown', (evt) => {
 | 
					        searchButton.addEventListener('keydown', (evt) => {
 | 
				
			||||||
            openSearch()
 | 
					            openSearch()
 | 
				
			||||||
        })
 | 
					        })
 | 
				
			||||||
 | 
					        searchContainer.addEventListener('click', (evt) => {
 | 
				
			||||||
 | 
					            closeSearch()
 | 
				
			||||||
 | 
					        })
 | 
				
			||||||
 | 
					        document.getElementById("search-space").addEventListener('click', (evt) => {
 | 
				
			||||||
 | 
					            evt.stopPropagation()
 | 
				
			||||||
 | 
					        })
 | 
				
			||||||
    })
 | 
					    })
 | 
				
			||||||
 | 
					
 | 
				
			||||||
</script>
 | 
					</script>
 | 
				
			||||||
		Reference in New Issue
	
	Block a user