-
Notifications
You must be signed in to change notification settings - Fork 25
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
171 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,6 +2,9 @@ | |
"env": { | ||
"browser": true | ||
}, | ||
"globals": { | ||
"lunr": true | ||
}, | ||
"rules": { | ||
"strict": [2, "function"] | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
|
||
|
||
function displaySearchResults(results, store) { | ||
var searchResults = document.getElementById('search-results'); | ||
|
||
if (results.length) { // Are there any results? | ||
var appendString = ''; | ||
|
||
for (var i = 0; i < results.length; i++) { // Iterate over the results | ||
var item = store[results[i].ref]; | ||
appendString += '<li><a href="' + item.url + '"><h3>' + item.title + '</h3></a>'; | ||
appendString += '<p>' + item.content.substring(0, 150) + '...</p></li>'; | ||
} | ||
|
||
searchResults.innerHTML = appendString; | ||
} else { | ||
searchResults.innerHTML = '<li>No results found</li>'; | ||
} | ||
} | ||
|
||
var searchTerm = new URLSearchParams(window.location.search).get('q'); | ||
|
||
if (searchTerm) { | ||
document.getElementById('search-box').setAttribute('value', searchTerm); | ||
|
||
// Initalize lunr with the fields it will be searching on. I've given title | ||
// a boost of 10 to indicate matches on this field are more important. | ||
var idx = lunr(function () { | ||
this.field('id'); | ||
this.field('title', { boost: 10 }); | ||
this.field('categories'); | ||
this.field('content'); | ||
|
||
for (var key in window.store) { // Add the data to lunr | ||
this.add({ | ||
'id': key, | ||
'title': window.store[key].title, | ||
'categories': window.store[key].categories, | ||
'content': window.store[key].content | ||
}); | ||
} | ||
}); | ||
|
||
var results = idx.search(searchTerm); // Get lunr to perform a search | ||
displaySearchResults(results, window.store); // We'll write this in the next section | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
--- | ||
layout: en | ||
--- | ||
|
||
<ul id="search-results"></ul> | ||
|
||
<script> | ||
window.store = { | ||
{% for post in site.posts %} | ||
{% if post.categories contains "zh" %} | ||
{% continue %} | ||
{% else %} | ||
"{{ post.url | slugify }}": { | ||
"title": {{ post.title | jsonify }}, | ||
"categories": {{ post.categories | jsonify }}, | ||
"content": {{ post.content | strip_html | strip_newlines | jsonify }}, | ||
"url": {{ post.url | jsonify }} | ||
}, | ||
{% endif %} | ||
{% endfor %} | ||
{% for page in site.pages %} | ||
{% if page.url contains "/zh/" %} | ||
{% continue %} | ||
{% else %} | ||
"{{ page.url | slugify }}": { | ||
"title": {{ page.title | jsonify }}, | ||
"categories": {{ page.categories | jsonify }}, | ||
"content": {{ page.content | strip_html | strip_newlines | jsonify }}, | ||
"url": {{ page.url | jsonify }} | ||
}, | ||
{% endif %} | ||
{% endfor %} | ||
}; | ||
</script> | ||
|
||
<script src="https://unpkg.com/lunr/lunr.js"></script> | ||
<script src="{{ 'assets/javascript/search.mjs' | relative_url }}"></script> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
--- | ||
layout: zh | ||
--- | ||
|
||
<ul id="search-results"></ul> | ||
|
||
<script> | ||
window.store = { | ||
{% for post in site.posts %} | ||
{% if post.categories contains "zh" %} | ||
"{{ post.url | slugify }}": { | ||
"title": {{ post.title | jsonify }}, | ||
"categories": {{ post.categories | jsonify }}, | ||
"content": {{ post.content | strip_html | strip_newlines | jsonify }}, | ||
"url": {{ post.url | jsonify }} | ||
} | ||
{% unless forloop.last %},{% endunless %} | ||
{% endif %} | ||
{% endfor %} | ||
{% for page in site.pages %} | ||
{% if page.url contains "/zh/" %} | ||
"{{ page.url | slugify }}": { | ||
"title": {{ page.title | jsonify }}, | ||
"categories": {{ page.categories | jsonify }}, | ||
"content": {{ page.content | strip_html | strip_newlines | jsonify }}, | ||
"url": {{ page.url | jsonify }} | ||
}, | ||
{% endif %} | ||
{% endfor %} | ||
}; | ||
</script> | ||
|
||
<script src="https://unpkg.com/lunr/lunr.js"></script> | ||
<script src="{{ 'assets/javascript/search.mjs' | relative_url }}"></script> |