Skip to content

Commit

Permalink
Add full text search to archive page
Browse files Browse the repository at this point in the history
Bump version to 1.0.1
  • Loading branch information
evaneykelen committed Jul 26, 2019
1 parent ec495d0 commit 1d79a60
Show file tree
Hide file tree
Showing 5 changed files with 75 additions and 14 deletions.
4 changes: 4 additions & 0 deletions gem/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# Changelog

## Version 1.0.1

- Add full text search to archive page

## Version 1.0.0

- Fix bug with development and runtime dependencies.
Expand Down
22 changes: 11 additions & 11 deletions gem/Gemfile.lock
Original file line number Diff line number Diff line change
@@ -1,16 +1,22 @@
PATH
remote: .
specs:
msgtrail (0.9.8)
msgtrail (1.0.1)
dotenv (~> 2.7)
http (~> 4.1)
multi_json (~> 1.13)
nicetitle (~> 1.0.1)
redcarpet (~> 3.4)
tilt (~> 2.0)

GEM
remote: https://rubygems.org/
specs:
addressable (2.6.0)
public_suffix (>= 2.0.2, < 4.0)
domain_name (0.5.20180417)
domain_name (0.5.20190701)
unf (>= 0.0.5, < 1.0.0)
dotenv (2.7.2)
dotenv (2.7.4)
http (4.1.1)
addressable (~> 2.3)
http-cookie (~> 1.0)
Expand All @@ -23,28 +29,22 @@ GEM
minitest (5.11.3)
multi_json (1.13.1)
nicetitle (1.0.1)
public_suffix (3.0.3)
public_suffix (3.1.1)
rake (10.5.0)
redcarpet (3.4.0)
tilt (2.0.9)
unf (0.1.4)
unf_ext
unf_ext (0.0.7.5)
unf_ext (0.0.7.6)

PLATFORMS
ruby

DEPENDENCIES
bundler (~> 2.0)
dotenv (~> 2.7)
http (~> 4.1)
minitest (~> 5.0)
msgtrail!
multi_json (~> 1.13)
nicetitle (~> 1.0.1)
rake (~> 10.0)
redcarpet (~> 3.4)
tilt (~> 2.0)

BUNDLED WITH
2.0.2
2 changes: 1 addition & 1 deletion gem/lib/msgtrail/version.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
module Msgtrail
VERSION = "1.0.0"
VERSION = "1.0.1"
end
Binary file added gem/pkg/msgtrail-1.0.1.gem
Binary file not shown.
61 changes: 59 additions & 2 deletions sample-blog/theme/archive.html.erb
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
<div class="row my-2">
<div class="col-12">
<% articles.each do |article| %>
<div class="px-3 mb-3">
<div class="mb-4 px-3">
<input type="search" id="search-phrase" class="form-control xform-control-lg" placeholder="Full-text search">
</div>
<% articles.each_with_index do |article, idx| %>
<div id="article-<%= idx %>" class="article px-3 mb-3">
<div class="form-row">
<div class="col-12 col-sm-3 col-md-2">
<a class="small text-muted" href="/articles/<%= article[:slug] %>">
Expand All @@ -18,3 +21,57 @@
<% end %>
</div>
</div>

<script>

document.addEventListener("DOMContentLoaded", function() {

articles = [
<% articles.each do |article| %>
<% body = article[:bodies].map { |body| plaintext.render(body[:body]).tr('\\', '') }.join %>
`<%= "#{article[:title]}#{body}".gsub(/\"|\n|\r|`/, '').downcase.scan(/.{100}/).join("\n") %>`,
<% end %>
];

// Return array indices for array element matching `query`
function findString(arr, query) {
result = [];
pattern = new RegExp(query, "i");
for(i = 0; i < arr.length; i++) {
if (arr[i].match(pattern)) result.push(i);
}
return result;
}

function showArticles() {
document.querySelectorAll(".article").forEach(el => {
el.classList.remove("d-none");
el.classList.add("d-block")
});
}

function hideArticles() {
document.querySelectorAll(".article").forEach(el => {
el.classList.remove("d-block");
el.classList.add("d-none")
});
}

document.getElementById("search-phrase").addEventListener("keyup", function() {
query = this.value.replace(/\s+/g, "");
if (query.length === 0) {
showArticles();
} else {
hideArticles();
result = findString(articles, query);
if (result.length > 0) {
for(i = 0; i < result.length; i++) {
document.getElementById("article-" + result[i]).classList.add("d-block");
}
}
}
});

});

</script>

0 comments on commit 1d79a60

Please sign in to comment.