Created
January 12, 2020 01:05
-
-
Save nurofsun/b613ded50df2fe0876eed07f237ec993 to your computer and use it in GitHub Desktop.
Hugo lunr.js Implementation
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
| {{ $data := getJSON "public/lunr.json" }} | |
| {{ $data | 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
| {{ define "main" }} | |
| <script src="https://unpkg.com/lunr/lunr.js"></script> | |
| <main class="search-page" aria-role="main"> | |
| {{ .Title }} | |
| <script> | |
| const indexJson = async function() { | |
| let res = await fetch('/index.json') | |
| let json = await res.json() | |
| const idx = lunr(function() { | |
| this.ref('uri') | |
| this.field('title') | |
| this.field('content') | |
| json.forEach(function(entry) { | |
| this.add(entry) | |
| }, this) | |
| }) | |
| let searchResults = await idx.search('post') | |
| console.log(searchResults) | |
| searchResults.forEach(function(item) { | |
| document.querySelector("#search-result").append(item.ref) | |
| }) | |
| } | |
| indexJson() | |
| </script> | |
| <div id="search-result"> | |
| </div> | |
| </main> | |
| {{ end }} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment