-
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
18 changed files
with
181 additions
and
919 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,12 +1,13 @@ | ||
import Alpine from 'alpinejs' | ||
import collapse from '@alpinejs/collapse' | ||
import persist from '@alpinejs/persist' | ||
import focus from '@alpinejs/focus' | ||
|
||
window.Alpine = Alpine | ||
|
||
Alpine.plugin(collapse) | ||
Alpine.store("showSidebar", false) | ||
|
||
Alpine.plugin(persist) | ||
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,54 @@ | ||
import docsearch from "@docsearch/js"; | ||
import * as params from "@params"; // hugo dict | ||
|
||
const { appid, apikey, indexname } = params; | ||
|
||
docsearch({ | ||
container: "#docsearch", | ||
appId: appid, | ||
apiKey: apikey, | ||
indexName: indexname, | ||
transformItems(items) { | ||
return items.map((item) => ({ | ||
...item, | ||
url: item.url.replace("https://docs.docker.com", ""), | ||
})); | ||
}, | ||
}); | ||
import Fuse from "fuse.js"; | ||
|
||
let indexed = false; | ||
let handler = null; | ||
|
||
const modalSearchInput = document.querySelector("#modal-search-input"); | ||
const modalSearchResults = document.querySelector("#modal-search-results"); | ||
|
||
async function initializeIndex() { | ||
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 }, | ||
{ name: "tags", weight: 1 }, | ||
], | ||
minMatchCharLength: 2, | ||
threshold: 0.2, | ||
}; | ||
|
||
handler = new Fuse(index, options); | ||
indexed = true; | ||
} | ||
|
||
async function executeSearch(query) { | ||
!indexed && (await initializeIndex()); | ||
const results = handler.search(query).map(({ item }) => item); | ||
return results | ||
} | ||
|
||
async function modalSearch(e) { | ||
const query = e.target.value; | ||
results = await executeSearch(query) | ||
|
||
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(""); | ||
|
||
modalSearchResults.innerHTML = resultsHTML; | ||
} | ||
|
||
modalSearchInput.addEventListener("input", modalSearch); |
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
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
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,45 @@ | ||
<div x-data="{ open: false }" | ||
@keyup.escape.window="open = false; if (!open) { document.body.classList.remove('overflow-hidden'); }" | ||
@keydown.window="(e) => { | ||
switch(e.key) { | ||
case 'k': | ||
if (e.metaKey || e.ctrlKey) { | ||
e.preventDefault() | ||
open = !open; | ||
if (open) { | ||
document.body.classList.add('overflow-hidden'); | ||
} else { | ||
document.body.classList.remove('overflow-hidden'); | ||
} | ||
} | ||
} | ||
}"> | ||
<!-- search button --> | ||
<button @click="open = true; document.body.classList.add('overflow-hidden');"> | ||
<span class="icon-svg">{{ partialCached "icon" "search" "search" }}</span> | ||
</button> | ||
<!-- search modal --> | ||
<div class="fixed left-0 top-0 z-20 flex w-lvw justify-center py-12 text-gray-light-800 dark:text-gray-dark-800" | ||
role="dialog" tabindex="-1" x-show="open" x-trap="open" x-cloak x-transition> | ||
<div class="max-h-[80vh] overflow-hidden lg:w-[600px] xl:w-[800px] rounded-lg bg-white p-2 mx-8 dark:bg-background-dark w-full flex flex-col" | ||
@click.away="open = false; document.body.classList.remove('overflow-hidden');"> | ||
<div class="m-2 text-xl">Search Docker documentation</div> | ||
<header class="flex items-center py-2"> | ||
<input type="search" id="modal-search-input" | ||
class="mx-1 flex h-12 flex-auto appearance-none rounded bg-transparent p-4 outline outline-2 outline-gray-light focus:outline-blue-light dark:outline-gray-dark dark:focus:outline-blue-dark" | ||
placeholder="Search..." tabindex="0" /> | ||
<div class="icon-svg px-2"> | ||
{{ partialCached "icon" "search" "search" }} | ||
</div> | ||
</header> | ||
<section class="overflow-y-auto px-2"> | ||
<div class="flex min-h-0 flex-col gap-2" id="modal-search-results"> | ||
<!-- results --> | ||
</div> | ||
</section> | ||
</div> | ||
</div> | ||
<!-- search modal backdrop --> | ||
<div class="fixed left-0 top-0 h-full w-full bg-background-dark/70 dark:bg-gray-dark-100/70" x-show="open" x-cloak> | ||
</div> | ||
</div> |
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.