Skip to content

Commit 85a7791

Browse files
committed
fix: EGA-archive#42 fix search 404 error
1 parent 82e7bac commit 85a7791

File tree

2 files changed

+121
-0
lines changed

2 files changed

+121
-0
lines changed

docs/search.html

Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
---
2+
layout: base
3+
title: Search
4+
nav_exclude: true
5+
search_exclude: true
6+
---
7+
8+
<h1 id="search">Search Results</h1>
9+
10+
<div id="search-results" class="search-results">
11+
Enter a search query in the search box on the left.
12+
</div>
13+
14+
<!-- We only need to load the search dependencies in this page. -->
15+
<script src="https://unpkg.com/lunr/lunr.js"></script>
16+
<script type="text/javascript">
17+
"use strict";
18+
19+
// First we figure out if there is a search query and show a "searching..." animation
20+
var getQueryVariable = function(variable) {
21+
var query = window.location.search.substring(1);
22+
var vars = query.split('&');
23+
for (var i = 0; i < vars.length; i++) {
24+
var pair = vars[i].split('=');
25+
if (pair[0] === variable) {
26+
return decodeURIComponent(pair[1].replace(/\+/g, '%20'));
27+
}
28+
}
29+
};
30+
var searchResults = document.getElementById('search-results');
31+
var searchQuery = getQueryVariable('q');
32+
var dotAnimation = null;
33+
if (searchQuery) {
34+
document.getElementById('search-query').setAttribute('value', searchQuery);
35+
var dotsCount = 0;
36+
dotAnimation = setInterval(function() {
37+
dotsCount++;
38+
var dots = new Array(dotsCount % 5).join('.');
39+
searchResults.innerHTML = '<li>Searching' + dots + '</li>';
40+
}, 500);
41+
}
42+
43+
// Then we perform the search on page load
44+
window.addEventListener('load', function() {
45+
var displaySearchResults = function(results, store) {
46+
clearInterval(dotAnimation);
47+
if (results.length) {
48+
var appendString = '';
49+
for (var i = 0; i < results.length; i++) {
50+
var item = store[results[i].ref];
51+
appendString += '<li><a href="' + item.url + '"><h3>' + item.title + '</h3></a>';
52+
appendString += '<p>' + item.content.substring(0, 150) + '...</p></li>';
53+
}
54+
searchResults.innerHTML = appendString;
55+
} else {
56+
searchResults.innerHTML = '<li>Your search did not match any documents. Please make sure that all words are spelled correctly and that you\'ve selected enough categories.</li>';
57+
}
58+
};
59+
60+
if (searchQuery) {
61+
var idx = lunr(function() {
62+
this.field('id');
63+
this.field('title', { boost: 10 });
64+
this.field('author');
65+
this.field('content');
66+
});
67+
$.getJSON('/search_data.json').then(function(search_data) {
68+
var idx = lunr(function() {
69+
this.field('id');
70+
this.field('title', { boost: 10 });
71+
this.field('author');
72+
this.field('content');
73+
74+
for (var key in search_data) {
75+
this.add({
76+
'id': key,
77+
'title': search_data[key].title,
78+
'author': search_data[key].author,
79+
'content': search_data[key].content
80+
});
81+
}
82+
});
83+
84+
var results = idx.search(searchQuery);
85+
displaySearchResults(results, search_data);
86+
});
87+
}
88+
});
89+
</script>

docs/search_data.json

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
---
2+
layout: null
3+
nav_exclude: true
4+
search_exclude: true
5+
---
6+
{
7+
{%- for site_page in site.pages %}
8+
{%- unless site_page.search_exclude == true %}
9+
"{{ site_page.url | slugify }}": {
10+
"title": "{{ site_page.title | xml_escape }}",
11+
"content": {{site_page.content | markdownify | newline_to_br | replace: '<br />', ' ' | strip_html | normalize_whitespace | jsonify }},
12+
"url": " {{ site_page.url | xml_escape }}",
13+
"author": "{{ site_page.author | xml_escape }}"
14+
},
15+
{%- endunless %}
16+
{%- endfor %}
17+
{%- for site_post in site.posts %}
18+
{%- unless site_page.search_exclude == true %}
19+
"{{ site_post.url | slugify }}": {
20+
"title": "{{ site_post.title | xml_escape }}",
21+
"content": {{site_post.content | markdownify | newline_to_br | replace: '<br />', ' ' | strip_html | normalize_whitespace | jsonify }},
22+
"url": " {{ site_post.url | xml_escape }}",
23+
"author": "{{ site_post.author | xml_escape }}"
24+
}{%- unless forloop.last %},{%- endunless %}
25+
{%- endunless %}
26+
{%- else %}
27+
{%- comment %} Dirty trick: There is a comma at the end of the sites loop, so we need at least one entry after {% endcomment %}
28+
".": {
29+
".": "."
30+
}
31+
{%- endfor %}
32+
}

0 commit comments

Comments
 (0)