-
Notifications
You must be signed in to change notification settings - Fork 270
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
ELY-2394 Add the ability to search for blog posts on the Elytron Blog…
ELY-2394 Add the ability to search for blog posts on the Elytron Blog…
- Loading branch information
1 parent
6a11bbe
commit 6f33c75
Showing
4 changed files
with
3,108 additions
and
38 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
<script src="{{ '/assets/javascript/lunr.js' | relative_url }}"></script> | ||
|
||
<script> | ||
{% assign counter = 0 %} | ||
|
||
var documents = [{% for page in site.posts %}{ | ||
"id": {{ counter }}, | ||
"url": "{{ site.baseurl }}{{ page.url }}", | ||
"link": "{{ page.link }}", | ||
"title": "{{ page.title }}", | ||
"author": "{{ page.author }}", | ||
"postDate": "{{ page.date | date: '%B %d, %Y'}}", | ||
"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 class="search-result"></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 link = documents[ref]['link']; | ||
var body = documents[ref]['body'].substring(0,75)+'...'; | ||
var postDate = documents[ref]['postDate']; | ||
var realUrl = link == '' ? url : link; | ||
document.querySelectorAll('#lunrsearchresults ul')[0].innerHTML = document.querySelectorAll('#lunrsearchresults ul')[0].innerHTML + | ||
"<div class='lunrsearchresult'>" + | ||
"<span class='post-title grid__item width-12-12'>" + | ||
"<a href= '" + realUrl + "'>" + title + "</a>" + | ||
"</span>" + | ||
"</div>"; | ||
document.querySelector("#posts-div").innerHTML = ""; | ||
} | ||
} 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: #d9230f;} | ||
.lunrsearchresult .url {color: silver;} | ||
.lunrsearchresult a {display: block; color: #777;} | ||
.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);" class="grid__item"> | ||
<p><input type="text" class="form-control search-input" id="lunrsearch" name="q" value="" placeholder="Search..." /></p> | ||
</form> | ||
<div id="lunrsearchresults"> | ||
<ul id="search-result" name="search-result" class="search-result"></ul> | ||
</div> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.