aboutsummaryrefslogtreecommitdiff
path: root/docs/js/search.js
diff options
context:
space:
mode:
authorFelix Mulder <felix.mulder@gmail.com>2016-11-07 17:29:50 +0100
committerFelix Mulder <felix.mulder@gmail.com>2016-11-07 17:29:50 +0100
commite0c1d1665c8100ccad6c324e8e15e38df9b39327 (patch)
treee5553b111aef328b5cc71909f6978cf0b5401c23 /docs/js/search.js
parentf179c9279f9f9a3bf9ef342900590e761855a696 (diff)
downloaddotty-e0c1d1665c8100ccad6c324e8e15e38df9b39327.tar.gz
dotty-e0c1d1665c8100ccad6c324e8e15e38df9b39327.tar.bz2
dotty-e0c1d1665c8100ccad6c324e8e15e38df9b39327.zip
Fix highlighting in search, add highlights in forwarding links
Diffstat (limited to 'docs/js/search.js')
-rw-r--r--docs/js/search.js54
1 files changed, 43 insertions, 11 deletions
diff --git a/docs/js/search.js b/docs/js/search.js
index 6127f55c7..63544d52f 100644
--- a/docs/js/search.js
+++ b/docs/js/search.js
@@ -1,4 +1,19 @@
(function() {
+ var searchTerm = getQueryVariable('query');
+ function showMark(first, last) {
+ var res = "";
+ if ((first.length + last.length) <= 150) {
+ res = '<p class="result-text">' + first + "<mark>" + searchTerm + "</mark>" + last + '...</p></li>';
+ } else if (first.length > 150 && last.length > 150) {
+ res = '<p class="result-text">' + first.substring(first.length - 76, first.length - 1) + "<mark>" + searchTerm + "</mark>" + last.substring(0, 75) + '...</p></li>';
+ } else if (first.length > last.length) {
+ var lastLen = Math.min(last.length, 75);
+ var frstLen = Math.min(first.length, 150 - lastLen);
+ res = '<p class="result-text">' + first.substring(first.length - frstLen - 1, first.length - 1) + "<mark>" + searchTerm + "</mark>" + last.substring(0, 75) + '...</p></li>';
+ }
+ return res;
+ }
+
function displaySearchResults(results, store) {
var searchResults = document.getElementById('search-results');
@@ -7,8 +22,21 @@
for (var i = 0; i < results.length; i++) { // Iterate over the results
var item = store[results[i].ref];
- appendString += '<li><a href="' + item.url + '"><h3>' + item.title + '</h3></a>';
- appendString += '<p>' + item.content.substring(0, 100) + '...</p></li>';
+ appendString += '<li><a href="' + item.url + "?highlight=" + searchTerm + '"><h3>' + item.title + '</h3></a>';
+
+ var text = item.content.split(searchTerm)
+
+ if (text.length == 1) {
+ appendString += '<p class="result-text">' + item.content.substring(0, 150) + '...</p></li>';
+ }
+ else if (text.length == 2) {
+ appendString += showMark(text[0], text[1]);
+ }
+ else {
+ for (var j = 0; j < text.length - 1; j++) {
+ appendString += showMark(text[j], text[j+1]);
+ }
+ }
}
searchResults.innerHTML = appendString;
@@ -30,27 +58,31 @@
}
}
- var searchTerm = getQueryVariable('query');
-
if (searchTerm) {
document.getElementById('search-box').setAttribute("value", searchTerm);
- var idx = lunr(function () {
- this.field('id');
- this.field('title', { boost: 10 }); // hits in title get a boost
- this.field('author');
- this.field('content');
+ var idx = elasticlunr(function () {
+ this.addField('title'); // hits in title get a boost
+ this.addField('author');
+ this.addField('content');
+ this.setRef('id');
});
for (var key in window.store) { // Add the data to lunr
var author = (typeof window.store[key].author === 'undefined') ? "" : window.store[key].author;
- idx.add({
+ idx.addDoc({
'id': key,
'title': window.store[key].title,
'author': author,
'content': window.store[key].content
});
- var results = idx.search(searchTerm); // Get lunr to perform a search
+ var results = idx.search(searchTerm, {
+ fields: {
+ title: { boost: 3 },
+ author: { boost: 2 },
+ content: { boost: 1 }
+ }
+ }); // Get lunr to perform a search
displaySearchResults(results, window.store); // We'll write this in the next section
}
}