github.com/hairyhenderson/templater@v3.5.0+incompatible/docs/static/js/search.js (about)

     1  var summaryInclude = 100
     2  
     3  var fuseOptions = {
     4    shouldSort: true,
     5    includeMatches: true,
     6    threshold: 0.0,
     7    tokenize: true,
     8    location: 0,
     9    distance: 100,
    10    maxPatternLength: 32,
    11    minMatchCharLength: 2,
    12    keys: [
    13      { name: "title", weight: 0.8 },
    14      { name: "tags", weight: 0.3 },
    15      { name: "categories", weight: 0.3 },
    16      { name: "contents", weight: 0.7 }
    17    ]
    18  }
    19  
    20  var searchQuery = param("s")
    21  if (searchQuery) {
    22    $("#search-query").val(searchQuery)
    23    executeSearch(searchQuery)
    24  } else {
    25    $("#search-string").replaceWith(searchQuery)
    26    $("#search-results-length").replaceWith(0)
    27  }
    28  
    29  function executeSearch(searchQuery) {
    30    $.getJSON("/index.json", function(data) {
    31      var pages = data
    32      var fuse = new Fuse(pages, fuseOptions)
    33      var result = fuse.search(searchQuery)
    34      console.log({ matches: result })
    35      $("#search-string").replaceWith(searchQuery)
    36      $("#search-results-length").replaceWith(result.length)
    37      if (result.length > 0) {
    38        populateResults(result)
    39      } else {
    40        $("#search-results").append("<p>No matches found</p>")
    41      }
    42    })
    43  }
    44  
    45  function populateResults(result) {
    46    $.each(result, function(key, value) {
    47      var contents = value.item.contents
    48      var snippet = ""
    49      var snippetHighlights = []
    50      var tags = []
    51      if (fuseOptions.tokenize) {
    52        snippetHighlights.push(searchQuery)
    53      } else {
    54        $.each(value.matches, function(matchKey, mvalue) {
    55          if (mvalue.key == "tags" || mvalue.key == "categories") {
    56            snippetHighlights.push(mvalue.value)
    57          } else if (mvalue.key == "contents") {
    58            let startIdx = mvalue.indices[0][0]
    59            let endIdx = mvalue.indices[0][1]
    60            let start = 0
    61            if (startIdx - summaryInclude > 0) {
    62              start = startIdx - summaryInclude
    63            }
    64            let end = contents.length
    65            if (endIdx + summaryInclude < contents.length) {
    66               endIdx + summaryInclude
    67            }
    68            snippet += contents.substring(start, end)
    69            snippetHighlights.push(
    70              mvalue.value.substring(
    71                startIdx,
    72                endIdx - startIdx + 1
    73              )
    74            )
    75          }
    76        })
    77      }
    78  
    79      if (snippet.length < 1) {
    80        snippet += contents.substring(0, summaryInclude * 2)
    81      }
    82      //pull template from hugo template definition
    83      var templateDefinition = $("#search-result-template").html()
    84      //replace values
    85      var output = render(templateDefinition, {
    86        key: key,
    87        title: value.item.title,
    88        link: value.item.permalink,
    89        tags: value.item.tags,
    90        categories: value.item.categories,
    91        snippet: snippet
    92      })
    93      $("#search-results").append(output)
    94  
    95      $.each(snippetHighlights, function(snipkey, snipvalue) {
    96        $("#summary-" + key).mark(snipvalue)
    97      })
    98    })
    99  }
   100  
   101  function param(name) {
   102    return decodeURIComponent(
   103      (location.search.split(name + "=")[1] || "").split("&")[0]
   104    ).replace(/\+/g, " ")
   105  }
   106  
   107  function render(templateString, data) {
   108    var conditionalMatches, conditionalPattern, copy
   109    conditionalPattern = /\$\{\s*isset ([a-zA-Z]*) \s*\}(.*)\$\{\s*end\s*}/g
   110    //since loop below depends on re.lastInxdex, we use a copy to capture any manipulations whilst inside the loop
   111    copy = templateString
   112    while (
   113      (conditionalMatches = conditionalPattern.exec(templateString)) !== null
   114    ) {
   115      if (data[conditionalMatches[1]]) {
   116        //valid key, remove conditionals, leave contents.
   117        copy = copy.replace(conditionalMatches[0], conditionalMatches[2])
   118      } else {
   119        //not valid, remove entire section
   120        copy = copy.replace(conditionalMatches[0], "")
   121      }
   122    }
   123    templateString = copy
   124    //now any conditionals removed we can do simple substitution
   125    var key, find, re
   126    for (key in data) {
   127      find = "\\$\\{\\s*" + key + "\\s*\\}"
   128      re = new RegExp(find, "g")
   129      templateString = templateString.replace(re, data[key])
   130    }
   131    return templateString
   132  }