aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorGöktürk Yüksek <gokturk@gentoo.org>2019-12-09 21:11:15 -0500
committerGöktürk Yüksek <gokturk@gentoo.org>2019-12-19 15:58:07 -0500
commit173273b10e50d649924f70223cd5817585fc59af (patch)
treeaee61e8abaacb070699afd456f6d45412163bccb /search.js
parentRewrite the search functionality and extend the coverage (diff)
downloaddevmanual-173273b10e50d649924f70223cd5817585fc59af.tar.gz
devmanual-173273b10e50d649924f70223cd5817585fc59af.tar.bz2
devmanual-173273b10e50d649924f70223cd5817585fc59af.zip
search.js: highlight the search terms in results
Signed-off-by: Göktürk Yüksek <gokturk@gentoo.org>
Diffstat (limited to 'search.js')
-rw-r--r--search.js25
1 files changed, 24 insertions, 1 deletions
diff --git a/search.js b/search.js
index ab28f87..9cbf05a 100644
--- a/search.js
+++ b/search.js
@@ -42,9 +42,32 @@ function search() {
$.each(results, function(index, result) {
var uid = result.ref;
var contents = getContents(documents, uid);
+ var stems = Object.keys(result.matchData.metadata);
+ var positions = [];
+ var text = "";
+ var pos = 0;
+
+ stems.forEach(function (stem) {
+ positions = positions.concat(result.matchData.metadata[stem].text.position);
+ });
+ positions.sort(function(x, y) {
+ if (x[0] < y[0]) { return -1; }
+ else if (x[0] > y[0]) { return 1; }
+ else { return 0; }
+ });
+
+ for (var i = 0; i < positions.length; i++) {
+ text += contents.text.substring(pos, positions[i][0]);
+ pos = positions[i][0];
+ text += "<span style='background-color: yellow;'>";
+ text += contents.text.substring(pos, pos + positions[i][1]);
+ pos += positions[i][1];
+ text += "</span>";
+ }
+ text += contents.text.substring(pos);
$("#searchResults .modal-body").append(`<article><h5><a href="${contents.url}">
- ${title}</a></h5><p>${contents.text}</p></article>`);
+ ${contents.name}</a></h5><p>${text}</p></article>`);
});
} else {
$("#searchResults .modal-body").empty();