diff --git a/_includes/scripts.html b/_includes/scripts.html index e69de29bb2d1d6434b8b29ae775ad8c2e48c5391..3f2ef451a5846f116d8af07dac0d67e5c9993f48 100644 --- a/_includes/scripts.html +++ b/_includes/scripts.html @@ -0,0 +1,2 @@ +<script type="text/javascript" src="https://analytics.lcsb.uni.lu/lap/static/js/jquery.min.js"></script> +<script type="text/javascript" src="{{ "assets/js/router.js" | relative_url }}"></script> \ No newline at end of file diff --git a/_includes/search.html b/_includes/search.html new file mode 100644 index 0000000000000000000000000000000000000000..d93cfa9823bde40acdc270400a5ca66774f9e8cc --- /dev/null +++ b/_includes/search.html @@ -0,0 +1,87 @@ +<link rel="prefetch" href="index.js" /> + +<form> + <input class="search" placeholder="What are you looking for?" name="search_query" id="search_query" /><br /> + <button class="search" type="submit">Find it</button> +</form> + +<h1 id="search_header">Results of your query:</h1> + +<table id="search_results_table"> + <tbody id="search_results_tbody"> + <tr id="search_results_placeholder" style="display: none"> + <td id="search_results_placeholder_td"><h3>Please wait, search is in progress...</h3></td> + </tr> + </tbody> +</table> + +<script src="https://cdnjs.cloudflare.com/ajax/libs/lunr.js/2.3.8/lunr.min.js" integrity="sha256-34Si1Y6llMBKM3G0jQILVeoQKEwuxjbk4zGWXXMT4ps=" crossorigin="anonymous"></script> + +<script> + var el_table_body = document.getElementById('search_results_tbody'); + var el_input_query = document.getElementById('search_query'); + var el_tr_placeholder = document.getElementById('search_results_placeholder'); + var el_header = document.getElementById('search_header'); + + var url_params = new URLSearchParams(window.location.search); + var search_term = url_params.get('search_query') || ""; + + if (!!search_term.length) { + el_input_query.value = search_term; + el_header.style.display = "block"; + + el_tr_placeholder.style.display = "block"; + } + + var construct_cell = function(title, content, link) { + if (link == '#') { + return '<td><h3 class="result">' + title + '</h3><p>' + content + '</p></td>'; + } else { + return '<td><h3><a href="' + link + '">' + title + '</a></h3><p>' + content + '</p></td>'; + } + } + + var capitalize = function(text) { + return text.charAt(0).toUpperCase() + text.slice(1); + } + + var prettify = function(text) { + return text.split(':').map(capitalize).join(' » '); + } + + {% assign the_cards = site.pages | where_exp:"card","card.searchable >= 1.0" %} + + window.content = { {% for card in the_cards %} + "{{ card.url | slugify }}": { + "title": "{{ card.shortcut | xml_escape }}", + "content": {{ card.content | strip_html | strip_newlines | jsonify }}, + "url": "{{ card.url | xml_escape | relative_url }}" + } {% unless forloop.last %},{% endunless %} {% endfor %} + }; + + fetch('{{ "index.js" | relative_url }}').then(response => response.text()).then((data) => { + var the_index = JSON.parse(data); + window.search_index = lunr.Index.load(the_index); + + if (search_term != "") { + var results = window.search_index.search(search_term); + for (var i = 0; i < results.length; i++) { + var node = document.createElement("tr"); + node.innerHTML = construct_cell(prettify(window.content[results[i].ref].title), + window.content[results[i].ref].content.substring(0, 125) + '...', + window.content[results[i].ref].url); + el_table_body.appendChild(node); + } + if (results.length == 0) { + var node = document.createElement("tr"); + node.innerHTML = construct_cell("No results", + 'Sorry, we were unable to find anything that matches your query', + "#"); + el_table_body.appendChild(node); + } + } + + el_tr_placeholder.style.display = "none"; + }) + +</script> \ No newline at end of file diff --git a/index.md b/index.md index d5f879594934c5236513466558a207f5e706e514..4e2fb0c8f27d7d1854e68b856ec58b06d6a8b19b 100644 --- a/index.md +++ b/index.md @@ -4,9 +4,7 @@ title: Home order: 1 --- -<script type="text/javascript" src="https://analytics.lcsb.uni.lu/lap/static/js/jquery.min.js"></script> -<script type="text/javascript" src="{{ "assets/js/router.js" | relative_url }}"></script> - +{% include scripts.html %} # LCSB How-to Cards diff --git a/search.md b/search.md index fef4ea45b6f7a66aed6aec7d1c8735438bb750ca..a340c28936a270cafa025885e548843f84338d0e 100644 --- a/search.md +++ b/search.md @@ -5,90 +5,4 @@ permalink: /search order: 4 --- -<link rel="prefetch" href="index.js" /> - -<form> - <input class="search" placeholder="What are you looking for?" name="search_query" id="search_query" /><br /> - <button class="search" type="submit">Find it</button> -</form> - -<h1 id="search_header">Results of your query:</h1> - -<table id="search_results_table"> - <tbody id="search_results_tbody"> - <tr id="search_results_placeholder" style="display: none"> - <td id="search_results_placeholder_td"><h3>Please wait, search is in progress...</h3></td> - </tr> - </tbody> -</table> - -<script src="https://cdnjs.cloudflare.com/ajax/libs/lunr.js/2.3.8/lunr.min.js" integrity="sha256-34Si1Y6llMBKM3G0jQILVeoQKEwuxjbk4zGWXXMT4ps=" crossorigin="anonymous"></script> - -<script> - var el_table_body = document.getElementById('search_results_tbody'); - var el_input_query = document.getElementById('search_query'); - var el_tr_placeholder = document.getElementById('search_results_placeholder'); - var el_header = document.getElementById('search_header'); - - var url_params = new URLSearchParams(window.location.search); - var search_term = url_params.get('search_query') || ""; - - if (!!search_term.length) { - el_input_query.value = search_term; - el_header.style.display = "block"; - - el_tr_placeholder.style.display = "block"; - } - - var construct_cell = function(title, content, link) { - if (link == '#') { - return '<td><h3 class="result">' + title + '</h3><p>' + content + '</p></td>'; - } else { - return '<td><h3><a href="' + link + '">' + title + '</a></h3><p>' + content + '</p></td>'; - } - } - - var capitalize = function(text) { - return text.charAt(0).toUpperCase() + text.slice(1); - } - - var prettify = function(text) { - return text.split(':').map(capitalize).join(' » '); - } - - {% assign the_cards = site.pages | where_exp:"card","card.searchable >= 1.0" %} - - window.content = { {% for card in the_cards %} - "{{ card.url | slugify }}": { - "title": "{{ card.shortcut | xml_escape }}", - "content": {{ card.content | strip_html | strip_newlines | jsonify }}, - "url": "{{ card.url | xml_escape | relative_url }}" - } {% unless forloop.last %},{% endunless %} {% endfor %} - }; - - fetch('{{ "index.js" | relative_url }}').then(response => response.text()).then((data) => { - var the_index = JSON.parse(data); - window.search_index = lunr.Index.load(the_index); - - if (search_term != "") { - var results = window.search_index.search(search_term); - for (var i = 0; i < results.length; i++) { - var node = document.createElement("tr"); - node.innerHTML = construct_cell(prettify(window.content[results[i].ref].title), - window.content[results[i].ref].content.substring(0, 125) + '...', - window.content[results[i].ref].url); - el_table_body.appendChild(node); - } - if (results.length == 0) { - var node = document.createElement("tr"); - node.innerHTML = construct_cell("No results", - 'Sorry, we were unable to find anything that matches your query', - "#"); - el_table_body.appendChild(node); - } - } - - el_tr_placeholder.style.display = "none"; - }) - -</script> +{% include search.html %} \ No newline at end of file