-
Notifications
You must be signed in to change notification settings - Fork 966
Offline search with Lunr.js #142
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
53d7145
Offline search
theletterf e860e34
Algolia fix
theletterf a93c680
Fix for Algolia
theletterf b6cd4e8
Revert fix
theletterf 473d41d
Fixes after review - single.html
theletterf 08ca6fe
Remove offlineSearch param from theme's config.toml
theletterf a8fc87d
Line fix
theletterf edeaf28
Add files and settings to userguide
theletterf File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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,3 +1,8 @@ | ||
| {{ with or .Site.Params.gcs_engine_id .Site.Params.algolia_docsearch }} | ||
| <input type="search" class="form-control td-search-input" placeholder=" {{ T "ui_search" }}" aria-label="{{ T "ui_search" }}" autocomplete="off"> | ||
| {{ if or .Site.Params.gcs_engine_id .Site.Params.algolia_docsearch }} | ||
| <input type="search" class="form-control td-search-input" placeholder=" {{ T "ui_search" }}" aria-label="{{ T "ui_search" }}" autocomplete="off"> | ||
| {{ else if .Site.Params.offlineSearch }} | ||
| <div id="search-nav-container"> | ||
| <input type="search" id="search-input" autocomplete="off" class="form-control td-search-input" placeholder=" {{ T "ui_search" }}" autocomplete="on"> | ||
| <div id="search-results" class="container"></div> | ||
| </div> | ||
| {{ end }} |
This file contains hidden or 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,5 @@ | ||
| {{- $.Scratch.Add "index" slice -}} | ||
| {{- range where .Site.Pages ".Params.exclude_search" "!=" true -}} | ||
| {{- $.Scratch.Add "index" (dict "title" .Title "ref" .Permalink "body" .Plain "excerpt" (.Summary | truncate 100)) -}} | ||
| {{- end -}} | ||
| {{- $.Scratch.Get "index" | jsonify -}} | ||
This file contains hidden or 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,39 @@ | ||
| #search-results{ | ||
| position: absolute; | ||
| width:90%; | ||
| font-size: 0.8rem; | ||
| top: 40px; | ||
| padding-left: 5px; | ||
| padding-right: 5px; | ||
| left:5%; | ||
| margin-top: -2px; | ||
| z-index: 1; | ||
| } | ||
|
|
||
| #search-nav-container { | ||
| position: relative; | ||
| width: 100%; | ||
| } | ||
|
|
||
| #search-results ul li{ | ||
| list-style-type: none; | ||
| list-style: none; | ||
| padding: 8px; | ||
| } | ||
|
|
||
| .td-search-input.form-control:focus{ | ||
| box-shadow: 0 0 0 1px #ccc!important; | ||
| } | ||
|
|
||
| #search-results ul{ | ||
| list-style-type: none; | ||
| list-style: none; | ||
| padding: 8px; | ||
| border-bottom:1px solid #ccc; | ||
| border-right:1px solid #ccc; | ||
| border-left:1px solid #ccc; | ||
| border-top:1px solid #ccc; | ||
| border-bottom-left-radius: 1rem; | ||
| border-bottom-right-radius: 1rem; | ||
| background: #fff; | ||
| } |
This file contains hidden or 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,103 @@ | ||
| // Adapted from code by Matt Walters https://www.mattwalters.net/posts/hugo-and-lunr/ | ||
|
|
||
| var idx = null; // Lunr index | ||
| var resultDetails = []; // Will hold the data for the search results (titles and summaries) | ||
| var $searchInput; // The search box element in the navbar | ||
| var $searchResults; // Results shown in the navbar | ||
|
|
||
| $(window).on('load', function() { | ||
| // Set up for an Ajax call to request the JSON data file that is created by | ||
| // Hugo's build process, with the template we added above | ||
| var request = new XMLHttpRequest(); | ||
| var query = ''; | ||
|
|
||
| // Get dom objects for the elements we'll be interacting with | ||
| $searchResults = document.getElementById('search-results'); | ||
| $searchInput = document.getElementById('search-input'); | ||
|
|
||
| request.overrideMimeType("application/json"); | ||
| request.open("GET", "/index.json", true); // Request the JSON file created during build | ||
| request.onload = function() { | ||
| if (request.status >= 200 && request.status < 400) { | ||
| // Success response received in requesting the search-index file | ||
| var searchDocuments = JSON.parse(request.responseText); | ||
|
|
||
| // Build the index so Lunr can search it. The `ref` field will hold the URL | ||
| // to the page/post. title, excerpt, and body will be fields searched. | ||
| idx = lunr(function lunrIndex() { | ||
| this.ref('ref'); | ||
| this.field('title'); | ||
| this.field('body'); | ||
|
|
||
| // Loop through all the items in the JSON file and add them to the index | ||
| // so they can be searched. | ||
| searchDocuments.forEach(function(doc) { | ||
| this.add(doc); | ||
| resultDetails[doc.ref] = { | ||
| 'title': doc.title, | ||
| 'excerpt': doc.excerpt, | ||
| }; | ||
| }, this); | ||
| }); | ||
| } else { | ||
| $searchResults.innerHTML = '<ul><li>Error loading search results</li></ul>'; | ||
| } | ||
| }; | ||
|
|
||
| request.onerror = function() { | ||
| $searchResults.innerHTML = '<ul><li>Error loading search results</li></ul>'; | ||
| }; | ||
|
|
||
| // Send the request to load the JSON | ||
| request.send(); | ||
|
|
||
| // Register handler for the search input field | ||
| registerSearchHandler(); | ||
| }); | ||
|
|
||
| function registerSearchHandler() { | ||
| $searchInput.oninput = function(event) { | ||
| var query = event.target.value; | ||
| var results = search(query); // Perform the search | ||
|
|
||
| // Render search results | ||
| renderSearchResults(results); | ||
|
|
||
| // Remove search results if the user empties the search phrase input field | ||
| if ($searchInput.value == '') { | ||
| $searchResults.innerHTML = ''; | ||
| } | ||
| } | ||
| } | ||
|
|
||
| function renderSearchResults(results) { | ||
| // Create a list of results | ||
| if (results.length > 0) { | ||
| var ul = document.createElement('ul'); | ||
| results.forEach(function(result) { | ||
| // Create result item | ||
| var li = document.createElement('li'); | ||
| li.innerHTML = '<a href="' + result.ref + '">' + resultDetails[result.ref].title + '</a><br>' + resultDetails[result.ref].excerpt + '...'; | ||
| ul.appendChild(li); | ||
| }); | ||
|
|
||
| // Remove any existing content so results aren't continually added as the user types | ||
| while ($searchResults.hasChildNodes()) { | ||
| $searchResults.removeChild( | ||
| $searchResults.lastChild | ||
| ); | ||
| } | ||
| } else { | ||
| $searchResults.innerHTML = '<ul><li>No results found</li></ul>'; | ||
| } | ||
| // Render the list | ||
| $searchResults.appendChild(ul); | ||
| } | ||
|
|
||
| function search(query) { | ||
| return idx.search(query); | ||
| } | ||
| // Disables enter key on input fields except textarea | ||
| $(document).on("keydown", ":input:not(textarea)", function(event) { | ||
| return event.key != "Enter"; | ||
| }); |
This file contains hidden or 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 hidden or 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,4 @@ | ||
| --- | ||
| type: "search-index" | ||
| url: "index.json" | ||
| --- |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.