|
11 | 11 | const urlParams = new URLSearchParams(window.location.search); |
12 | 12 | // Получаем конкретные значения |
13 | 13 | const query = urlParams.get('query'); |
14 | | - const posts = await fetch("/search.json"); |
15 | | - for post in posts { |
16 | | - if post["title"].prototype.includes(query) { |
| 14 | + |
| 15 | + fetch("/search.json") |
| 16 | + .then(response => response.json()) |
| 17 | + .then(posts => { |
| 18 | + const resultsContainer = document.getElementById('results'); |
17 | 19 |
|
18 | | - } |
19 | | - } |
| 20 | + const filteredPosts = posts.filter(post => |
| 21 | + post.title.toLowerCase().includes(query.toLowerCase()) || |
| 22 | + (post.description && post.description.toLowerCase().includes(query.toLowerCase())) || |
| 23 | + (post.excerpt && post.excerpt.toLowerCase().includes(query.toLowerCase())) |
| 24 | + ); |
| 25 | + |
| 26 | + filteredPosts.forEach(post => { |
| 27 | + // Создаем элемент списка |
| 28 | + const listItem = document.createElement('li'); |
| 29 | + listItem.className = 'article-list-item'; |
| 30 | + |
| 31 | + // Создаем ссылку |
| 32 | + const link = document.createElement('a'); |
| 33 | + link.href = post.url; |
| 34 | + link.title = post.title; |
| 35 | + |
| 36 | + // Создаем заголовок |
| 37 | + const heading = document.createElement('h5'); |
| 38 | + heading.textContent = post.title; |
| 39 | + |
| 40 | + // Добавляем стрелочку (предполагая, что это SVG иконка) |
| 41 | + const arrowIcon = document.createElement('span'); |
| 42 | + arrowIcon.innerHTML = '{% include svg-icon.html icon="arrow-right" %}'; |
| 43 | + heading.appendChild(arrowIcon); |
| 44 | + |
| 45 | + link.appendChild(heading); |
| 46 | + |
| 47 | + // Создаем описание |
| 48 | + const description = document.createElement('p'); |
| 49 | + description.textContent = post.description || post.excerpt; |
| 50 | + |
| 51 | + // Создаем footer (упрощенная версия) |
| 52 | + const footer = document.createElement('div'); |
| 53 | + footer.className = 'article-info-footer'; |
| 54 | + // Здесь можно добавить дату, теги и т.д. |
| 55 | + if (post.date) { |
| 56 | + const dateSpan = document.createElement('span'); |
| 57 | + dateSpan.className = 'article-date'; |
| 58 | + dateSpan.textContent = new Date(post.date).toLocaleDateString(); |
| 59 | + footer.appendChild(dateSpan); |
| 60 | + } |
| 61 | + |
| 62 | + // Собираем все вместе |
| 63 | + listItem.appendChild(link); |
| 64 | + listItem.appendChild(description); |
| 65 | + listItem.appendChild(footer); |
| 66 | + |
| 67 | + resultsContainer.appendChild(listItem); |
| 68 | + }); |
| 69 | + }) |
| 70 | + .catch(error => { |
| 71 | + console.error('Ошибка при загрузке постов:', error); |
| 72 | + }); |
20 | 73 | </script> |
0 commit comments