Skip to content

Fix lunr search problem, add ability to search page excerpt #264

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions _config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ disqus: 'demowebsite'
mailchimp-list: 'https://wowthemes.us11.list-manage.com/subscribe/post?u=8aeb20a530e124561927d3bd8&id=8c3d2d214b'
include: ["_pages"]
permalink: /:title/
search_full_content: true

# Authors
authors:
Expand Down
8 changes: 3 additions & 5 deletions _includes/search-lunr.html
Original file line number Diff line number Diff line change
@@ -1,6 +1,3 @@
<script src="{{site.baseurl}}/assets/js/lunr.js"></script>


<style>
.lunrsearchresult .title {color: #d9230f;}
.lunrsearchresult .url {color: silver;}
Expand All @@ -17,5 +14,6 @@
<div id="lunrsearchresults">
<ul></ul>
</div>

<script src="{{site.baseurl}}/assets/js/lunrsearchengine.js"></script>
<script src="{{ '/assets/js/lunr/lunr.min.js' | relative_url }}"></script>
<script src="{{ '/assets/js/lunr/lunr-store.js' | relative_url }}"></script>
<script src="{{ '/assets/js/lunr/lunr-en.js' | relative_url }}"></script>
57 changes: 57 additions & 0 deletions assets/js/lunr/lunr-en.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
---
layout: none
---

var idx = lunr(function () {
this.field('title')
this.field('excerpt')
this.field('categories')
this.field('tags')
this.ref('id')

this.pipeline.remove(lunr.trimmer)

for (var item in store) {
this.add({
title: store[item].title,
excerpt: store[item].excerpt,
categories: store[item].categories,
tags: store[item].tags,
id: item
})
}
});

function lunr_search(term) {
$('#lunrsearchresults').show( 400 );
$( "body" ).addClass( "modal-open" );

document.getElementById('lunrsearchresults').innerHTML = '<div id="resultsmodal" class="modal fade show d-block" tabindex="-1" role="dialog" aria-labelledby="resultsmodal"> <div class="modal-dialog shadow-lg" role="document"> <div class="modal-content"> <div class="modal-header" id="modtit"> <button type="button" class="close" id="btnx" data-dismiss="modal" aria-label="Close"> &times; </button> </div> <div class="modal-body"> <ul class="mb-0"> </ul> </div> <div class="modal-footer"><button id="btnx" type="button" class="btn btn-danger btn-sm" data-dismiss="modal">Close</button></div></div> </div></div>';
if (term) {
document.getElementById('modtit').innerHTML = "<h5 class='modal-title'>Search results for '" + term + "'</h5>" + document.getElementById('modtit').innerHTML;
//put results on the screen.
var results = idx.search(term);
if(results.length>0){
//console.log(idx.search(term));
//if results
for (var item in results) {
// more statements
var ref = results[item].ref;
var url = store[ref].url;
var title = store[ref]['title'];
var body = store[ref]['excerpt'].substring(0,160)+'...';
document.querySelectorAll('#lunrsearchresults ul')[0].innerHTML = document.querySelectorAll('#lunrsearchresults ul')[0].innerHTML + "<li class='lunrsearchresult'><a href='" + url + "'><span class='title'>" + title + "</span><br /><small><span class='body'>"+ body +"</span><br /><span class='url'>"+ url +"</span></small></a></li>";
}
} else {
document.querySelectorAll('#lunrsearchresults ul')[0].innerHTML = "<li class='lunrsearchresult'>Sorry, no results found. Close & try a different search!</li>";
}
}
return false;
}

$(function() {
$("#lunrsearchresults").on('click', '#btnx', function () {
$('#lunrsearchresults').hide( 5 );
$( "body" ).removeClass( "modal-open" );
});
});
84 changes: 84 additions & 0 deletions assets/js/lunr/lunr-store.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
---
layout: none
---

var store = [
{%- for c in site.collections -%}
{%- if forloop.last -%}
{%- assign l = true -%}
{%- endif -%}
{%- assign docs = c.docs | where_exp:'doc','doc.search != false' -%}
{%- for doc in docs -%}
{%- if doc.header.teaser -%}
{%- capture teaser -%}{{ doc.header.teaser }}{%- endcapture -%}
{%- else -%}
{%- assign teaser = site.teaser -%}
{%- endif -%}
{
"title": {{ doc.title | jsonify }},
"excerpt":
{%- if site.search_full_content == true -%}
{{ doc.content | newline_to_br |
replace:"<br />", " " |
replace:"</p>", " " |
replace:"</h1>", " " |
replace:"</h2>", " " |
replace:"</h3>", " " |
replace:"</h4>", " " |
replace:"</h5>", " " |
replace:"</h6>", " "|
strip_html | strip_newlines | jsonify }},
{%- else -%}
{{ doc.content | newline_to_br |
replace:"<br />", " " |
replace:"</p>", " " |
replace:"</h1>", " " |
replace:"</h2>", " " |
replace:"</h3>", " " |
replace:"</h4>", " " |
replace:"</h5>", " " |
replace:"</h6>", " "|
strip_html | strip_newlines | truncatewords: 50 | jsonify }},
{%- endif -%}
"categories": {{ doc.categories | jsonify }},
"tags": {{ doc.tags | jsonify }},
"url": {{ doc.url | relative_url | jsonify }},
"teaser": {{ teaser | relative_url | jsonify }}
}{%- unless forloop.last and l -%},{%- endunless -%}
{%- endfor -%}
{%- endfor -%}{%- if site.lunr.search_within_pages -%},
{%- assign pages = site.pages | where_exp: 'doc', 'doc.search != false' | where_exp: 'doc', 'doc.title != null' -%}
{%- for doc in pages -%}
{%- if forloop.last -%}
{%- assign l = true -%}
{%- endif -%}
{
"title": {{ doc.title | jsonify }},
"excerpt":
{%- if site.search_full_content == true -%}
{{ doc.content | newline_to_br |
replace:"<br />", " " |
replace:"</p>", " " |
replace:"</h1>", " " |
replace:"</h2>", " " |
replace:"</h3>", " " |
replace:"</h4>", " " |
replace:"</h5>", " " |
replace:"</h6>", " "|
strip_html | strip_newlines | jsonify }},
{%- else -%}
{{ doc.content | newline_to_br |
replace:"<br />", " " |
replace:"</p>", " " |
replace:"</h1>", " " |
replace:"</h2>", " " |
replace:"</h3>", " " |
replace:"</h4>", " " |
replace:"</h5>", " " |
replace:"</h6>", " "|
strip_html | strip_newlines | truncatewords: 50 | jsonify }},
{%- endif -%}
"url": {{ doc.url | absolute_url | jsonify }}
}{%- unless forloop.last and l -%},{%- endunless -%}
{%- endfor -%}
{%- endif -%}]
Loading