Skip to content

Commit 2b6727b

Browse files
authored
Search feature (#74)
* Updated configured production URL to drop unneeded ending slash. * Updated ATOM feed to remove extra slash from self link. * Updated reference to ~atom_path` and `rss_path`. * Updated sitemap to sort pages and posts alphabetically. * Added a JSON search feed file. * Added search page with JavaScript search. * Updated site template to add link for search page to header section.
1 parent 11fd95d commit 2b6727b

File tree

8 files changed

+131
-35
lines changed

8 files changed

+131
-35
lines changed

_config.yml

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -51,14 +51,13 @@ exclude: [
5151
]
5252

5353
# The production_url is only used when full-domain names are needed
54-
# such as sitemap.txt
54+
# such as sitemap.txt, atom.xml, rss.xml, search.json
5555
# Most places will/should use BASE_PATH to make the urls
5656
#
5757
# If you have set a CNAME (pages.github.com) set your custom domain here.
5858
# Else if you are pushing to username.github.io, replace with your username.
5959
# Finally if you are pushing to a GitHub project page, include the project name at the end.
60-
#
61-
production_url : https://bpkg.sh/
60+
production_url : https://bpkg.sh
6261

6362
# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
6463
# Jekyll-Bootstrap specific configurations

_includes/themes/the-program/default.html

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,9 +27,12 @@
2727
<ul>
2828
<li class="logo"><a href="/">{{ site.title }}</a></li>
2929

30-
<li class="archive"><a href="/packages/name">packages by name</a></li>
31-
<li class="archive"><a href="/packages/tag">packages by tag</a></li>
32-
<li class="archive"><a href="/packages/category">packages by category</a></li>
30+
<li class="archive"><a href="/packages/name">Packages by Name</a></li>
31+
<li class="archive"><a href="/packages/tag">Packages by Tag</a></li>
32+
<li class="archive"><a href="/packages/category">Packages by Category</a></li>
33+
34+
<li class="archive">&nbsp;</li>
35+
<li class="archive"><a href="/search">Search Packages</a></li>
3336

3437
<!-- github buttons -->
3538
<li class="forkme">

assets/js/search.js

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
class PackageData {
2+
title;
3+
date;
4+
author;
5+
description;
6+
category;
7+
tags;
8+
url;
9+
10+
constructor(data) {
11+
this.title = data.title;
12+
this.date = data.date;
13+
this.author = data.author;
14+
this.description = data.description;
15+
this.category = data.category;
16+
this.tags = data.tags;
17+
this.url = data.url;
18+
}
19+
}
20+
21+
document.addEventListener('DOMContentLoaded', function (event) {
22+
const search = document.getElementById('search');
23+
const results = document.getElementById('results');
24+
let generated = '';
25+
let data = [];
26+
let search_term = '';
27+
28+
fetch('/feed/search.json')
29+
.then((response) => response.json())
30+
.then((data_server) => {
31+
generated = data_server.generated;
32+
data = data_server.posts.map(post => new PackageData(post));
33+
});
34+
35+
search.addEventListener('input', (event) => {
36+
search_term = event.target.value.toLowerCase();
37+
showList();
38+
});
39+
40+
const showList = () => {
41+
results.innerHTML = '';
42+
43+
if (search_term.length == 0) return;
44+
45+
const match = new RegExp(`${search_term}`, 'gi');
46+
let result = data.filter((name) => match.test(name.title));
47+
48+
if (result.length == 0) {
49+
const li = document.createElement('li');
50+
li.innerHTML = `Zero results found`;
51+
results.appendChild(li);
52+
}
53+
54+
result.forEach((package) => {
55+
const li = document.createElement('li');
56+
li.innerHTML = `<a href="${package.url}">${package.title}</a>`;
57+
results.appendChild(li);
58+
});
59+
};
60+
61+
});

feed/atom.xml

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,23 +5,21 @@ title : Atom Feed
55
<?xml version="1.0" encoding="utf-8"?>
66
<feed xmlns="http://www.w3.org/2005/Atom">
77
<title>{{ site.title }}</title>
8-
<link href="{{ site.production_url }}/{{ site.atom_path }}" rel="self"/>
8+
<link href="{{ site.production_url }}{{ site.JB.atom_path }}" rel="self"/>
99
<link href="{{ site.production_url }}"/>
10-
<updated>{{ site.time | date_to_xmlschema }}</updated>
1110
<id>{{ site.production_url }}</id>
11+
<updated>{{ site.time | date_to_xmlschema }}</updated>
1212
<author>
1313
<name>{{ site.author.name }}</name>
1414
<email>{{ site.author.email }}</email>
1515
</author>
16-
1716
{% for post in site.posts %}
1817
<entry>
1918
<title>{{ post.title }}</title>
2019
<link href="{{ site.production_url }}{{ post.url }}"/>
21-
<updated>{{ post.date | date_to_xmlschema }}</updated>
2220
<id>{{ site.production_url }}{{ post.id }}</id>
21+
<updated>{{ post.date | date_to_xmlschema }}</updated>
2322
<content type="html">{{ post.content | xml_escape }}</content>
2423
</entry>
2524
{% endfor %}
26-
2725
</feed>

feed/rss.xml

Lines changed: 18 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -4,24 +4,22 @@ title : RSS Feed
44

55
<?xml version="1.0" encoding="UTF-8" ?>
66
<rss version="2.0">
7-
<channel>
8-
<title>{{ site.title }}</title>
9-
<description>{{ site.title }} - {{ site.author.name }}</description>
10-
<link>{{ site.production_url }}{{ site.rss_path }}</link>
11-
<link>{{ site.production_url }}</link>
12-
<lastBuildDate>{{ site.time | date_to_xmlschema }}</lastBuildDate>
13-
<pubDate>{{ site.time | date_to_xmlschema }}</pubDate>
14-
<ttl>1800</ttl>
15-
16-
{% for post in site.posts %}
17-
<item>
18-
<title>{{ post.title }}</title>
19-
<description>{{ post.content | xml_escape }}</description>
20-
<link>{{ site.production_url }}{{ post.url }}</link>
21-
<guid>{{ site.production_url }}{{ post.id }}</guid>
22-
<pubDate>{{ post.date | date_to_xmlschema }}</pubDate>
23-
</item>
24-
{% endfor %}
25-
26-
</channel>
7+
<channel>
8+
<title>{{ site.title }}</title>
9+
<description>{{ site.title }} - {{ site.author.name }}</description>
10+
<link>{{ site.production_url }}{{ site.JB.rss_path }}</link>
11+
<link>{{ site.production_url }}</link>
12+
<pubDate>{{ site.time | date_to_xmlschema }}</pubDate>
13+
<lastBuildDate>{{ site.time | date_to_xmlschema }}</lastBuildDate>
14+
<ttl>1800</ttl>
15+
{% for post in site.posts %}
16+
<item>
17+
<title>{{ post.title }}</title>
18+
<link>{{ site.production_url }}{{ post.url }}</link>
19+
<guid>{{ site.production_url }}{{ post.id }}</guid>
20+
<pubDate>{{ post.date | date_to_xmlschema }}</pubDate>
21+
<description>{{ post.content | xml_escape }}</description>
22+
</item>
23+
{% endfor %}
24+
</channel>
2725
</rss>

feed/search.json

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
---
2+
title : JavaScript Search
3+
permalink : '/feed/search.json'
4+
---
5+
6+
{
7+
"generated":"{{ site.time | date_to_xmlschema }}",
8+
"posts":[
9+
{% for post in site.posts %}{
10+
"title":"{{ post.title }}",
11+
"date":"{{ post.date | date: '%Y-%m-%d' }}",
12+
"author":"{{ post.author }}",
13+
"description":"{{ post.description }}",
14+
"category":"{{ post.category }}",
15+
"tags":{{ post.tags | jsonify }},
16+
"repository":"{{ post.repository }}",
17+
"url":"{{ site.production_url }}{{ post.url }}"
18+
}{% if forloop.last == false %},{% endif %}
19+
{% endfor %}
20+
]
21+
}

search/index.html

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
---
2+
layout: page
3+
title: Search
4+
header: Search Packages
5+
group: navigation
6+
permalink: /search/
7+
---
8+
9+
{% include JB/setup %}
10+
11+
<div>
12+
<input id="search" type="search" autofocus autocomplete="off" placeholder="Search for packages" />
13+
<ul id="results">
14+
</ul>
15+
</div>
16+
<script type="text/javascript" src="{{ BASE_PATH }}/assets/js/search.js" defer></script>

sitemap.txt

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
title : Sitemap
44
---
55

6-
{% for page in site.pages %}
7-
{{site.production_url}}{{ page.url }}{% endfor %}
8-
{% for post in site.posts %}
9-
{{site.production_url}}{{ post.url }}{% endfor %}
6+
{% assign pages = site.pages | sort:"url" %}{% for page in pages %}{{site.production_url}}{{ page.url }}
7+
{% endfor %}
8+
{% assign posts = site.posts | sort:"url" %}{% for post in posts %}{{site.production_url}}{{ post.url }}
9+
{% endfor %}

0 commit comments

Comments
 (0)