Skip to content

Commit 9d60af1

Browse files
iranzotalha131
authored andcommitted
feat(search): replace tipue_search with lunr.js
Signed-off-by: Pablo Iranzo Gómez <Pablo.Iranzo@gmail.com> Update #275
1 parent 6f73317 commit 9d60af1

File tree

1 file changed

+67
-16
lines changed

1 file changed

+67
-16
lines changed

templates/search.html

Lines changed: 67 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -22,11 +22,14 @@
2222
<meta property="og:image" content="{{ FEATURED_IMAGE }}" />
2323
<meta name="twitter:image" content="{{ FEATURED_IMAGE }}" >
2424
{% endif %}
25+
<script src="https://unpkg.com/lunr/lunr.js"></script>
2526
{% endblock meta_tags_in_head %}
2627

2728
{% block content %}
2829
<div class="span8 offset2">
29-
<div id="tipue_search_content"><div id="tipue_search_loading"></div></div>
30+
<div id="lunrsearchresults">
31+
<ul></ul>
32+
</div>
3033
</div>
3134
{% endblock content %}
3235

@@ -37,21 +40,69 @@
3740
{% endif %}
3841

3942
<script>
40-
$(document).ready(function() {
41-
$('#tipue_search_input').tipuesearch({
42-
{% if 'tipue_search' in PLUGINS %}
43-
'mode' : 'json',
44-
{% else %}
45-
'mode': 'live',
46-
{% endif %}
47-
'show': 10,
48-
'newWindow': false,
49-
{# I cannot place following statements in the conditionals above because then Tipue Search fails to work. Possibly a bug in Tipue Search. #}
50-
{% if not 'tipue_search' in PLUGINS %}
51-
'liveDescription': '.article-content'
52-
{% endif %}
53-
});
54-
});
43+
var items = tipuesearch['pages'];
44+
var documents = tipuesearch["pages"]
45+
var counter = 0
46+
47+
for (item in documents){
48+
documents[item]['id'] = counter;
49+
counter = counter +1;
50+
}
51+
52+
var idx = lunr(function () {
53+
this.ref('id')
54+
this.field('title')
55+
this.field('url')
56+
this.field('text', { boost: 10 })
57+
this.field('tags')
58+
59+
items.forEach(function (doc) {
60+
this.add(doc)
61+
}, this)
62+
})
63+
64+
function lunr_search(term) {
65+
document.getElementById('lunrsearchresults').innerHTML = '<ul></ul>';
66+
if(term) {
67+
document.getElementById('lunrsearchresults').innerHTML = "<p>Search results for '" + term + "'</p>" + document.getElementById('lunrsearchresults').innerHTML;
68+
//put results on the screen.
69+
var results = idx.search(term);
70+
if(results.length>0){
71+
//console.log(idx.search(term));
72+
//if results
73+
for (var i = 0; i < results.length; i++) {
74+
// more statements
75+
var ref = results[i]['ref'];
76+
var url = documents[ref]['url'];
77+
var title = documents[ref]['title'];
78+
var body = documents[ref]['text'].substring(0,160)+'...';
79+
document.querySelectorAll('#lunrsearchresults ul')[0].innerHTML = document.querySelectorAll('#lunrsearchresults ul')[0].innerHTML + "<li class='lunrsearchresult'><a href='" + url + "'><span class='title'>" + title + "</span></a><br /><span class='body'>"+ body +"</span><br /><span class='url'>"+ url +"</span></li>";
80+
}
81+
} else {
82+
document.querySelectorAll('#lunrsearchresults ul')[0].innerHTML = "<li class='lunrsearchresult'>No results found...</li>";
83+
}
84+
}
85+
return false;
86+
}
87+
88+
function getQueryVariable(variable) {
89+
var query = window.location.search.substring(1);
90+
var vars = query.split('&');
91+
92+
for (var i = 0; i < vars.length; i++) {
93+
var pair = vars[i].split('=');
94+
95+
if (pair[0] === variable) {
96+
return decodeURIComponent(pair[1].replace(/\+/g, '%20'));
97+
}
98+
}
99+
}
100+
101+
102+
var searchTerm = getQueryVariable('q');
103+
if (searchTerm) {
104+
lunr_search(searchTerm)
105+
}
55106
</script>
56107

57108
{% endblock script %}

0 commit comments

Comments
 (0)