-
Notifications
You must be signed in to change notification settings - Fork 7.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
hugo: use fuse.js search instead of algolia
Signed-off-by: David Karlsson <35727626+dvdksn@users.noreply.github.com>
- Loading branch information
Showing
11 changed files
with
138 additions
and
902 deletions.
There are no files selected for viewing
This file was deleted.
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
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,9 +1,11 @@ | ||
import Alpine from 'alpinejs' | ||
import collapse from '@alpinejs/collapse' | ||
import focus from '@alpinejs/focus' | ||
|
||
window.Alpine = Alpine | ||
|
||
Alpine.plugin(collapse) | ||
Alpine.plugin(focus) | ||
Alpine.store("showSidebar", false) | ||
|
||
Alpine.start() |
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,17 +1,40 @@ | ||
import docsearch from "@docsearch/js"; | ||
import * as params from "@params"; // hugo dict | ||
import Fuse from "fuse.js"; | ||
|
||
const { appid, apikey, indexname } = params; | ||
const searchInput = document.querySelector("#search-input"); | ||
const searchResults = document.querySelector("#search-results"); | ||
|
||
docsearch({ | ||
container: "#docsearch", | ||
appId: appid, | ||
apiKey: apikey, | ||
indexName: indexname, | ||
transformItems(items) { | ||
return items.map((item) => ({ | ||
...item, | ||
url: item.url.replace("https://docs.docker.com", ""), | ||
})); | ||
}, | ||
}); | ||
async function handleSearch(e) { | ||
const searchQuery = e.target.value; | ||
const index = await fetch("/metadata.json").then((response) => | ||
response.json(), | ||
); | ||
|
||
const options = { | ||
keys: [ | ||
{ name: "title", weight: 2 }, | ||
{ name: "description", weight: 1 }, | ||
{ name: "keywords", weight: 1 }, | ||
], | ||
minMatchCharLength: 2, | ||
threshold: 0.2, | ||
}; | ||
const fuse = new Fuse(index, options); | ||
|
||
const results = fuse.search(searchQuery).map(({item}) => item); | ||
|
||
let resultsHTML = `<div>${results.length} results</div>` | ||
resultsHTML += results | ||
.map((item) => { | ||
return `<div class="bg-gray-light-100 dark:bg-gray-dark-200 rounded p-4"> | ||
<div class="flex flex-col"> | ||
<a class="link" href="${item.url}">${item.title}</a> | ||
<p>${item.description}</p> | ||
</div> | ||
</div>`; | ||
}) | ||
.join(""); | ||
|
||
searchResults.innerHTML = resultsHTML; | ||
} | ||
|
||
searchInput.addEventListener("input", handleSearch); |
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
Oops, something went wrong.