|
| 1 | +<script src="/js/lunr.js"></script> |
| 2 | + |
| 3 | +<script> |
| 4 | + {% assign counter = 0 %} |
| 5 | + var documents = [{% for page in site.pages %}{% if page.url contains '.xml' or page.url contains 'assets' %}{% else %}{ |
| 6 | + "id": {{ counter }}, |
| 7 | + "url": "{{ site.url }}{{ page.url }}", |
| 8 | + "title": "{{ page.title }}", |
| 9 | + "body": "{{ page.content | markdownify | replace: '.', '. ' | replace: '</h2>', ': ' | replace: '</h3>', ': ' | replace: '</h4>', ': ' | replace: '</p>', ' ' | strip_html | strip_newlines | replace: ' ', ' ' | replace: '"', ' ' }}"{% assign counter = counter | plus: 1 %} |
| 10 | + }, {% endif %}{% endfor %}{% for page in site.without-plugin %}{ |
| 11 | + "id": {{ counter }}, |
| 12 | + "url": "{{ site.url }}{{ page.url }}", |
| 13 | + "title": "{{ page.title }}", |
| 14 | + "body": "{{ page.content | markdownify | replace: '.', '. ' | replace: '</h2>', ': ' | replace: '</h3>', ': ' | replace: '</h4>', ': ' | replace: '</p>', ' ' | strip_html | strip_newlines | replace: ' ', ' ' | replace: '"', ' ' }}"{% assign counter = counter | plus: 1 %} |
| 15 | + }, {% endfor %}{% for page in site.posts %}{ |
| 16 | + "id": {{ counter }}, |
| 17 | + "url": "{{ site.url }}{{ page.url }}", |
| 18 | + "title": "{{ page.title }}", |
| 19 | + "body": "{{ page.date | date: "%Y/%m/%d" }} - {{ page.content | markdownify | replace: '.', '. ' | replace: '</h2>', ': ' | replace: '</h3>', ': ' | replace: '</h4>', ': ' | replace: '</p>', ' ' | strip_html | strip_newlines | replace: ' ', ' ' | replace: '"', ' ' }}"{% assign counter = counter | plus: 1 %} |
| 20 | + }, {% endfor %}{% for page in site.docs %}{ |
| 21 | + "id": {{ counter }}, |
| 22 | + "url": "{{ site.url }}{{ page.url }}", |
| 23 | + "title": "{{ page.title }}", |
| 24 | + "body": "{{ page.date | date: "%Y/%m/%d" }} - {{ page.content | markdownify | replace: '.', '. ' | replace: '\', '/' | replace: '</h2>', ': ' | replace: '</h3>', ': ' | replace: '</h4>', ': ' | replace: '</p>', ' ' | strip_html | strip_newlines | replace: ' ', ' ' | replace: '"', ' ' }}"{% assign counter = counter | plus: 1 %} |
| 25 | + }{% if forloop.last %}{% else %}, {% endif %}{% endfor %}]; |
| 26 | + |
| 27 | + var idx = lunr(function () { |
| 28 | + this.ref('id') |
| 29 | + this.field('title') |
| 30 | + this.field('body') |
| 31 | + |
| 32 | + documents.forEach(function (doc) { |
| 33 | + this.add(doc) |
| 34 | + }, this) |
| 35 | + }); |
| 36 | + function lunr_search(term) { |
| 37 | + document.getElementById('lunrsearchresults').innerHTML = '<ul></ul>'; |
| 38 | + if(term) { |
| 39 | + document.getElementById('lunrsearchresults').innerHTML = "<p>Search results for '" + term + "'</p>" + document.getElementById('lunrsearchresults').innerHTML; |
| 40 | + //put results on the screen. |
| 41 | + var results = idx.search(term); |
| 42 | + if(results.length>0){ |
| 43 | + //console.log(idx.search(term)); |
| 44 | + //if results |
| 45 | + for (var i = 0; i < results.length; i++) { |
| 46 | + // more statements |
| 47 | + var ref = results[i]['ref']; |
| 48 | + var url = documents[ref]['url']; |
| 49 | + var title = documents[ref]['title']; |
| 50 | + var body = documents[ref]['body'].substring(0,160)+'...'; |
| 51 | + document.querySelectorAll('#lunrsearchresults ul')[0].innerHTML = document.querySelectorAll('#lunrsearchresults ul')[0].innerHTML + "<li class='lunrsearchresult'><a href='" + url + "'><span class='title'>" + title + "</span><br /><span class='body'>"+ body +"</span><br /><span class='url'>"+ url +"</span></a></li>"; |
| 52 | + } |
| 53 | + } else { |
| 54 | + document.querySelectorAll('#lunrsearchresults ul')[0].innerHTML = "<li class='lunrsearchresult'>No results found...</li>"; |
| 55 | + } |
| 56 | + } |
| 57 | + return false; |
| 58 | + } |
| 59 | +</script> |
| 60 | +<style> |
| 61 | + .lunrsearchresult .title {color: #d9230f;} |
| 62 | + .lunrsearchresult .url {color: silver;} |
| 63 | + .lunrsearchresult a {display: block; color: #777;} |
| 64 | + .lunrsearchresult a:hover, .lunrsearchresult a:focus {text-decoration: none;} |
| 65 | + .lunrsearchresult a:hover .title {text-decoration: underline;} |
| 66 | +</style> |
| 67 | + |
| 68 | +<form class="navbar-form navbar-right" onSubmit="return lunr_search(document.getElementById('lunrsearch').value);"> |
| 69 | + <div class="form-group"> |
| 70 | + <label for="lunrsearch" class="fa fa-search"></label> |
| 71 | + <input type="text" class="form-control fa fa-search" id="lunrsearch" name="q" maxlength="255" value="" placeholder="Search via Lunr.js" /> |
| 72 | + </div> |
| 73 | +</form> |
0 commit comments