Skip to content

Commit a2c75dc

Browse files
authored
Integrated lunr search and fixed MathJax (RoboticsKnowledgebase#81)
* Fix MathJax rendering RoboticsKnowledgebase#64 * Fixed latex math syntax * Integrated lunr search for pages * Improved search indexing
1 parent 0b0e497 commit a2c75dc

18 files changed

+4415
-82
lines changed

_config.yml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -148,6 +148,7 @@ markdown_ext: "markdown,mkdown,mkdn,mkd,md"
148148

149149
# Conversion
150150
markdown: kramdown
151+
math: true
151152
highlighter: rouge
152153
lsi: false
153154
excerpt_separator: "\n\n"
@@ -166,6 +167,11 @@ kramdown:
166167
enable_coderay: false
167168

168169

170+
# Search
171+
search: true
172+
search_full_content: true
173+
174+
169175
# Sass/SCSS
170176
sass:
171177
sass_dir: _sass

_includes/google-search.html

Lines changed: 0 additions & 12 deletions
This file was deleted.

_includes/scripts.html

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
<!-- Note that this overrides any other include file in the remote theme. So the contents of the original file in the remote theme is copied below. https://github.com/mmistakes/minimal-mistakes/blob/master/_includes/scripts.html-->
2+
3+
<!-- REMOTE SCRIPTS.HTML CONTENT -->
4+
{% if site.footer_scripts %}
5+
{% for script in site.footer_scripts %}
6+
<script src="{{ script | relative_url }}"></script>
7+
{% endfor %}
8+
{% else %}
9+
<script src="{{ '/assets/js/main.min.js' | relative_url }}"></script>
10+
<script src="https://kit.fontawesome.com/4eee35f757.js"></script>
11+
{% endif %}
12+
13+
{% if site.search == true or page.layout == "search" %}
14+
{%- assign search_provider = site.search_provider | default: "lunr" -%}
15+
{%- case search_provider -%}
16+
{%- when "lunr" -%}
17+
{% include_cached search/lunr-search-scripts.html %}
18+
{%- when "google" -%}
19+
{% include_cached search/google-search-scripts.html %}
20+
{%- when "algolia" -%}
21+
{% include_cached search/algolia-search-scripts.html %}
22+
{%- endcase -%}
23+
{% endif %}
24+
25+
{% include analytics.html %}
26+
{% include /comments-providers/scripts.html %}
27+
28+
{% if site.after_footer_scripts %}
29+
{% for script in site.after_footer_scripts %}
30+
<script src="{{ script | relative_url }}"></script>
31+
{% endfor %}
32+
{% endif %}
33+
<!-- REMOTE SCRIPTS.HTML CONTENT -->
34+
35+
<!-- Support for MathJax rendering -->
36+
{% if site.math == true %}
37+
<script type="text/javascript" async
38+
src="https://cdnjs.cloudflare.com/ajax/libs/mathjax/2.7.5/latest.js?config=TeX-MML-AM_CHTML">
39+
</script>
40+
41+
<script type="text/x-mathjax-config">
42+
MathJax.Hub.Config({
43+
extensions: ["tex2jax.js"],
44+
jax: ["input/TeX", "output/HTML-CSS"],
45+
tex2jax: {
46+
inlineMath: [ ['$','$'], ["\\(","\\)"] ],
47+
displayMath: [ ['$$','$$'], ["\\[","\\]"] ],
48+
processEscapes: true
49+
},
50+
"HTML-CSS": { availableFonts: ["TeX"] }
51+
});
52+
</script>
53+
{% endif %}
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
<!-- Including InstantSearch.js library and styling -->
2+
<script src="https://cdn.jsdelivr.net/npm/instantsearch.js@2.3.3/dist/instantsearch.min.js"></script>
3+
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/instantsearch.js@2.3.3/dist/instantsearch.min.css">
4+
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/instantsearch.js@2.3.3/dist/instantsearch-theme-algolia.min.css">
5+
6+
<script>
7+
// Instanciating InstantSearch.js with Algolia credentials
8+
const search = instantsearch({
9+
appId: '{{ site.algolia.application_id }}',
10+
apiKey: '{{ site.algolia.search_only_api_key }}',
11+
indexName: '{{ site.algolia.index_name }}',
12+
searchParameters: {
13+
restrictSearchableAttributes: [
14+
'title',
15+
'content'
16+
]
17+
}
18+
});
19+
20+
const hitTemplate = function(hit) {
21+
const url = hit.url;
22+
const title = hit._highlightResult.title.value;
23+
const content = hit._highlightResult.html.value;
24+
25+
return `
26+
<div class="list__item">
27+
<article class="archive__item" itemscope itemtype="https://schema.org/CreativeWork">
28+
<h2 class="archive__item-title" itemprop="headline"><a href="{{ site.baseurl }}${url}">${title}</a></h2>
29+
<div class="archive__item-excerpt" itemprop="description">${content}</div>
30+
</article>
31+
</div>
32+
`;
33+
}
34+
35+
// Adding searchbar and results widgets
36+
search.addWidget(
37+
instantsearch.widgets.searchBox({
38+
container: '.search-searchbar',
39+
{% unless site.algolia.powered_by == false %}poweredBy: true,{% endunless %}
40+
placeholder: '{{ site.data.ui-text[site.locale].search_placeholder_text | default: "Enter your search term..." }}'
41+
})
42+
);
43+
search.addWidget(
44+
instantsearch.widgets.hits({
45+
container: '.search-hits',
46+
templates: {
47+
item: hitTemplate
48+
}
49+
})
50+
);
51+
52+
// Starting the search
53+
search.start();
54+
</script>
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
<script>
2+
(function () {
3+
var cx = '{{ site.google.search_engine_id }}';
4+
var gcse = document.createElement('script');
5+
gcse.type = 'text/javascript';
6+
gcse.async = true;
7+
gcse.src = 'https://cse.google.com/cse.js?cx=' + cx;
8+
var s = document.getElementsByTagName('script')[0];
9+
s.parentNode.insertBefore(gcse, s);
10+
})();
11+
12+
function googleCustomSearchExecute() {
13+
var input = document.getElementById('cse-search-input-box-id');
14+
var element = google.search.cse.element.getElement('searchresults-only0');
15+
if (input.value == '') {
16+
element.clearAllResults();
17+
} else {
18+
element.execute(input.value);
19+
}
20+
return false;
21+
}
22+
23+
{% if site.google.instant_search %}
24+
$(document).ready(function () {
25+
$('input#cse-search-input-box-id').on('keyup', function () {
26+
googleCustomSearchExecute();
27+
});
28+
});
29+
{% endif %}
30+
</script>
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
{% assign lang = site.locale | slice: 0,2 | default: "en" %}
2+
{% case lang %}
3+
{% when "gr" %}
4+
{% assign lang = "gr" %}
5+
{% else %}
6+
{% assign lang = "en" %}
7+
{% endcase %}
8+
<script src="{{ '/assets/js/lunr/lunr.min.js' | relative_url }}"></script>
9+
<script src="{{ '/assets/js/lunr/lunr-store.js' | relative_url }}"></script>
10+
<script src="{{ '/assets/js/lunr/lunr-' | append: lang | append: '.js' | relative_url }}"></script>

_includes/search/search_form.html

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
<div class="search-content__inner-wrap">
2+
{%- assign search_provider = site.search_provider | default: "lunr" -%}
3+
{%- case search_provider -%}
4+
{%- when "lunr" -%}
5+
<form class="search-content__form" onkeydown="return event.key != 'Enter';">
6+
<label class="sr-only" for="search">
7+
{{ site.data.ui-text[site.locale].search_label_text | default: 'Enter your search term...' }}
8+
</label>
9+
<input type="search" id="search" class="search-input" tabindex="-1" placeholder="{{ site.data.ui-text[site.locale].search_placeholder_text | default: 'Enter your search term...' }}" />
10+
</form>
11+
<div id="results" class="results"></div>
12+
{%- when "google" -%}
13+
<form onsubmit="return googleCustomSearchExecute();" id="cse-search-box-form-id">
14+
<label class="sr-only" for="cse-search-input-box-id">
15+
{{ site.data.ui-text[site.locale].search_label_text | default: 'Enter your search term...' }}
16+
</label>
17+
<input type="search" id="cse-search-input-box-id" class="search-input" tabindex="-1" placeholder="{{ site.data.ui-text[site.locale].search_placeholder_text | default: 'Enter your search term...' }}" />
18+
</form>
19+
<div id="results" class="results">
20+
<gcse:searchresults-only></gcse:searchresults-only>
21+
</div>
22+
{%- when "algolia" -%}
23+
<div class="search-searchbar"></div>
24+
<div class="search-hits"></div>
25+
{%- endcase -%}
26+
</div>

_templates/template.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -74,8 +74,8 @@ def recover_msg(msg):
7474
This would be a good spot further explain you code snippet. Break it down for the user so they understand what is going on.
7575

7676
#### LaTex Math Support
77-
Here is an example MathJax inline rendering \\( 1/x^{2} \\), and here is a block rendering:
78-
\\[ \frac{1}{n^{2}} \\]
77+
Here is an example MathJax inline rendering $ \phi(x\|y) $ (note the additional escape for using \|), and here is a block rendering:
78+
$$ \frac{1}{n^{2}} $$
7979

8080
#### Images and Video
8181
Images and embedded video are supported.

assets/js/lunr/lunr-en.js

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
---
2+
layout: null
3+
---
4+
5+
var idx = lunr(function () {
6+
this.field('title')
7+
this.field('excerpt')
8+
this.field('categories')
9+
this.field('tags')
10+
this.ref('id')
11+
12+
this.pipeline.remove(lunr.trimmer)
13+
14+
for (var item in store) {
15+
this.add({
16+
title: store[item].title,
17+
excerpt: store[item].excerpt,
18+
categories: store[item].categories,
19+
tags: store[item].tags,
20+
id: item
21+
})
22+
}
23+
});
24+
25+
$(document).ready(function() {
26+
$('input#search').on('keyup', function () {
27+
var resultdiv = $('#results');
28+
var query = $(this).val().toLowerCase();
29+
var result =
30+
idx.query(function (q) {
31+
query.split(lunr.tokenizer.separator).forEach(function (term) {
32+
q.term(term, { boost: 100 })
33+
if(query.lastIndexOf(" ") != query.length-1){
34+
q.term(term, { usePipeline: false, wildcard: lunr.Query.wildcard.TRAILING, boost: 10 })
35+
}
36+
if (term != ""){
37+
q.term(term, { usePipeline: false, editDistance: 1, boost: 1 })
38+
}
39+
})
40+
});
41+
resultdiv.empty();
42+
resultdiv.prepend('<p class="results__found">'+result.length+' {{ site.data.ui-text[site.locale].results_found | default: "Result(s) found" }}</p>');
43+
for (var item in result) {
44+
var ref = result[item].ref;
45+
if(store[ref].teaser){
46+
var searchitem =
47+
'<div class="list__item">'+
48+
'<article class="archive__item" itemscope itemtype="https://schema.org/CreativeWork">'+
49+
'<h2 class="archive__item-title" itemprop="headline">'+
50+
'<a href="'+store[ref].url+'" rel="permalink">'+store[ref].title+'</a>'+
51+
'</h2>'+
52+
'<div class="archive__item-teaser">'+
53+
'<img src="'+store[ref].teaser+'" alt="">'+
54+
'</div>'+
55+
'<p class="archive__item-excerpt" itemprop="description">'+store[ref].excerpt.split(" ").splice(0,20).join(" ")+'...</p>'+
56+
'</article>'+
57+
'</div>';
58+
}
59+
else{
60+
var searchitem =
61+
'<div class="list__item">'+
62+
'<article class="archive__item" itemscope itemtype="https://schema.org/CreativeWork">'+
63+
'<h2 class="archive__item-title" itemprop="headline">'+
64+
'<a href="'+store[ref].url+'" rel="permalink">'+store[ref].title+'</a>'+
65+
'</h2>'+
66+
'<p class="archive__item-excerpt" itemprop="description">'+store[ref].excerpt.split(" ").splice(0,20).join(" ")+'...</p>'+
67+
'</article>'+
68+
'</div>';
69+
}
70+
resultdiv.append(searchitem);
71+
}
72+
});
73+
});

0 commit comments

Comments
 (0)