---
layout: page_default
title: 'Search'
---
<section>
  <p><b>Warning:</b> This search is fairly data-intensive! Use WiFi if you can!</p>

  <br/>

  <p>Podcast:</p>
  <input type="radio" name="podcast" id="podcast0" value="rqg" checked/>
  <label for="podcast0">Rusty Quill Gaming</label>
  
  <br/>
  
  <input type="radio" name="podcast" id="podcast1" value="tma" />
  <label for="podcast1">The Magnus Archives</label>

  <br/>
  
  <input type="radio" name="podcast" id="podcast2" value="tf" />
  <label for="podcast2">Trice Forgotten</label>

  <br/>
  
  <input type="radio" name="podcast" id="podcast3" value="neon" />
  <label for="podcast3">Neon Inkwell</label>

  <br/>
  
  <input type="radio" name="podcast" id="podcast4" value="tmagp" />
  <label for="podcast4">The Magnus Protocol</label>

  <br/><br/>
  
  <input type="text" id="search-box" name="query" placeholder="statement of joe spooky" onkeypress="onKey();"/>
  <button id="submit-button" onclick="search();">Search</button>

  <hr/>

  <h3 class="search-results-header">Results:</h3>

  <ul id="search-results"></ul>
</section>

<script>
  window.store = {
  {% for post in site.posts %}
    "{{ post.url | slugify }}": {
      "title": "{{ post.title | xml_escape }}",
      "podcasts": {{ post.podcasts | jsonify }},
      "summary": "{{ post.summary | xml_escape }}",
      "content": {{ post.content | strip_html | jsonify | replace: "\n\n", " "}},
      "url": "{{ post.url | xml_escape }}"
    }
    {% unless forloop.last %},{% endunless %}
  {% endfor %}
  };
</script>
<script src="{{site.baseurl}}/js/lunr.js"></script>
<script>
  function displaySearchResults(results, store) {
    var searchResults = document.getElementById('search-results');
    if (results.length) { // Are there any results?
      var appendString = '';
      for (var i = 0; i < results.length; i++) {  // Iterate over the results
        var item = store[results[i].ref];
        appendString+='<li><a href="{{site.baseurl}}'+item.url+'">'+item.title+'</a>';
        summary = item.summary.replaceAll("&lt;", "<").replaceAll("&gt;", ">");
        appendString+='<p>'+summary+'</p></li>';
      }
      searchResults.innerHTML = appendString;
    } else {
      searchResults.innerHTML = '<li>No results found.</li>';
    }
  }

  function getPodcast() {
    for (var i = 0; i < 5; i++) {
      var element = document.getElementById("podcast" + i);
      if (element.checked) {
        return element.value;
      }
    }
    return "";
  }

  function search() {
    var query = document.getElementById("search-box").value;
    var podcast = getPodcast();
    if (query && podcast) {
      var idx = lunr(function () {
        this.field('id');
        this.field('title', { boost: 10 });
        this.field('summary');
        this.field('content');
        for (var key in window.store) {
          var podlist = window.store[key].podcasts;
          if (podlist != null && podlist.includes(podcast)) {
            this.add({
              'id': key,
              'title': window.store[key].title,
              'summary': window.store[key].summary,
              'content': window.store[key].content
            });
          }
        };
      });
      var results = idx.search(query); // Get lunr to perform a search
      displaySearchResults(results, window.store);
    }
  }

  function onKey() {
    var key = window.event.keyCode;
    // if user has pressed enter
    if (key === 13) {
      search();
    }
  }

</script>