diff options
| author | Danilo M. <danix@danix.xyz> | 2026-04-22 12:43:22 +0200 |
|---|---|---|
| committer | Danilo M. <danix@danix.xyz> | 2026-04-22 12:43:22 +0200 |
| commit | 5b476f8905f411768e23cb01d577a60e5a5fd725 (patch) | |
| tree | 0a08cc83d809dbea714f52826e822501ee7c0165 /themes/danix-xyz-hacker/assets/js/search.js | |
| parent | 082e9246ffe453031894d32d3cee9d5d1bf2b67a (diff) | |
| download | danixxyz-5b476f8905f411768e23cb01d577a60e5a5fd725.tar.gz danixxyz-5b476f8905f411768e23cb01d577a60e5a5fd725.zip | |
chore: extract theme into git submodule (danix2-hugo-theme)
Diffstat (limited to 'themes/danix-xyz-hacker/assets/js/search.js')
| -rw-r--r-- | themes/danix-xyz-hacker/assets/js/search.js | 134 |
1 files changed, 0 insertions, 134 deletions
diff --git a/themes/danix-xyz-hacker/assets/js/search.js b/themes/danix-xyz-hacker/assets/js/search.js deleted file mode 100644 index 8fb6262..0000000 --- a/themes/danix-xyz-hacker/assets/js/search.js +++ /dev/null @@ -1,134 +0,0 @@ -// Lazy-load search index from JSON file (language-aware) -async function loadSearchIndex() { - if (window.searchIndex) { - return window.searchIndex; - } - try { - // Detect current language from URL - const isItalian = window.location.pathname.startsWith('/it/'); - const indexPath = isItalian ? '/it/search-index.json' : '/search-index.json'; - - const response = await fetch(indexPath); - if (!response.ok) throw new Error('Failed to load search index'); - window.searchIndex = await response.json(); - return window.searchIndex; - } catch (error) { - console.error('Error loading search index:', error); - return []; - } -} - -// Filter articles by query (case-insensitive, max 5 results) -function filterArticles(query, articles) { - if (!query.trim()) { - return []; - } - const lowerQuery = query.toLowerCase(); - return articles - .filter(article => - article.title.toLowerCase().includes(lowerQuery) || - article.summary.toLowerCase().includes(lowerQuery) - ) - .slice(0, 5); -} - -// Register Alpine.js components -document.addEventListener('alpine:init', () => { - // Desktop search modal component - Alpine.data('searchOverlay', () => ({ - isOpen: false, - searchQuery: '', - filteredArticles: [], - allArticles: [], - indexLoaded: false, - - async open() { - this.isOpen = true; - await this.ensureIndexLoaded(); - this.$nextTick(() => { - const input = this.$el.querySelector('#search-input-desktop'); - if (input) input.focus(); - }); - }, - - close() { - this.isOpen = false; - this.searchQuery = ''; - this.filteredArticles = []; - }, - - async ensureIndexLoaded() { - if (!this.indexLoaded) { - this.allArticles = await loadSearchIndex(); - this.indexLoaded = true; - } - }, - - filterArticles(query) { - this.searchQuery = query; - this.filteredArticles = filterArticles(query, this.allArticles); - }, - - handleEscape(event) { - if (event.key === 'Escape') { - this.close(); - } - } - })); - - // Mobile search component (integrated into hamburger menu) - Alpine.data('mobileSearch', () => ({ - searchQuery: '', - filteredArticles: [], - allArticles: [], - indexLoaded: false, - - async ensureIndexLoaded() { - if (!this.indexLoaded) { - this.allArticles = await loadSearchIndex(); - this.indexLoaded = true; - } - }, - - filterArticles(query) { - this.searchQuery = query; - this.filteredArticles = filterArticles(query, this.allArticles); - } - })); - - // Refactored 404 page component - Alpine.data('notFoundPage', () => ({ - showEasterEgg: false, - searchQuery: '', - filteredArticles: [], - allArticles: [], - indexLoaded: false, - - async init() { - await this.ensureIndexLoaded(); - }, - - async ensureIndexLoaded() { - if (!this.indexLoaded) { - this.allArticles = await loadSearchIndex(); - this.indexLoaded = true; - } - }, - - filterArticles(query) { - this.searchQuery = query; - this.filteredArticles = filterArticles(query, this.allArticles); - }, - - toggleEasterEgg() { - this.showEasterEgg = !this.showEasterEgg; - }, - - goToRandomArticle() { - if (this.allArticles.length > 0) { - const randomArticle = this.allArticles[Math.floor(Math.random() * this.allArticles.length)]; - window.location.href = randomArticle.url; - } - } - })); -}); |
