-
Notifications
You must be signed in to change notification settings - Fork 80
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #92 from nickhammond/add-search
Connect search form and get lunrjs search working
- Loading branch information
Showing
12 changed files
with
218 additions
and
5 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
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
Large diffs are not rendered by default.
Oops, something went wrong.
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,89 @@ | ||
function ready() { | ||
|
||
function showResults(results, store, searchTerm) { | ||
|
||
var searchResults = document.getElementById('search-results'); | ||
|
||
document.getElementById('search-query').innerHTML = 'Search results for <mark>' + searchTerm + '</mark>'; | ||
Check failure Code scanning / CodeQL Client-side cross-site scripting High
Cross-site scripting vulnerability due to
user-provided value Error loading related location Loading |
||
|
||
var appendString = ''; | ||
|
||
if(results.length) { | ||
|
||
for(var i = 0; i < results.length; i++) { | ||
|
||
var item = store[results[i].ref]; | ||
|
||
appendString += '<div class="search-result">'; | ||
appendString += ' <h3><a href="' + item.url + '">' + item.title + '</a></h3>'; | ||
appendString += ' <small><a href="' + item.url + '">' + item.section + '</a></small>'; | ||
appendString += ' <p>' + item.content.substring(0, 250) + '…</p>'; | ||
appendString += '</div>'; | ||
|
||
} | ||
|
||
} else { | ||
|
||
appendString = '<div class="search-result"><p>No results found.</p></div>'; | ||
|
||
} | ||
|
||
searchResults.innerHTML = appendString; | ||
|
||
} | ||
|
||
function getQuery(variable) { | ||
|
||
var query = window.location.search.substring(1); | ||
var vars = query.split('&'); | ||
|
||
for(var i = 0; i < vars.length; i++) { | ||
|
||
var pair = vars[i].split('='); | ||
|
||
if(pair[0] === variable) { | ||
|
||
return decodeURIComponent(pair[1].replace(/\+/g, '%20')); | ||
|
||
} | ||
|
||
} | ||
|
||
} | ||
|
||
var searchTerm = getQuery('q'); | ||
|
||
if(searchTerm) { | ||
|
||
document.getElementById('search-box').setAttribute('value', searchTerm); | ||
|
||
var idx = lunr(function() { | ||
|
||
this.field('id'); | ||
this.field('title', { | ||
boost: 10 | ||
}); | ||
this.field('author'); | ||
this.field('section'); | ||
this.field('content'); | ||
|
||
for(var key in window.store) { | ||
this.add({ | ||
'id': key, | ||
'title': window.store[key].title, | ||
'author': window.store[key].author, | ||
'section': window.store[key].section, | ||
'content': window.store[key].content | ||
}); | ||
} | ||
}); | ||
|
||
var results = idx.search(searchTerm); | ||
|
||
showResults(results, window.store, searchTerm); | ||
|
||
} | ||
|
||
} | ||
|
||
export { ready }; |
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 |
---|---|---|
@@ -1,10 +1,13 @@ | ||
import * as code from './modules/code.js'; | ||
import * as search from './modules/search.js'; | ||
import * as video from './modules/video.js'; | ||
|
||
document.addEventListener('DOMContentLoaded', () => { | ||
|
||
code.ready(); | ||
|
||
search.ready(); | ||
|
||
video.ready(); | ||
|
||
}); |
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 |
---|---|---|
@@ -1,4 +1,5 @@ | ||
--- | ||
title: Commands | ||
redirect_to: | ||
- /docs/commands/view-all-commands/ | ||
--- |
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 |
---|---|---|
@@ -1,4 +1,5 @@ | ||
--- | ||
title: Configuration | ||
redirect_to: | ||
- /docs/configuration/configuration-overview/ | ||
--- |
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 |
---|---|---|
@@ -1,4 +1,5 @@ | ||
--- | ||
title: Hooks | ||
redirect_to: | ||
- /docs/hooks/hooks-overview/ | ||
--- |
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,24 @@ | ||
--- | ||
title: Search Results | ||
nav: false | ||
--- | ||
|
||
<h1 id="search-query"></h1> | ||
|
||
<div id="search-results"></div> | ||
|
||
<script> | ||
{% assign docs = site.data.docs %} | ||
|
||
window.store = { | ||
{% for page in site.pages %} | ||
"{{ page.url | slugify }}": { | ||
"title": "{{ page.title | smartify | xml_escape }}", | ||
"content": {{ page.content | markdownify | strip_html | strip_newlines | jsonify }}, | ||
"section": "{{ page.url }}".split("/").filter(element => element !== "").slice(1).join("/"), | ||
"url": "{{ page.url | xml_escape }}" | ||
} | ||
{% unless forloop.last %},{% endunless %} | ||
{% endfor %} | ||
}; | ||
</script> |