|
39 | 39 | }); |
40 | 40 | }); |
41 | 41 | }); |
| 42 | + |
| 43 | + document.addEventListener("DOMContentLoaded", () => { |
| 44 | + const searchInput = document.getElementById("search-bar"); |
| 45 | + const resultsContainer = document.createElement("div"); |
| 46 | + resultsContainer.className = "search-results"; |
| 47 | + searchInput.parentNode.appendChild(resultsContainer); |
| 48 | + |
| 49 | + let articles = []; |
| 50 | + |
| 51 | + // Load JSON |
| 52 | + fetch("{{ SITEURL }}/search.json") |
| 53 | + .then(response => response.json()) |
| 54 | + .then(data => { articles = data; }); |
| 55 | + |
| 56 | + // Search logic |
| 57 | + searchInput.addEventListener("input", () => { |
| 58 | + const query = searchInput.value.toLowerCase().trim(); |
| 59 | + resultsContainer.innerHTML = ""; |
| 60 | + |
| 61 | + if (!query) return; |
| 62 | + |
| 63 | + const words = query.split(/\s+/); |
| 64 | + |
| 65 | + // ✅ At least one word must match title, summary, or tags |
| 66 | + const matches = articles.filter(article => |
| 67 | + words.some(word => |
| 68 | + (article.title && article.title.toLowerCase().includes(word)) || |
| 69 | + (article.summary && article.summary.toLowerCase().includes(word)) || |
| 70 | + (article.tags && article.tags.join(" ").toLowerCase().includes(word)) |
| 71 | + ) |
| 72 | + ); |
| 73 | + |
| 74 | + if (matches.length === 0) { |
| 75 | + resultsContainer.innerHTML = "<div>No results found</div>"; |
| 76 | + } else { |
| 77 | + matches.forEach(article => { |
| 78 | + const item = document.createElement("div"); |
| 79 | + item.className = "search-result"; |
| 80 | + item.innerHTML = `<a href="${article.url}">${article.title}</a>`; |
| 81 | + resultsContainer.appendChild(item); |
| 82 | + }); |
| 83 | + } |
| 84 | + }); |
| 85 | +}); |
42 | 86 | </script> |
43 | 87 |
|
44 | 88 | </head> |
|
0 commit comments