Skip to content

Commit

Permalink
fix: EGA-archive#42 fix search 404 error
Browse files Browse the repository at this point in the history
  • Loading branch information
ahornos committed May 15, 2023
1 parent 82e7bac commit 85a7791
Show file tree
Hide file tree
Showing 2 changed files with 121 additions and 0 deletions.
89 changes: 89 additions & 0 deletions docs/search.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
---
layout: base
title: Search
nav_exclude: true
search_exclude: true
---

<h1 id="search">Search Results</h1>

<div id="search-results" class="search-results">
Enter a search query in the search box on the left.
</div>

<!-- We only need to load the search dependencies in this page. -->
<script src="https://unpkg.com/lunr/lunr.js"></script>
<script type="text/javascript">
"use strict";

// First we figure out if there is a search query and show a "searching..." animation
var getQueryVariable = function(variable) {
var query = window.location.search.substring(1);
var vars = query.split('&');
for (var i = 0; i < vars.length; i++) {
var pair = vars[i].split('=');
if (pair[0] === variable) {
return decodeURIComponent(pair[1].replace(/\+/g, '%20'));
}
}
};
var searchResults = document.getElementById('search-results');
var searchQuery = getQueryVariable('q');
var dotAnimation = null;
if (searchQuery) {
document.getElementById('search-query').setAttribute('value', searchQuery);
var dotsCount = 0;
dotAnimation = setInterval(function() {
dotsCount++;
var dots = new Array(dotsCount % 5).join('.');
searchResults.innerHTML = '<li>Searching' + dots + '</li>';
}, 500);
}

// Then we perform the search on page load
window.addEventListener('load', function() {
var displaySearchResults = function(results, store) {
clearInterval(dotAnimation);
if (results.length) {
var appendString = '';
for (var i = 0; i < results.length; i++) {
var item = store[results[i].ref];
appendString += '<li><a href="' + item.url + '"><h3>' + item.title + '</h3></a>';
appendString += '<p>' + item.content.substring(0, 150) + '...</p></li>';
}
searchResults.innerHTML = appendString;
} else {
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>';
}
};

if (searchQuery) {
var idx = lunr(function() {
this.field('id');
this.field('title', { boost: 10 });
this.field('author');
this.field('content');
});
$.getJSON('/search_data.json').then(function(search_data) {
var idx = lunr(function() {
this.field('id');
this.field('title', { boost: 10 });
this.field('author');
this.field('content');

for (var key in search_data) {
this.add({
'id': key,
'title': search_data[key].title,
'author': search_data[key].author,
'content': search_data[key].content
});
}
});

var results = idx.search(searchQuery);
displaySearchResults(results, search_data);
});
}
});
</script>
32 changes: 32 additions & 0 deletions docs/search_data.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
---
layout: null
nav_exclude: true
search_exclude: true
---
{
{%- for site_page in site.pages %}
{%- unless site_page.search_exclude == true %}
"{{ site_page.url | slugify }}": {
"title": "{{ site_page.title | xml_escape }}",
"content": {{site_page.content | markdownify | newline_to_br | replace: '<br />', ' ' | strip_html | normalize_whitespace | jsonify }},
"url": " {{ site_page.url | xml_escape }}",
"author": "{{ site_page.author | xml_escape }}"
},
{%- endunless %}
{%- endfor %}
{%- for site_post in site.posts %}
{%- unless site_page.search_exclude == true %}
"{{ site_post.url | slugify }}": {
"title": "{{ site_post.title | xml_escape }}",
"content": {{site_post.content | markdownify | newline_to_br | replace: '<br />', ' ' | strip_html | normalize_whitespace | jsonify }},
"url": " {{ site_post.url | xml_escape }}",
"author": "{{ site_post.author | xml_escape }}"
}{%- unless forloop.last %},{%- endunless %}
{%- endunless %}
{%- else %}
{%- comment %} Dirty trick: There is a comma at the end of the sites loop, so we need at least one entry after {% endcomment %}
".": {
".": "."
}
{%- endfor %}
}

0 comments on commit 85a7791

Please sign in to comment.