Added Lunr.js site search, fixed errors with jekyll serve

This commit is contained in:
Zachary Klosko 2020-11-12 16:54:33 -05:00
parent 578e58ccff
commit 3b27f2c01e
7 changed files with 3075 additions and 10 deletions

View File

@ -4,17 +4,17 @@ GEM
addressable (2.7.0)
public_suffix (>= 2.0.2, < 5.0)
colorator (1.1.0)
concurrent-ruby (1.1.6)
em-websocket (0.5.1)
concurrent-ruby (1.1.7)
em-websocket (0.5.2)
eventmachine (>= 0.12.9)
http_parser.rb (~> 0.6.0)
eventmachine (1.2.7)
ffi (1.13.1)
forwardable-extended (2.6.0)
http_parser.rb (0.6.0)
i18n (1.8.3)
i18n (1.8.5)
concurrent-ruby (~> 1.0)
jekyll (4.1.0)
jekyll (4.1.1)
addressable (~> 2.4)
colorator (~> 1.0)
em-websocket (~> 0.5)
@ -33,7 +33,7 @@ GEM
sassc (> 2.0.1, < 3.0)
jekyll-watch (2.2.1)
listen (~> 3.0)
kramdown (2.2.1)
kramdown (2.3.0)
rexml
kramdown-parser-gfm (1.1.0)
kramdown (~> 2.0)
@ -44,12 +44,12 @@ GEM
mercenary (0.4.0)
pathutil (0.16.2)
forwardable-extended (~> 2.6)
public_suffix (4.0.5)
public_suffix (4.0.6)
rb-fsevent (0.10.4)
rb-inotify (0.10.1)
ffi (~> 1.0)
rexml (3.2.4)
rouge (3.20.0)
rouge (3.24.0)
safe_yaml (1.0.5)
sassc (2.4.0)
ffi (~> 1.9)

View File

@ -17,9 +17,7 @@
<a class="nav-link px-0" href="https://github.com/libretime/libretime" target="_blank"><i class="fab fa-github mr-1"></i> Star on Github</a>
</li>
<li class="nav-item ml-lg-4 mb-2 mb-lg-0">
<div id="search-searchbar">
<!-- SearchBox widget will appear here -->
</div>
<a class="nav-link px-0" href="/search"><i class="fas fa-search"></i></a>
</li>
</ul>
</div>

View File

@ -0,0 +1,71 @@
<script src="/lunr.js"></script>
<script>
{% assign counter = 0 %}
var documents = [{% for page in site.pages %}{% if page.url contains '.xml' or page.url contains 'assets' %}{% else %}{
"id": {{ counter }},
"url": "{{ site.url }}{{ page.url }}",
"title": "{{ page.title }}",
"body": "{{ page.content | markdownify | replace: '.', '. ' | replace: '</h2>', ': ' | replace: '</h3>', ': ' | replace: '</h4>', ': ' | replace: '</p>', ' ' | strip_html | strip_newlines | replace: ' ', ' ' | replace: '"', ' ' }}"{% assign counter = counter | plus: 1 %}
}, {% endif %}{% endfor %}{% for page in site.without-plugin %}{
"id": {{ counter }},
"url": "{{ site.url }}{{ page.url }}",
"title": "{{ page.title }}",
"body": "{{ page.content | markdownify | replace: '.', '. ' | replace: '</h2>', ': ' | replace: '</h3>', ': ' | replace: '</h4>', ': ' | replace: '</p>', ' ' | strip_html | strip_newlines | replace: ' ', ' ' | replace: '"', ' ' }}"{% assign counter = counter | plus: 1 %}
}, {% endfor %}{% for page in site.docs %}{
"id": {{ counter }},
"url": "{{ site.url }}{{ page.url }}",
"title": "{{ page.title }}",
"body": "{{ page.content | markdownify | replace: '.', '. ' | replace: '</h2>', ': ' | replace: '</h3>', ': ' | replace: '</h4>', ': ' | replace: '</p>', ' ' | strip_html | strip_newlines | replace: ' ', ' ' | replace: '"', ' ' }}"{% assign counter = counter | plus: 1 %}
}{% if forloop.last %}{% else %}, {% endif %}{% endfor %}];
var idx = lunr(function () {
this.ref('id')
this.field('title')
this.field('body')
documents.forEach(function (doc) {
this.add(doc)
}, this)
});
function lunr_search(term) {
document.getElementById('lunrsearchresults').innerHTML = '<ul></ul>';
if(term) {
document.getElementById('lunrsearchresults').innerHTML = "<p>Search results for '" + term + "'</p>" + document.getElementById('lunrsearchresults').innerHTML;
//put results on the screen.
var results = idx.search(term);
if(results.length>0){
//console.log(idx.search(term));
//if results
for (var i = 0; i < results.length; i++) {
// more statements
var ref = results[i]['ref'];
var url = documents[ref]['url'];
var title = documents[ref]['title'];
var body = documents[ref]['body'].substring(0,160)+'...';
document.querySelectorAll('#lunrsearchresults ul')[0].innerHTML = document.querySelectorAll('#lunrsearchresults ul')[0].innerHTML + "<li class='lunrsearchresult mb-2'><a href='" + url + "'><span class='title'>" + title + "</span><br /><span class='body'>"+ body +"</span><br /><span class='url'>"+ url +"</span></a></li>";
}
} else {
document.querySelectorAll('#lunrsearchresults ul')[0].innerHTML = "<li class='lunrsearchresult'>No results found...</li>";
}
}
return false;
}
</script>
<style>
#lunrsearchresults {padding-top: 0.2rem;}
.lunrsearchresult {padding-bottom: 1rem;}
.lunrsearchresult .title {color: #f05f40;}
.lunrsearchresult .url {color: gray;}
.lunrsearchresult a {display: block; color: black;}
.lunrsearchresult a:hover, .lunrsearchresult a:focus {text-decoration: none;}
.lunrsearchresult a:hover .title {text-decoration: underline;}
</style>
<form onSubmit="return lunr_search(document.getElementById('lunrsearch').value);">
<p><input type="text" class="form-control" id="lunrsearch" name="q" maxlength="255" value="" placeholder="Search via Lunr.js" /></p>
</form>
<div id="lunrsearchresults">
<ul class="list-line mb-0"></ul>
</div>

View File

@ -1,5 +1,6 @@
---
layout: default
title: Docs
---
<main>

View File

@ -1,5 +1,6 @@
---
layout: default
title: FAQ
---
<!-- Promo Section -->

2977
docs/lunr.js Normal file

File diff suppressed because it is too large Load Diff

17
docs/search.html Normal file
View File

@ -0,0 +1,17 @@
---
layout: default
---
<section class="pt-11 pb-6">
<div class="container-lg">
{% include search-lunr.html %}
<div id="search-box">
<!-- SearchBox widget will appear here -->
</div>
<div id="hits">
<!-- Hits widget will appear here -->
</div>
</div>
</section>