diff options
author | uvok cheetah | 2020-05-22 21:07:36 +0200 |
---|---|---|
committer | uvok cheetah | 2020-05-22 21:07:36 +0200 |
commit | 19d0278400cbe1459b95554a1d0e616e13d3d485 (patch) | |
tree | 02e78fbf35a0e0886c642772aa827cfe3e07da26 /search.html | |
parent | 4228cddead7b4ef367ba1341e9908ace3cf7df9b (diff) |
(WIP) Add search page
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> |