Blake Allen
2021-10-26 1fc2da4fe293f47ec47a7b5d33fa94fa3a06bb3b
layouts/partials/search.html
@@ -1,5 +1,5 @@
<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...">
        <div id="results-container">
        </div>
@@ -68,12 +68,10 @@
</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,
        cache: 10,
  })
  const scrapedContent = {{$.Site.Data.contentIndex}}
@@ -81,12 +79,11 @@
    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 highlightWindow = 20
    const tokenizedTerm = term.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
      .map(includesCheck)
@@ -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
      }
@@ -119,23 +116,32 @@
  }
  const resultToHTML = ({url, title, content, term}) => {
    const md = content.split("---")[2]
    const text = removeMarkdown(md)
        const text = removeMarkdown(content)
    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 redir = (id, term) => {
        window.location.href = "{{.Site.BaseURL}}" + `${id}#:~:text=${encodeURIComponent(term)}`
  }
  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]
            redir(anchor.id, term)
        }
    })
  source.addEventListener('input', (e) => {
    const term = e.target.value
        term = e.target.value
    contentIndex.search(term, {
      limit: 5,
      depth: 3,
            limit: 15,
      suggest: true,
    }).then(searchResults => {
      const resultIds = [...new Set(searchResults)]
@@ -159,9 +165,7 @@
          .join("\n")
        const anchors = document.getElementsByClassName("result-card");
        [...anchors].forEach(anchor => {
          anchor.onclick = () => {
            window.location.href = `${anchor.id}#:~:text=${encodeURIComponent(term)}`
          }
                    anchor.onclick = () => redir(anchor.id, term)
        })
      }
    })
@@ -203,6 +207,12 @@
    searchButton.addEventListener('keydown', (evt) => {
      openSearch()
    })
        searchContainer.addEventListener('click', (evt) => {
            closeSearch()
        })
        document.getElementById("search-space").addEventListener('click', (evt) => {
            evt.stopPropagation()
        })
  })
</script>