|
40 | 40 | }); |
41 | 41 | }); |
42 | 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 | | - }); |
| 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 | + fetch("{{ SITEURL }}/search.json") |
| 52 | + .then(response => response.json()) |
| 53 | + .then(data => { articles = data; }); |
| 54 | + |
| 55 | + searchInput.addEventListener("input", () => { |
| 56 | + |
| 57 | + const query = searchInput.value.toLowerCase().trim(); |
| 58 | + |
| 59 | + if (!query) return; |
| 60 | + |
| 61 | + const words = query.split(/\s+/); |
| 62 | + |
| 63 | + // Almenos una coincidencia con title, summary, tags |
| 64 | + const matches = articles.filter(article => |
| 65 | + words.some(word => |
| 66 | + (article.title && article.title.toLowerCase().includes(word)) || |
| 67 | + (article.summary && article.summary.toLowerCase().includes(word)) || |
| 68 | + (article.tags && article.tags.join(" ").toLowerCase().includes(word)) |
| 69 | + )); |
| 70 | + |
| 71 | + if (matches.length !== 0) { |
| 72 | + |
| 73 | + const previewContainer = document.querySelector(".preview-container"); |
| 74 | + previewContainer.innerHTML = ""; |
| 75 | + |
| 76 | + matches.forEach(article => { |
| 77 | + const li = document.createElement("li"); |
| 78 | + li.className = "preview-post"; |
| 79 | + li.innerHTML = ` |
| 80 | + <a class="preview-link" href="${article.url}"> |
| 81 | + <img class="preview-img" src="${article.image || '{{ SITEURL }}/img/otros/default.webp'}" alt=""> |
| 82 | + <div class="preview-title">${article.title}</div> |
| 83 | + <div class="preview-summary">${article.summary || ""}</div> |
| 84 | + <div class="preview-author">${article.date || ""} ${article.author ? "Por " + article.author : ""}</div> |
| 85 | + <div class="preview-tags-container"> |
| 86 | + ${(article.tags || []).map(tag => `<div class="preview-tag">${tag}</div>`).join("")} |
| 87 | + </div> |
| 88 | + </a> |
| 89 | + `; |
| 90 | + previewContainer.appendChild(li); |
| 91 | + }); |
| 92 | + } |
| 93 | + }); |
85 | 94 | }); |
86 | 95 | </script> |
87 | 96 |
|
|
0 commit comments