Skip to content

Commit c7a7203

Browse files
committed
Add a search option
Will be useful later for the (static) blog and wiki based on https://chodounsky.com/2015/05/14/full-text-search-on-static-website/
1 parent 4951f22 commit c7a7203

39 files changed

+259
-35
lines changed

README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,9 @@ Included are the following 3rd party software:
8888
* Magnific Popup - http://dimsemenov.com/plugins/magnific-popup/ -
8989
Copyright (c) 2014-2016 Dmitry Semenov, provided under the MIT license
9090

91+
* lunr - https://lunrjs.com - Copyright (c) 2013 by Oliver Nightingale,
92+
provided under the MIT license
93+
9194
* Favicons have been processed by https://realfavicongenerator.net
9295

9396
* IRC contact page is provided by https://kiwiirc.com

Rules

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,9 @@ compile '/**/*.scss' do
1919
write item.identifier.without_ext + '.css'
2020
end
2121

22-
compile '/sitemap.xml' do
22+
compile '/{sitemap.xml,js/search-index.js}' do
2323
filter :erb
24-
write '/sitemap.xml'
24+
write item.identifier
2525
end
2626

2727
compile '/htaccess.txt' do

content/about/index.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
---
22
title: About EasyRPG
33
priority: 0.8
4-
menu_weight: 5
4+
menu_weight: 10
55
---
66
<div class="info" markdown="1">
77

content/about/website.md

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,8 @@ title: About the EasyRPG Homepage
3030

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

3536
### 3rd party software
3637

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

content/contribute/artists.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
---
2-
title: How to Contribute
2+
title: "How to Contribute: Artists"
33
menu_weight: 3
44
---
55
<div class="info" markdown="1">

content/contribute/index.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
---
2-
title: How to Contribute
2+
title: "How to Contribute: Introduction"
33
priority: 0.8
4-
menu_weight: 0
4+
menu_weight: 1
55
---
66
<div class="info" markdown="1">
77

content/contribute/programmers.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
---
2-
title: How to Contribute
2+
title: "How to Contribute: Programmers"
33
menu_weight: 4
44
---
55
<div class="info" markdown="1">

content/contribute/testers.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
---
2-
title: How to Contribute
2+
title: "How to Contribute: Testers"
33
menu_weight: 2
44
---
55
<div class="info" markdown="1">

content/contribute/translators.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
---
2-
title: How to Contribute
2+
title: "How to Contribute: Translators"
33
menu_weight: 1
44
---
55
<div class="info" markdown="1">

content/css/easyrpg.scss

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -245,6 +245,53 @@ $darkgrey: #202020;
245245
}
246246
}
247247

248+
/* Search box and results */
249+
form#search-form {
250+
margin: 1em 0;
251+
252+
input {
253+
border: 2px solid $normalgreen;
254+
margin: 4px 2px;
255+
padding: 10px 12px;
256+
border-radius: 5px;
257+
}
258+
input[type=text] {
259+
border-collapse: collapse;
260+
width: 300px;
261+
}
262+
input[type=submit] {
263+
background-color: $lightgrey;
264+
color: black;
265+
cursor: pointer;
266+
}
267+
}
268+
ul#search-results {
269+
// entry height
270+
$eheight: 32px;
271+
272+
margin-top: -1px;
273+
margin-bottom: 0;
274+
padding: 0 0.5em;
275+
276+
li {
277+
display: inline-block;
278+
line-height: $eheight;
279+
vertical-align: middle;
280+
white-space: nowrap;
281+
282+
a {
283+
display: inline-block;
284+
height: $eheight;
285+
padding: 0 1em;
286+
text-decoration: none;
287+
288+
&:hover {
289+
color: $normalgreen;
290+
}
291+
}
292+
}
293+
}
294+
248295
/* Footer */
249296
#footer {
250297
font-size: 11px;

content/editor/downloads.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
---
2-
title: EasyRPG Editor
2+
title: "EasyRPG Editor: Downloads"
33
menu_weight: 2
44
---
55
<div class="info" markdown="1">

content/editor/index.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
---
2-
title: EasyRPG Editor
2+
title: "EasyRPG Editor: Overview"
33
priority: 0.7
44
menu_name: EasyRPG Editor
5-
menu_weight: 1
5+
menu_weight: 6
66
---
77
<div class="info" markdown="1">
88

content/editor/media.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
---
2-
title: EasyRPG Editor
2+
title: "EasyRPG Editor: Media"
33
menu_weight: 1
44
---
55
<div class="info" markdown="1">

content/editor/progress.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
---
2-
title: EasyRPG Editor
2+
title: "EasyRPG Editor: Progress"
33
menu_weight: 3
44
---
55
<div class="info" markdown="1">

content/index.html

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
---
22
has_spotlight: true
3+
title: EasyRPG Homepage
34
---
45
<div id="spotlight">
56
<div class="maxwidth">

content/js/search-index.js

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
var pages = <%= make_search_index %>
2+
3+
document.addEventListener("DOMContentLoaded", function(event) {
4+
var input = document.getElementById("search-input")
5+
var form = document.getElementById("search-form")
6+
var resultsContainer = document.getElementById("search-results")
7+
var resultsHeading = document.getElementById("search-heading")
8+
9+
var searchIndex = lunr(function () {
10+
this.field('title', { boost: 4 })
11+
this.field('tags', { boost: 2 })
12+
this.field('body')
13+
this.ref('id')
14+
// add all pages to search
15+
pages.forEach(function(page) { this.add(page) }, this)
16+
})
17+
18+
var clearResults = function() {
19+
resultsContainer.innerHTML = ""
20+
resultsHeading.innerHTML = ""
21+
}
22+
23+
var search = function(query) {
24+
clearResults();
25+
26+
// find
27+
results = searchIndex.search(query).map(function(id) {
28+
for (var i = 0; i < pages.length; i++) {
29+
if (pages[i].id === id.ref)
30+
return pages[i]
31+
}
32+
})
33+
34+
// render
35+
resultsHeading.innerHTML = results.length + " results found:"
36+
results.forEach(function(result) {
37+
var li = document.createElement("li")
38+
var a = document.createElement("a")
39+
a.href = result.id
40+
a.innerHTML = result.title
41+
li.appendChild(a)
42+
resultsContainer.appendChild(li)
43+
})
44+
}
45+
46+
var searchQueryFromUrl = function() {
47+
var q;
48+
location.search.substr(1).split("&").forEach(function(item) {
49+
if (item[0] === "q")
50+
q = decodeURIComponent(item.substring(2))
51+
})
52+
return q;
53+
}
54+
55+
form.addEventListener("submit", function(e) {
56+
var v = input.value
57+
history.pushState({ q: v }, "", "?q=" + encodeURIComponent(v))
58+
search(v)
59+
e.preventDefault()
60+
})
61+
62+
window.onpopstate = function(event) {
63+
if (event.state !== null) {
64+
input.value = event.state.q
65+
search(input.value)
66+
} else {
67+
// clear
68+
input.value = ""
69+
clearResults()
70+
}
71+
}
72+
73+
if (searchQueryFromUrl() !== undefined) {
74+
input.value = searchQueryFromUrl()
75+
search(input.value)
76+
}
77+
})

content/js/vendor/lunr.min.js

Lines changed: 6 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

content/player/downloads.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
---
2-
title: EasyRPG Player
2+
title: "EasyRPG Player: Downloads"
33
menu_weight: 3
44
---
55
<% # The following variables can be quite handy ;)

content/player/guide.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
---
2-
title: "EasyRPG Player"
2+
title: "EasyRPG Player: Guide"
33
menu_weight: 1
44
---
55
<div class="info" markdown="1">

content/player/guide/game_translation.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
---
2-
title: "EasyRPG Player"
2+
title: "EasyRPG Player: Guide game translation"
33
---
44

55
<div class="info" markdown="1">

content/player/guide/webplayer.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
---
2-
title: "EasyRPG Player"
2+
title: "EasyRPG Player: Guide webplayer"
33
---
44

55
<div class="info" markdown="1">

content/player/index.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
---
2-
title: EasyRPG Player
2+
title: "EasyRPG Player: Overview"
33
priority: 0.7
44
menu_name: EasyRPG Player
5-
menu_weight: 4
5+
menu_weight: 8
66
---
77
<div class="info" markdown="1">
88

content/player/media.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
---
2-
title: EasyRPG Player
2+
title: "EasyRPG Player: Media"
33
menu_weight: 2
44
---
55
<div class="info" markdown="1">

content/player/progress.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
---
2-
title: EasyRPG Player
2+
title: "EasyRPG Player: Progress"
33
menu_weight: 4
44
---
55
<div class="info" markdown="1">

content/rtp-replacement/downloads.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
---
2-
title: RTP Replacement
2+
title: "RTP Replacement: Downloads"
33
menu_weight: 2
44
---
55
<div class="info" markdown="1">

content/rtp-replacement/index.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
---
2-
title: RTP Replacement
2+
title: "RTP Replacement: Overview"
33
priority: 0.7
44
menu_name: RTP Replacement
5-
menu_weight: 3
5+
menu_weight: 4
66
---
77
<div class="info" markdown="1">
88

content/rtp-replacement/media.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
---
2-
title: RTP Replacement
2+
title: "RTP Replacement: Media"
33
menu_weight: 1
44
---
55
<div class="info" markdown=1>

content/rtp-replacement/progress.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
---
2-
title: RTP Replacement
2+
title: "RTP Replacement: Progress"
33
menu_weight: 3
44
---
55
<div class="info" markdown="1">

content/search/index.html

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
---
2+
title: Search on this site
3+
menu_name: 🔍 Site search
4+
menu_weight: -1
5+
---
6+
<form action="/search/" id="search-form" class="search" method="get" >
7+
<input name="q" type="text" id="search-input" placeholder="enter search term..." autofocus required>
8+
<input type="submit" value="Search">
9+
</form>
10+
<h2 id="search-heading"></h2>
11+
<ul id="search-results"></ul>
12+
<script src="/js/vendor/lunr.min.js"></script>
13+
<script src="/js/search-index.js"></script>

content/tools/downloads.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
---
2-
title: EasyRPG Tools
2+
title: "EasyRPG Tools: Downloads"
33
menu_weight: 2
44
---
55
<div class="info" markdown="1">

content/tools/index.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
---
2-
title: EasyRPG Tools
2+
title: "EasyRPG Tools: Overview"
33
priority: 0.7
4-
menu_weight: 2
4+
menu_weight: 3
55
---
66
<div class="info" markdown="1">
77

content/tools/media.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
---
2-
title: EasyRPG Tools
2+
title: "EasyRPG Tools: Media"
33
menu_weight: 1
44
---
55
<div class="info" markdown=1>

0 commit comments

Comments
 (0)