-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #6 from StephenHoover/master
Added search functionality Thanks @StephenHoover
- Loading branch information
Showing
6 changed files
with
181 additions
and
1 deletion.
There are no files selected for viewing
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,6 @@ | ||
+++ | ||
title = "Search" | ||
date = "2024-02-27" | ||
template = "search.html" | ||
extra = { is_search_page = true } | ||
+++ |
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,96 @@ | ||
|
||
// Zola Paper Mod javascript search code by Stephen Hoover | ||
// Used: | ||
// - https://github.com/getzola/zola/blob/master/docs/static/search.js | ||
// - https://github.com/reorx/hugo-PaperModX/blob/master/assets/js/fastsearch.js | ||
|
||
// Debounce function definition | ||
function debounce(func, wait, immediate) { | ||
var timeout; | ||
return function() { | ||
var context = this, args = arguments; | ||
var later = function() { | ||
timeout = null; | ||
if (!immediate) func.apply(context, args); | ||
}; | ||
var callNow = immediate && !timeout; | ||
clearTimeout(timeout); | ||
timeout = setTimeout(later, wait); | ||
if (callNow) func.apply(context, args); | ||
}; | ||
} | ||
|
||
function formatSearchResultItem(item, terms) { | ||
// Adjust this to match your desired result item structure | ||
return `<li class="post-entry"> | ||
<header class="entry-header">${item.doc.title} »</header>` + | ||
`<a href="${item.ref}" aria-label="${item.doc.title}"></a> | ||
</li>`; | ||
} | ||
|
||
function initSearch() { | ||
var $searchInput = document.getElementById("searchInput"); // Make sure ID matches HTML | ||
var $searchResults = document.getElementById("searchResults"); // Ensure this matches your HTML | ||
var MAX_ITEMS = 10; | ||
|
||
var options = { | ||
bool: "AND", | ||
fields: { | ||
title: {boost: 2}, | ||
body: {boost: 1}, | ||
} | ||
}; | ||
var currentTerm = ""; | ||
var index; | ||
|
||
var initIndex = async function () { | ||
if (index === undefined) { | ||
index = fetch("/search_index.en.json") // Make sure the path to your search index is correct | ||
.then( | ||
async function(response) { | ||
return await elasticlunr.Index.load(await response.json()); | ||
} | ||
); | ||
} | ||
let res = await index; | ||
return res; | ||
} | ||
|
||
$searchInput.addEventListener("keyup", debounce(async function() { | ||
var term = $searchInput.value.trim(); | ||
if (term === currentTerm) { | ||
return; | ||
} | ||
$searchResults.style.display = term === "" ? "none" : "block"; | ||
$searchResults.innerHTML = ""; // Clear previous results | ||
currentTerm = term; | ||
if (term === "") { | ||
return; | ||
} | ||
|
||
var results = (await initIndex()).search(term, options); | ||
if (results.length === 0) { | ||
$searchResults.style.display = "none"; | ||
return; | ||
} | ||
|
||
// Directly inserting formatted search result items without additional <li> creation | ||
for (var i = 0; i < Math.min(results.length, MAX_ITEMS); i++) { | ||
$searchResults.innerHTML += formatSearchResultItem(results[i], term.split(" ")); | ||
} | ||
}, 150)); | ||
window.addEventListener('click', function(e) { | ||
if ($searchResults.style.display == "block" && !$searchResults.contains(e.target)) { | ||
$searchResults.style.display = "none"; | ||
} | ||
}); | ||
} | ||
|
||
|
||
if (document.readyState === "complete" || | ||
(document.readyState !== "loading" && !document.documentElement.doScroll) | ||
) { | ||
initSearch(); | ||
} else { | ||
document.addEventListener("DOMContentLoaded", initSearch); | ||
} |
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,33 @@ | ||
<header class="page-header"> | ||
{% if page.title %} | ||
<h1>{{ page.title | safe }} | ||
<svg | ||
xmlns="http://www.w3.org/2000/svg" | ||
width="28" | ||
height="28" | ||
viewBox="0 0 24 24" | ||
fill="none" | ||
stroke="currentColor" | ||
stroke-width="2" | ||
stroke-linecap="round" | ||
stroke-linejoin="round"> | ||
<circle cx="11" cy="11" r="8"></circle> | ||
<line x1="21" y1="21" x2="16.65" y2="16.65"></line> | ||
</svg> | ||
</h1> | ||
{% endif %} | ||
{% if page.description %} | ||
<div class="post-description"> | ||
{{ page.description | safe }} | ||
</div> | ||
{% endif %} | ||
<div id="searchbox"> | ||
<input id="searchInput" | ||
autofocus="" | ||
placeholder="Search locally ↵" | ||
aria-label="search" | ||
type="search" | ||
autocomplete="off"> | ||
<ul id="searchResults" aria-label="search results"></ul> | ||
</div> | ||
</header> |
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,23 @@ | ||
{% import "macros/social_icon.svg.html" as macro_social -%} | ||
<!DOCTYPE html> | ||
<html lang="{{ lang }}" dir="{{ config.extra.papermod.language_direction }}"> | ||
<head> | ||
{% block head %} | ||
{% include "partials/head.html" %} | ||
{% endblock %} | ||
</head> | ||
<body class="{% if section or term or taxonomy %}list {% endif %}{% if config.extra.papermod.theme == 'dark' %} dark{% endif %}" id="top"> | ||
{% block header %} | ||
{% include "partials/header.html" %} | ||
{% endblock %} | ||
<main class="main"> | ||
{% block main %} | ||
{% include "partials/search.html" %} | ||
{% endblock %} | ||
</main> | ||
{% block footer %} | ||
{% include "partials/footer.html" %} | ||
{% endblock %} | ||
</body> | ||
</html> | ||
|