aboutsummaryrefslogtreecommitdiffhomepage
path: root/doc/html/mkdocs/js/search.js
diff options
context:
space:
mode:
authornodiscc <nodiscc@gmail.com>2017-07-04 21:37:30 +0200
committerGitHub <noreply@github.com>2017-07-04 21:37:30 +0200
commit5b25a9635fe808bc1c4f3eee8cdf8485115da75d (patch)
treed5c46525f3c739d9c63de860427e0b3cadd688e5 /doc/html/mkdocs/js/search.js
parentd5d22a6d07917865c44148ad76f43c65a929a890 (diff)
parent8bf94136e10c64496711c8f66a4f58f89c515360 (diff)
downloadShaarli-5b25a9635fe808bc1c4f3eee8cdf8485115da75d.tar.gz
Shaarli-5b25a9635fe808bc1c4f3eee8cdf8485115da75d.tar.zst
Shaarli-5b25a9635fe808bc1c4f3eee8cdf8485115da75d.zip
Merge pull request #772 from nodiscc/rtfd
Generate HTML documentation using MkDocs
Diffstat (limited to 'doc/html/mkdocs/js/search.js')
-rw-r--r--doc/html/mkdocs/js/search.js88
1 files changed, 88 insertions, 0 deletions
diff --git a/doc/html/mkdocs/js/search.js b/doc/html/mkdocs/js/search.js
new file mode 100644
index 00000000..d5c86616
--- /dev/null
+++ b/doc/html/mkdocs/js/search.js
@@ -0,0 +1,88 @@
1require([
2 base_url + '/mkdocs/js/mustache.min.js',
3 base_url + '/mkdocs/js/lunr.min.js',
4 'text!search-results-template.mustache',
5 'text!../search_index.json',
6], function (Mustache, lunr, results_template, data) {
7 "use strict";
8
9 function getSearchTerm()
10 {
11 var sPageURL = window.location.search.substring(1);
12 var sURLVariables = sPageURL.split('&');
13 for (var i = 0; i < sURLVariables.length; i++)
14 {
15 var sParameterName = sURLVariables[i].split('=');
16 if (sParameterName[0] == 'q')
17 {
18 return decodeURIComponent(sParameterName[1].replace(/\+/g, '%20'));
19 }
20 }
21 }
22
23 var index = lunr(function () {
24 this.field('title', {boost: 10});
25 this.field('text');
26 this.ref('location');
27 });
28
29 data = JSON.parse(data);
30 var documents = {};
31
32 for (var i=0; i < data.docs.length; i++){
33 var doc = data.docs[i];
34 doc.location = base_url + doc.location;
35 index.add(doc);
36 documents[doc.location] = doc;
37 }
38
39 var search = function(){
40
41 var query = document.getElementById('mkdocs-search-query').value;
42 var search_results = document.getElementById("mkdocs-search-results");
43 while (search_results.firstChild) {
44 search_results.removeChild(search_results.firstChild);
45 }
46
47 if(query === ''){
48 return;
49 }
50
51 var results = index.search(query);
52
53 if (results.length > 0){
54 for (var i=0; i < results.length; i++){
55 var result = results[i];
56 doc = documents[result.ref];
57 doc.base_url = base_url;
58 doc.summary = doc.text.substring(0, 200);
59 var html = Mustache.to_html(results_template, doc);
60 search_results.insertAdjacentHTML('beforeend', html);
61 }
62 } else {
63 search_results.insertAdjacentHTML('beforeend', "<p>No results found</p>");
64 }
65
66 if(jQuery){
67 /*
68 * We currently only automatically hide bootstrap models. This
69 * requires jQuery to work.
70 */
71 jQuery('#mkdocs_search_modal a').click(function(){
72 jQuery('#mkdocs_search_modal').modal('hide');
73 });
74 }
75
76 };
77
78 var search_input = document.getElementById('mkdocs-search-query');
79
80 var term = getSearchTerm();
81 if (term){
82 search_input.value = term;
83 search();
84 }
85
86 search_input.addEventListener("keyup", search);
87
88});