Skip to content

Commit 85a3d63

Browse files
authored
Merge pull request #92 from skipgu/91-add-search-to-blogs
Add search to blogs
2 parents 2766047 + 2942ee0 commit 85a3d63

3 files changed

Lines changed: 40 additions & 3 deletions

File tree

_includes/blog-filters.html

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,11 @@
11
{% assign postsByYear = site.posts | group_by_exp: 'post', 'post.date | date: "%Y"' %}
22
{% assign all_tags = site.posts | map: 'tags' | flatten | uniq | sort %}
33

4-
<h2>Filters</h2>
4+
<h3>Search & Filters</h3>
5+
<div class="filter-group">
6+
<span class="filter-label">Title:</span>
7+
<input type="text" class="filter-search" placeholder="Search blog posts...">
8+
</div>
59
<div class="filter-bar">
610
<div class="filter-group" data-filter-type="year">
711
<span class="filter-label">Year:</span>

assets/js/filter.js

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ document.addEventListener('DOMContentLoaded', function () {
66
let groups = filterBar.querySelectorAll('.filter-group');
77
let entries = document.querySelectorAll('.entry');
88
let noResults = document.querySelector('.no-results');
9+
let searchInput = document.querySelector('.filter-search');
10+
let searchTerm = '';
911
let selected = {};
1012

1113
function applyFilters() {
@@ -14,6 +16,11 @@ document.addEventListener('DOMContentLoaded', function () {
1416
let entryYear = entry.getAttribute('data-year');
1517
let entryTags = (entry.getAttribute('data-tags') || '').split(',');
1618
let show = true;
19+
20+
let title = entry.querySelector('.entry-title').textContent.toLowerCase();
21+
if (searchTerm && title.indexOf(searchTerm) === -1) {
22+
show = false;
23+
}
1724

1825
groups.forEach(function (group) {
1926
if (!show) return;
@@ -57,4 +64,11 @@ document.addEventListener('DOMContentLoaded', function () {
5764
});
5865
});
5966
});
67+
68+
if (searchInput) {
69+
searchInput.addEventListener('input', function () {
70+
searchTerm = this.value.toLowerCase();
71+
applyFilters();
72+
});
73+
}
6074
});

assets/stylesheets/main.scss

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -210,16 +210,35 @@ $gray: #555;
210210
.filter-bar {
211211
display: flex;
212212
flex-wrap: wrap;
213-
margin-bottom: 2rem;
214213
padding-bottom: 1rem;
215-
gap: 1rem;
214+
margin-bottom: 1rem;
216215
border-bottom: 1px solid rgba(234, 234, 234, 0.5);
217216
}
218217

218+
.filter-search {
219+
margin-left: 0.5rem;
220+
padding: 0.4rem 0.8rem;
221+
border: 1px solid $white;
222+
border-radius: 10px;
223+
background: transparent;
224+
font-size: 0.9rem;
225+
color: $white;
226+
227+
&::placeholder {
228+
color: $white;
229+
opacity: 0.6;
230+
}
231+
232+
&:focus {
233+
border-color: $skip-red;
234+
}
235+
}
236+
219237
.filter-group {
220238
display: flex;
221239
flex-wrap: wrap;
222240
align-items: center;
241+
margin-bottom: 1rem;
223242
gap: 0.5rem;
224243
}
225244

0 commit comments

Comments
 (0)