Skip to content

Commit

Permalink
Add a search option
Browse files Browse the repository at this point in the history
Will be useful later for the (static) blog and wiki

based on https://chodounsky.com/2015/05/14/full-text-search-on-static-website/
  • Loading branch information
carstene1ns committed Apr 18, 2022
1 parent bf14156 commit 4f17730
Show file tree
Hide file tree
Showing 39 changed files with 259 additions and 35 deletions.
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,9 @@ Included are the following 3rd party software:
* Magnific Popup - http://dimsemenov.com/plugins/magnific-popup/ -
Copyright (c) 2014-2016 Dmitry Semenov, provided under the MIT license

* lunr - https://lunrjs.com - Copyright (c) 2013 by Oliver Nightingale,
provided under the MIT license

* Favicons have been processed by https://realfavicongenerator.net

* IRC contact page is provided by https://kiwiirc.com
Expand Down
4 changes: 2 additions & 2 deletions Rules
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,9 @@ compile '/**/*.scss' do
write item.identifier.without_ext + '.css'
end

compile '/sitemap.xml' do
compile '/{sitemap.xml,js/search-index.js}' do
filter :erb
write '/sitemap.xml'
write item.identifier
end

compile '/htaccess.txt' do
Expand Down
2 changes: 1 addition & 1 deletion content/about/index.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
---
title: About EasyRPG
priority: 0.8
menu_weight: 5
menu_weight: 10
---
<div class="info" markdown="1">

Expand Down
5 changes: 4 additions & 1 deletion content/about/website.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,8 @@ title: About the EasyRPG Homepage

- Nanoc static site generator - <https://nanoc.app>
- kramdown markdown superset converter - <https://kramdown.gettalong.org/>
- Sass <https://sass-lang.com>
- Sass - <https://sass-lang.com>
- lunr client search engine - <https://lunrjs.com>

### 3rd party software

Expand All @@ -40,6 +41,8 @@ Included are the following 3rd party software:
contributors, provided under the MIT license
- Magnific Popup - <https://dimsemenov.com/plugins/magnific-popup/>
\- Copyright © 2014-2016 Dmitry Semenov, provided under the MIT license
- lunr - Copyright © 2013 by Oliver Nightingale, provided under the
MIT license
- Favicons have been processed by <https://realfavicongenerator.net>
- IRC contact page is provided by <https://kiwiirc.com>

Expand Down
2 changes: 1 addition & 1 deletion content/contribute/artists.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
---
title: How to Contribute
title: "How to Contribute: Artists"
menu_weight: 3
---
<div class="info" markdown="1">
Expand Down
4 changes: 2 additions & 2 deletions content/contribute/index.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
---
title: How to Contribute
title: "How to Contribute: Introduction"
priority: 0.8
menu_weight: 0
menu_weight: 1
---
<div class="info" markdown="1">

Expand Down
2 changes: 1 addition & 1 deletion content/contribute/programmers.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
---
title: How to Contribute
title: "How to Contribute: Programmers"
menu_weight: 4
---
<div class="info" markdown="1">
Expand Down
2 changes: 1 addition & 1 deletion content/contribute/testers.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
---
title: How to Contribute
title: "How to Contribute: Testers"
menu_weight: 2
---
<div class="info" markdown="1">
Expand Down
2 changes: 1 addition & 1 deletion content/contribute/translators.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
---
title: How to Contribute
title: "How to Contribute: Translators"
menu_weight: 1
---
<div class="info" markdown="1">
Expand Down
47 changes: 47 additions & 0 deletions content/css/easyrpg.scss
Original file line number Diff line number Diff line change
Expand Up @@ -245,6 +245,53 @@ $darkgrey: #202020;
}
}

/* Search box and results */
form#search-form {
margin: 1em 0;

input {
border: 2px solid $normalgreen;
margin: 4px 2px;
padding: 10px 12px;
border-radius: 5px;
}
input[type=text] {
border-collapse: collapse;
width: 300px;
}
input[type=submit] {
background-color: $lightgrey;
color: black;
cursor: pointer;
}
}
ul#search-results {
// entry height
$eheight: 32px;

margin-top: -1px;
margin-bottom: 0;
padding: 0 0.5em;

li {
display: inline-block;
line-height: $eheight;
vertical-align: middle;
white-space: nowrap;

a {
display: inline-block;
height: $eheight;
padding: 0 1em;
text-decoration: none;

&:hover {
color: $normalgreen;
}
}
}
}

/* Footer */
#footer {
font-size: 11px;
Expand Down
2 changes: 1 addition & 1 deletion content/editor/downloads.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
---
title: EasyRPG Editor
title: "EasyRPG Editor: Downloads"
menu_weight: 2
---
<div class="info" markdown="1">
Expand Down
4 changes: 2 additions & 2 deletions content/editor/index.md
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
---
title: EasyRPG Editor
title: "EasyRPG Editor: Overview"
priority: 0.7
menu_name: EasyRPG Editor
menu_weight: 1
menu_weight: 6
---
<div class="info" markdown="1">

Expand Down
2 changes: 1 addition & 1 deletion content/editor/media.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
---
title: EasyRPG Editor
title: "EasyRPG Editor: Media"
menu_weight: 1
---
<div class="info" markdown="1">
Expand Down
2 changes: 1 addition & 1 deletion content/editor/progress.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
---
title: EasyRPG Editor
title: "EasyRPG Editor: Progress"
menu_weight: 3
---
<div class="info" markdown="1">
Expand Down
1 change: 1 addition & 0 deletions content/index.html
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
---
has_spotlight: true
title: EasyRPG Homepage
---
<div id="spotlight">
<div class="maxwidth">
Expand Down
77 changes: 77 additions & 0 deletions content/js/search-index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
var pages = <%= make_search_index %>

document.addEventListener("DOMContentLoaded", function(event) {
var input = document.getElementById("search-input")
var form = document.getElementById("search-form")
var resultsContainer = document.getElementById("search-results")
var resultsHeading = document.getElementById("search-heading")

var searchIndex = lunr(function () {
this.field('title', { boost: 4 })
this.field('tags', { boost: 2 })
this.field('body')
this.ref('id')
// add all pages to search
pages.forEach(function(page) { this.add(page) }, this)
})

var clearResults = function() {
resultsContainer.innerHTML = ""
resultsHeading.innerHTML = ""
}

var search = function(query) {
clearResults();

// find
results = searchIndex.search(query).map(function(id) {
for (var i = 0; i < pages.length; i++) {
if (pages[i].id === id.ref)
return pages[i]
}
})

// render
resultsHeading.innerHTML = results.length + " results found:"
results.forEach(function(result) {
var li = document.createElement("li")
var a = document.createElement("a")
a.href = result.id
a.innerHTML = result.title
li.appendChild(a)
resultsContainer.appendChild(li)
})
}

var searchQueryFromUrl = function() {
var q;
location.search.substr(1).split("&").forEach(function(item) {
if (item[0] === "q")
q = decodeURIComponent(item.substring(2))
})
return q;
}

form.addEventListener("submit", function(e) {
var v = input.value
history.pushState({ q: v }, "", "?q=" + encodeURIComponent(v))
search(v)
e.preventDefault()
})

window.onpopstate = function(event) {
if (event.state !== null) {
input.value = event.state.q
search(input.value)
} else {
// clear
input.value = ""
clearResults()
}
}

if (searchQueryFromUrl() !== undefined) {
input.value = searchQueryFromUrl()
search(input.value)
}
})
6 changes: 6 additions & 0 deletions content/js/vendor/lunr.min.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion content/player/downloads.html
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
---
title: EasyRPG Player
title: "EasyRPG Player: Downloads"
menu_weight: 3
---
<% # The following variables can be quite handy ;)
Expand Down
2 changes: 1 addition & 1 deletion content/player/guide.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
---
title: "EasyRPG Player"
title: "EasyRPG Player: Guide"
menu_weight: 1
---
<div class="info" markdown="1">
Expand Down
2 changes: 1 addition & 1 deletion content/player/guide/game_translation.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
---
title: "EasyRPG Player"
title: "EasyRPG Player: Guide game translation"
---

<div class="info" markdown="1">
Expand Down
2 changes: 1 addition & 1 deletion content/player/guide/webplayer.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
---
title: "EasyRPG Player"
title: "EasyRPG Player: Guide webplayer"
---

<div class="info" markdown="1">
Expand Down
4 changes: 2 additions & 2 deletions content/player/index.md
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
---
title: EasyRPG Player
title: "EasyRPG Player: Overview"
priority: 0.7
menu_name: EasyRPG Player
menu_weight: 4
menu_weight: 8
---
<div class="info" markdown="1">

Expand Down
2 changes: 1 addition & 1 deletion content/player/media.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
---
title: EasyRPG Player
title: "EasyRPG Player: Media"
menu_weight: 2
---
<div class="info" markdown="1">
Expand Down
2 changes: 1 addition & 1 deletion content/player/progress.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
---
title: EasyRPG Player
title: "EasyRPG Player: Progress"
menu_weight: 4
---
<div class="info" markdown="1">
Expand Down
2 changes: 1 addition & 1 deletion content/rtp-replacement/downloads.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
---
title: RTP Replacement
title: "RTP Replacement: Downloads"
menu_weight: 2
---
<div class="info" markdown="1">
Expand Down
4 changes: 2 additions & 2 deletions content/rtp-replacement/index.md
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
---
title: RTP Replacement
title: "RTP Replacement: Overview"
priority: 0.7
menu_name: RTP Replacement
menu_weight: 3
menu_weight: 4
---
<div class="info" markdown="1">

Expand Down
2 changes: 1 addition & 1 deletion content/rtp-replacement/media.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
---
title: RTP Replacement
title: "RTP Replacement: Media"
menu_weight: 1
---
<div class="info" markdown=1>
Expand Down
2 changes: 1 addition & 1 deletion content/rtp-replacement/progress.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
---
title: RTP Replacement
title: "RTP Replacement: Progress"
menu_weight: 3
---
<div class="info" markdown="1">
Expand Down
13 changes: 13 additions & 0 deletions content/search/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
---
title: Search on this site
menu_name: 🔍 Site search
menu_weight: -1
---
<form action="/search/" id="search-form" class="search" method="get" >
<input name="q" type="text" id="search-input" placeholder="enter search term..." autofocus required>
<input type="submit" value="Search">
</form>
<h2 id="search-heading"></h2>
<ul id="search-results"></ul>
<script src="/js/vendor/lunr.min.js"></script>
<script src="/js/search-index.js"></script>
2 changes: 1 addition & 1 deletion content/tools/downloads.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
---
title: EasyRPG Tools
title: "EasyRPG Tools: Downloads"
menu_weight: 2
---
<div class="info" markdown="1">
Expand Down
4 changes: 2 additions & 2 deletions content/tools/index.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
---
title: EasyRPG Tools
title: "EasyRPG Tools: Overview"
priority: 0.7
menu_weight: 2
menu_weight: 3
---
<div class="info" markdown="1">

Expand Down
2 changes: 1 addition & 1 deletion content/tools/media.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
---
title: EasyRPG Tools
title: "EasyRPG Tools: Media"
menu_weight: 1
---
<div class="info" markdown=1>
Expand Down
Loading

0 comments on commit 4f17730

Please sign in to comment.