-
Notifications
You must be signed in to change notification settings - Fork 64
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 #534 from 47degrees/feature/doc-search
Add Document Search
- Loading branch information
Showing
19 changed files
with
3,892 additions
and
34 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
div[id$='search-dropdown'] { | ||
position: relative; | ||
|
||
.dropdown { | ||
display: block; | ||
outline: 0; | ||
width: 100%; | ||
} | ||
|
||
label { | ||
padding-right: 0.3em; | ||
|
||
i { | ||
padding-right: 0.2em; | ||
} | ||
} | ||
|
||
/* Documentation Dropdown Content (Hidden by Default) */ | ||
.dropdown-content { | ||
position: absolute; | ||
min-width: 100px; | ||
overflow: hidden auto; | ||
box-shadow: 0 8px 16px 0 rgba(0, 0, 0, 0.2); | ||
z-index: 1; | ||
background: $white-color; | ||
margin: 0; | ||
padding: 0; | ||
opacity: 0.5; | ||
transform: rotate3d(1, 0, 0, 90deg); | ||
transition: transform ease 250ms, opacity ease 100ms; | ||
transform-origin: top; | ||
|
||
.dropdown-item { | ||
width: 100%; | ||
|
||
.dropdown-item-link { | ||
padding: 12px 9px; | ||
text-decoration: none; | ||
display: block; | ||
border-radius: 0; | ||
color: #333; | ||
|
||
&:hover span { | ||
border-bottom: 2px solid; | ||
} | ||
|
||
&:focus span { | ||
border-bottom: 2px solid; | ||
} | ||
|
||
} | ||
} | ||
} | ||
|
||
/* Show the documentation dropdown menu (use JS to add this class | ||
to the .dropdown-content container when the user clicks on | ||
the dropdown button) */ | ||
.show { | ||
transform: rotate3d(1, 0, 0, 0); | ||
opacity: 1; | ||
} | ||
} |
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 |
---|---|---|
@@ -0,0 +1,192 @@ | ||
--- | ||
--- | ||
// When the user clicks on the search box, we want to toggle the search dropdown | ||
function displayToggleSearch(e) { | ||
e.preventDefault(); | ||
e.stopPropagation(); | ||
|
||
closeDropdownSearch(e); | ||
|
||
if (idx === null) { | ||
console.log("Building search index..."); | ||
prepareIdxAndDocMap(); | ||
console.log("Search index built."); | ||
} | ||
const dropdown = document.querySelector("#search-dropdown-content"); | ||
if (dropdown) { | ||
if (!dropdown.classList.contains("show")) { | ||
dropdown.classList.add("show"); | ||
} | ||
document.addEventListener("click", closeDropdownSearch); | ||
document.addEventListener("keydown", searchOnKeyDown); | ||
document.addEventListener("keyup", searchOnKeyUp); | ||
} | ||
} | ||
|
||
//We want to prepare the index only after clicking the search bar | ||
var idx = null | ||
const docMap = new Map() | ||
|
||
function prepareIdxAndDocMap() { | ||
const docs = [{% if site.search_enabled %} {% for page in site.pages %} {% if page.title %} | ||
{ | ||
"title": "{{ page.title }}", | ||
"url": "{{ site.baseurl }}{{ page.url }}", | ||
"content": {{ page.content | markdownify | strip_html | normalize_whitespace | strip | jsonify }} | ||
} {% unless forloop.last %}, {% endunless %} {% endif %} {% endfor %} {% endif %} | ||
]; | ||
|
||
idx = lunr(function () { | ||
this.ref("title"); | ||
this.field("content"); | ||
|
||
docs.forEach(function (doc) { | ||
this.add(doc); | ||
}, this); | ||
}); | ||
|
||
docs.forEach(function (doc) { | ||
docMap.set(doc.title, doc.url); | ||
}); | ||
} | ||
|
||
// The onkeypress handler for search functionality | ||
function searchOnKeyDown(e) { | ||
const keyCode = e.keyCode; | ||
const parent = e.target.parentElement; | ||
const isSearchBar = e.target.id === "search-bar"; | ||
const isSearchResult = parent ? parent.id.startsWith("result-") : false; | ||
const isSearchBarOrResult = isSearchBar || isSearchResult; | ||
|
||
if (keyCode === 40 && isSearchBarOrResult) { | ||
// On 'down', try to navigate down the search results | ||
e.preventDefault(); | ||
e.stopPropagation(); | ||
selectDown(e); | ||
} else if (keyCode === 38 && isSearchBarOrResult) { | ||
// On 'up', try to navigate up the search results | ||
e.preventDefault(); | ||
e.stopPropagation(); | ||
selectUp(e); | ||
} else if (keyCode === 27 && isSearchBarOrResult) { | ||
// On 'ESC', close the search dropdown | ||
e.preventDefault(); | ||
e.stopPropagation(); | ||
closeDropdownSearch(e); | ||
} | ||
} | ||
|
||
// Search is only done on key-up so that the search terms are properly propagated | ||
function searchOnKeyUp(e) { | ||
// Filter out up, down, esc keys | ||
const keyCode = e.keyCode; | ||
const cannotBe = [40, 38, 27]; | ||
const isSearchBar = e.target.id === "search-bar"; | ||
const keyIsNotWrong = !cannotBe.includes(keyCode); | ||
if (isSearchBar && keyIsNotWrong) { | ||
// Try to run a search | ||
runSearch(e); | ||
} | ||
} | ||
|
||
// Move the cursor up the search list | ||
function selectUp(e) { | ||
if (e.target.parentElement.id.startsWith("result-")) { | ||
const index = parseInt(e.target.parentElement.id.substring(7)); | ||
if (!isNaN(index) && (index > 0)) { | ||
const nextIndexStr = "result-" + (index - 1); | ||
const querySel = "li[id$='" + nextIndexStr + "'"; | ||
const nextResult = document.querySelector(querySel); | ||
if (nextResult) { | ||
nextResult.firstChild.focus(); | ||
} | ||
} | ||
} | ||
} | ||
|
||
// Move the cursor down the search list | ||
function selectDown(e) { | ||
if (e.target.id === "search-bar") { | ||
const firstResult = document.querySelector("li[id$='result-0']"); | ||
if (firstResult) { | ||
firstResult.firstChild.focus(); | ||
} | ||
} else if (e.target.parentElement.id.startsWith("result-")) { | ||
const index = parseInt(e.target.parentElement.id.substring(7)); | ||
if (!isNaN(index)) { | ||
const nextIndexStr = "result-" + (index + 1); | ||
const querySel = "li[id$='" + nextIndexStr + "'"; | ||
const nextResult = document.querySelector(querySel); | ||
if (nextResult) { | ||
nextResult.firstChild.focus(); | ||
} | ||
} | ||
} | ||
} | ||
|
||
// Search for whatever the user has typed so far | ||
function runSearch(e) { | ||
if (e.target.value === "") { | ||
// On empty string, remove all search results | ||
// Otherwise this may show all results as everything is a "match" | ||
applySearchResults([]); | ||
} else { | ||
const tokens = e.target.value.split(" "); | ||
const moddedTokens = tokens.map(function (token) { | ||
// "*" + token + "*" | ||
return token; | ||
}) | ||
const searchTerm = moddedTokens.join(" "); | ||
const searchResults = idx.search(searchTerm); | ||
const mapResults = searchResults.map(function (result) { | ||
const resultUrl = docMap.get(result.ref); | ||
return { name: result.ref, url: resultUrl }; | ||
}) | ||
|
||
applySearchResults(mapResults); | ||
} | ||
|
||
} | ||
|
||
// After a search, modify the search dropdown to contain the search results | ||
function applySearchResults(results) { | ||
const dropdown = document.querySelector("div[id$='search-dropdown'] > .dropdown-content.show"); | ||
if (dropdown) { | ||
//Remove each child | ||
while (dropdown.firstChild) { | ||
dropdown.removeChild(dropdown.firstChild); | ||
} | ||
|
||
//Add each result as an element in the list | ||
results.forEach(function (result, i) { | ||
const elem = document.createElement("li"); | ||
elem.setAttribute("class", "dropdown-item"); | ||
elem.setAttribute("id", "result-" + i); | ||
|
||
const elemLink = document.createElement("a"); | ||
elemLink.setAttribute("title", result.name); | ||
elemLink.setAttribute("href", result.url); | ||
elemLink.setAttribute("class", "dropdown-item-link"); | ||
|
||
const elemLinkText = document.createElement("span"); | ||
elemLinkText.setAttribute("class", "dropdown-item-link-text"); | ||
elemLinkText.innerHTML = result.name; | ||
|
||
elemLink.appendChild(elemLinkText); | ||
elem.appendChild(elemLink); | ||
dropdown.appendChild(elem); | ||
}); | ||
} | ||
} | ||
|
||
// Close the dropdown if the user clicks (only) outside of it | ||
function closeDropdownSearch(e) { | ||
// Check if where we're clicking is the search dropdown | ||
if (e.target.id !== "search-bar") { | ||
const dropdown = document.querySelector("div[id$='search-dropdown'] > .dropdown-content.show"); | ||
if (dropdown) { | ||
dropdown.classList.remove("show"); | ||
document.documentElement.removeEventListener("click", closeDropdownSearch); | ||
} | ||
} | ||
} |
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 |
---|---|---|
@@ -0,0 +1,20 @@ | ||
|
||
Copyright (C) 2013 by Oliver Nightingale | ||
|
||
Permission is hereby granted, free of charge, to any person obtaining a copy | ||
of this software and associated documentation files (the "Software"), to deal | ||
in the Software without restriction, including without limitation the rights | ||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
copies of the Software, and to permit persons to whom the Software is | ||
furnished to do so, subject to the following conditions: | ||
|
||
The above copyright notice and this permission notice shall be included in | ||
all copies or substantial portions of the Software. | ||
|
||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN | ||
THE SOFTWARE. |
Oops, something went wrong.