diff options
author | Lucas Ramage <ramage.lucas@protonmail.com> | 2019-04-11 19:21:27 +0000 |
---|---|---|
committer | Göktürk Yüksek <gokturk@gentoo.org> | 2019-12-19 15:52:49 -0500 |
commit | 7f13e9b5f59ee496db5b8af7ab093a17c8f76066 (patch) | |
tree | df74bfd4c0cd50b260d8394d7f756aa24a5f9e2c /search.js | |
parent | str.tokenize.function.xsl: Fix line endings from CRLF to LF. (diff) | |
download | devmanual-7f13e9b5f59ee496db5b8af7ab093a17c8f76066.tar.gz devmanual-7f13e9b5f59ee496db5b8af7ab093a17c8f76066.tar.bz2 devmanual-7f13e9b5f59ee496db5b8af7ab093a17c8f76066.zip |
Implement search functionality via lunr.js
Bug: https://bugs.gentoo.org/674378
Signed-off-by: Lucas Ramage <ramage.lucas@protonmail.com>
Signed-off-by: Göktürk Yüksek <gokturk@gentoo.org>
Diffstat (limited to 'search.js')
-rw-r--r-- | search.js | 60 |
1 files changed, 60 insertions, 0 deletions
diff --git a/search.js b/search.js new file mode 100644 index 0000000..0b9292f --- /dev/null +++ b/search.js @@ -0,0 +1,60 @@ +/* + * Copyright 2019 Gentoo Authors + * Distributed under the terms of the GNU GPL version 2 or later + */ +"use strict"; + +var search_index = lunr(function () { + this.ref('name'); + this.field('text'); + this.field('url'); + + documents.forEach(function (doc) { + this.add(doc); + }, this); +}); + +var search_input = document.getElementById("searchInput"); + +search_input.addEventListener("keyup", function(event) { + if(event.keyCode === 13) { + event.preventDefault(); + document.getElementById("mw-searchButton").click(); + } +}); + +function getContents(docs, article) { + var contents = { text: "", url: "" }; + + for (var i = 0; i< docs.length; i++) { + if (docs[i].name == article) { + contents.text = docs[i].text; + contents.url = docs[i].url; + } + } + return contents; +} + +function search() { + var term = document.getElementById("searchInput").value; + if (term !== "") { + var results = search_index.search(term); + if (results.length > 0) { + $("#searchResults .modal-body").empty(); + $.each(results, function(index, result) { + var title = result.ref; + var contents = getContents(documents, title); + + $("#searchResults .modal-body").append(`<article><h5><a href="${contents.url}"> + ${title}</a></h5><p>${contents.text}</p></article>`); + }); + } else { + $("#searchResults .modal-body").empty(); + $("#searchResults .modal-body").append("<p>No results found.</p>"); + } + } else { + $("#searchResults .modal-body").empty(); + $("#searchResults .modal-body").append("<p>No search term defined.</p>"); + } + $("#searchResults").modal(); +} |