diff options
| author | Mitja Felicijan <m@mitjafelicijan.com> | 2023-06-29 22:07:53 +0200 |
|---|---|---|
| committer | Mitja Felicijan <m@mitjafelicijan.com> | 2023-06-29 22:07:53 +0200 |
| commit | 0ee84cf49b09628e0321edfd1c552e12179c3460 (patch) | |
| tree | 5fc14124f2542888180836ecb35b80081d0f9d9f /themes/simple/layouts/partials/search.html | |
| parent | b43c8546760cc05af867b2c4c067cb29c7c911fe (diff) | |
| download | mitjafelicijan.com-0ee84cf49b09628e0321edfd1c552e12179c3460.tar.gz | |
Minor tweaks to navigation, pages and search
Diffstat (limited to 'themes/simple/layouts/partials/search.html')
| -rw-r--r-- | themes/simple/layouts/partials/search.html | 11 |
1 files changed, 7 insertions, 4 deletions
diff --git a/themes/simple/layouts/partials/search.html b/themes/simple/layouts/partials/search.html index 5a76384..7978000 100644 --- a/themes/simple/layouts/partials/search.html +++ b/themes/simple/layouts/partials/search.html | |||
| @@ -7,6 +7,7 @@ | |||
| 7 | <script> | 7 | <script> |
| 8 | (async function() { | 8 | (async function() { |
| 9 | const debounceDelay = 700; | 9 | const debounceDelay = 700; |
| 10 | const maxSearchResults = 10; | ||
| 10 | 11 | ||
| 11 | // Fetch search index generated by Hugo. | 12 | // Fetch search index generated by Hugo. |
| 12 | const req = await fetch('/index.json'); | 13 | const req = await fetch('/index.json'); |
| @@ -41,9 +42,9 @@ | |||
| 41 | const searchButtonElement = document.querySelector('.search-button'); | 42 | const searchButtonElement = document.querySelector('.search-button'); |
| 42 | const searchButtonTextElement = document.querySelector('.search-button-text'); | 43 | const searchButtonTextElement = document.querySelector('.search-button-text'); |
| 43 | if (searchButtonElement) { | 44 | if (searchButtonElement) { |
| 44 | let searchButtonText = 'ctrl+k'; | 45 | let searchButtonText = 'Ctrl+K'; |
| 45 | if (navigator.platform.toUpperCase().indexOf('MAC') >= 0) { | 46 | if (navigator.platform.toUpperCase().indexOf('MAC') >= 0) { |
| 46 | searchButtonText = 'cmd+k'; | 47 | searchButtonText = '⌘+K'; |
| 47 | } | 48 | } |
| 48 | searchButtonTextElement.innerText = searchButtonText; | 49 | searchButtonTextElement.innerText = searchButtonText; |
| 49 | searchButtonElement.classList.remove('hidden'); | 50 | searchButtonElement.classList.remove('hidden'); |
| @@ -75,6 +76,8 @@ | |||
| 75 | searchResults.classList.add('hidden'); | 76 | searchResults.classList.add('hidden'); |
| 76 | } | 77 | } |
| 77 | } | 78 | } |
| 79 | } else { | ||
| 80 | searchInput.focus(); | ||
| 78 | } | 81 | } |
| 79 | } | 82 | } |
| 80 | 83 | ||
| @@ -136,7 +139,7 @@ | |||
| 136 | searchInput.addEventListener('keyup', debounce((evt)=> { | 139 | searchInput.addEventListener('keyup', debounce((evt)=> { |
| 137 | const query = evt.target.value.trim().toLowerCase(); | 140 | const query = evt.target.value.trim().toLowerCase(); |
| 138 | if (query.length && query != cachedSearchTerm) { | 141 | if (query.length && query != cachedSearchTerm) { |
| 139 | const results = searchIndex.search(query); | 142 | const results = searchIndex.search(`*${query}*`); |
| 140 | 143 | ||
| 141 | if (results.length == 0) { | 144 | if (results.length == 0) { |
| 142 | if (!searchResults.classList.contains('hidden')) { | 145 | if (!searchResults.classList.contains('hidden')) { |
| @@ -147,7 +150,7 @@ | |||
| 147 | searchResults.classList.remove('hidden'); | 150 | searchResults.classList.remove('hidden'); |
| 148 | cachedSearchTerm = query; | 151 | cachedSearchTerm = query; |
| 149 | 152 | ||
| 150 | results.forEach(resultItem => { | 153 | results.slice(0, maxSearchResults).forEach(resultItem => { |
| 151 | const item = window.searchDocuments.find(doc => doc.permalink === resultItem.ref); | 154 | const item = window.searchDocuments.find(doc => doc.permalink === resultItem.ref); |
| 152 | 155 | ||
| 153 | const link = document.createElement('a'); | 156 | const link = document.createElement('a'); |
