diff options
author | Alexander Neonxp Kiryukhin <i@neonxp.ru> | 2024-12-17 20:37:32 +0300 |
---|---|---|
committer | Alexander Neonxp Kiryukhin <i@neonxp.ru> | 2024-12-17 20:37:32 +0300 |
commit | c0073e38e6ee3753f6cece23f60c608162a06918 (patch) | |
tree | da60ecf78e390f0b52e7857a2a0d527c274a700a /static/js/search.js | |
parent | a7d25c3f329ba0c9906acd486a1c12707c617f2d (diff) |
Auto-commit 2024-12-17
Diffstat (limited to 'static/js/search.js')
-rw-r--r-- | static/js/search.js | 27 |
1 files changed, 27 insertions, 0 deletions
diff --git a/static/js/search.js b/static/js/search.js new file mode 100644 index 0000000..62bcbbb --- /dev/null +++ b/static/js/search.js @@ -0,0 +1,27 @@ +fetch('/index.json') + .then(response => response.json()) + .then(data => { + const fuse = new Fuse(data, { + keys: ['title', 'contents', 'tags'], + includeScore: true + }); + document.getElementById('search-input').addEventListener('input', function (e) { + const results = fuse.search(e.target.value); + displayResults(results); + }); + }); + +function displayResults(results) { + const searchResults = document.getElementById('search-results'); + searchResults.innerHTML = ''; + if (results.length > 0) { + searchResults.classList.remove("hidden"); + } else { + searchResults.classList.add("hidden"); + } + results.forEach(result => { + const elem = document.createElement('div'); + elem.innerHTML = `<a class="list-group-item list-group-item-action" href="${result.item.permalink}">${result.item.title}</a></a`; + searchResults.appendChild(elem); + }); +} |