diff options
Diffstat (limited to 'search.html')
-rw-r--r-- | search.html | 60 |
1 files changed, 60 insertions, 0 deletions
diff --git a/search.html b/search.html new file mode 100644 index 0000000..fb31595 --- /dev/null +++ b/search.html @@ -0,0 +1,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> |