summaryrefslogtreecommitdiff
path: root/search.html
blob: fb315952e9079e4f53e59e4f9004e709aeaf20a9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
---
layout: page
---
<script src="/assets/js/lunr.min.js"></script>

<h1>Search results for <span id="qterm"></span></h1>
<div id="results">
</div>


<script>
  // data
  var pages = [
    {% for post in site.posts %}
      {
        "id": "{{ post.url | slufigy }}",
        "title": {{ post.title | jsonify }},
        "content": {{ post.content | strip_html | jsonify }},
        "url": "{{ post.url | xml_escape }}"
      }
      {% unless forloop.last %},{% endunless %}
    {% endfor %}
  ];

  // index
  var idx = lunr(function () {
    this.ref('url');
    this.field('title');
    this.field('content');
    pages.forEach(function (doc) {
      this.add(doc);
    }, this);
  });

  // search
  var queryterm = "zeit";
  var result = idx.search(queryterm);
  if(result.length) {
    var root = document.getElementById("results").appendChild(
      document.createElement("ul")
    );


    result.forEach(r => {
      var p = pages.filter(p => p.id == r.ref)[0];
      var li = document.createElement("li")
      var el = li.appendChild(document.createElement("a"));
      el.textContent = p.title;
      el.href = p.url;
      root.appendChild(li);

    });

  } else {
    var el = document.createElement("p");
    el.textContent = "No results";
    document.getElementById("results").appendChild(el);
  }
  document.getElementById("qterm").textContent = queryterm;
</script>