mikewalch closed pull request #162: Adding Search functionality to website
URL: https://github.com/apache/fluo-website/pull/162
This is a PR merged from a forked repository.
As GitHub hides the original diff on merge, it is displayed below for
the sake of provenance:
As this is a foreign pull request (from a fork), the diff is supplied
below (as it won't show otherwise due to GitHub magic):
diff --git a/_layouts/default.html b/_layouts/default.html
index 89c754cd..b6297f5b 100644
--- a/_layouts/default.html
+++ b/_layouts/default.html
@@ -59,10 +59,11 @@
<li><a href="{{ site.baseurl }}/release-process/">Release
Process</a></li>
</ul>
</li>
+ <li><a href="{{ site.baseurl }}/search/">Search</a></li>
</ul>
<ul class="navbar-nav nav navbar-right">
<li class="dropdown">
- <a class="dropdown-toggle" data-toggle="dropdown"
href="#">Apache Software Foundation<span class="caret"></span></a>
+ <a class="dropdown-toggle" data-toggle="dropdown" href="#"><img
alt="Apache Software Foundation"
src="https://www.apache.org/images/feather-small.png" width="70"/><span
class="caret"></span></a>
<ul class="dropdown-menu">
<li><a href="https://www.apache.org">Apache Homepage</a></li>
<li><a href="https://www.apache.org/licenses/">License</a></li>
diff --git a/js/search.js b/js/search.js
new file mode 100644
index 00000000..5501f85a
--- /dev/null
+++ b/js/search.js
@@ -0,0 +1,48 @@
+jQuery(function() {
+
+ window.idx = lunr(function () {
+ this.field('id');
+ this.field('title');
+ this.field('content', { boost: 10 });
+ this.field('categories');
+ });
+
+ window.data = $.getJSON('/search_data.json');
+
+ window.data.then(function(loaded_data){
+ $.each(loaded_data, function(index, value){
+ window.idx.add($.extend({ "id": index }, value));
+ });
+ });
+
+ $("#site_search").submit(function(event){
+ event.preventDefault();
+ var query = $("#search_box").val();
+ var results = window.idx.search(query);
+ display_search_results(query, results);
+ });
+
+ function display_search_results(query, results) {
+ var $search_status = $("#search_status");
+ var $search_results = $("#search_results");
+
+ window.data.then(function(loaded_data) {
+
+ if (results.length) {
+ $search_status.html('Found ' + results.length + ' results for "' +
query + '"');
+ $search_results.empty();
+ results.forEach(function(result) {
+ var item = loaded_data[result.ref];
+ var n = item.content.search(query) - 50
+ if (n < 0) {
+ n = 0;
+ }
+ var appendString = '<tr><td><a href="' + item.url + '">' +
item.title + '</a><td>' + item.content.substring(n, n+100) + '</tr>';
+ $search_results.append(appendString);
+ });
+ } else {
+ $search_status.html('No results found!');
+ }
+ });
+ }
+});
diff --git a/pages/search.md b/pages/search.md
new file mode 100644
index 00000000..3e2fdd53
--- /dev/null
+++ b/pages/search.md
@@ -0,0 +1,30 @@
+---
+layout: page
+title: Search
+permalink: /search/
+---
+
+Search results are limited to blog posts, release notes, and documentation
(Fluo 1.2 & Fluo Recipes 1.2).
+
+<div class="row">
+ <div class="col-lg-6">
+ <form action="get" id="site_search">
+ <div class="input-group">
+ <input class="form-control" type="text" id="search_box"
placeholder="Search for...">
+ <span class="input-group-btn">
+ <button class="btn btn-default" type="submit">Search</button>
+ </span>
+ </div>
+ </form>
+ </div>
+</div>
+
+<br/>
+
+<div id="search_status"></div>
+
+<table class="table table-striped" id="search_results"></table>
+
+<script
src="https://cdnjs.cloudflare.com/ajax/libs/lunr.js/1.0.0/lunr.min.js"></script>
+<script
src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script>
+<script src="/js/search.js"></script>
diff --git a/search_data.json b/search_data.json
new file mode 100644
index 00000000..25c3c525
--- /dev/null
+++ b/search_data.json
@@ -0,0 +1,30 @@
+---
+layout: null
+---
+{
+ {% for post in site.fluo-1-2 %}
+ "{{ post.url | slugify }}": {
+ "title": "{{ post.title | xml_escape }}",
+ "content" : "{{post.content | strip_html | strip_newlines |
remove: " " | escape | remove: "\"}}",
+ "url": " {{ post.url | xml_escape }}",
+ "categories": "{% for category in post.categories %}{{ category }}{%
unless forloop.last %}, {% endunless %}{% endfor %}"
+ },
+ {% endfor %}
+ {% for post in site.recipes-1-2 %}
+ "{{ post.url | slugify }}": {
+ "title": "{{ post.title | xml_escape }}",
+ "content" : "{{post.content | strip_html | strip_newlines |
remove: " " | escape | remove: "\"}}",
+ "url": " {{ post.url | xml_escape }}",
+ "categories": "{% for category in post.categories %}{{ category }}{%
unless forloop.last %}, {% endunless %}{% endfor %}"
+ },
+ {% endfor %}
+ {% for post in site.posts %}
+ "{{ post.url | slugify }}": {
+ "title": "{{ post.title | xml_escape }}",
+ "content" : "{{post.content | strip_html | strip_newlines |
remove: " " | escape | remove: "\"}}",
+ "url": " {{ post.url | xml_escape }}",
+ "categories": "{% for category in post.categories %}{{ category }}{%
unless forloop.last %}, {% endunless %}{% endfor %}"
+ }
+ {% unless forloop.last %},{% endunless %}
+ {% endfor %}
+}
----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on GitHub and use the
URL above to go to the specific comment.
For queries about this service, please contact Infrastructure at:
[email protected]
With regards,
Apache Git Services