Skip to content

Instantly share code, notes, and snippets.

@nurofsun
Created January 12, 2020 01:05
Show Gist options
  • Save nurofsun/b613ded50df2fe0876eed07f237ec993 to your computer and use it in GitHub Desktop.
Save nurofsun/b613ded50df2fe0876eed07f237ec993 to your computer and use it in GitHub Desktop.
Hugo lunr.js Implementation
{{ $data := getJSON "public/lunr.json" }}
{{ $data | jsonify }}
{{ 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