|
22 | 22 | }); |
23 | 23 | } |
24 | 24 |
|
25 | | - // Search functionality |
26 | | - const searchBtn = document.getElementById('search-btn'); |
27 | | - const searchModal = document.getElementById('search-modal'); |
28 | | - const searchClose = document.getElementById('search-close'); |
29 | | - const searchInput = document.getElementById('search-input'); |
30 | | - const searchResults = document.getElementById('search-results'); |
31 | | - |
32 | | - // Debug: Check if elements exist |
33 | | - console.log('Search button:', searchBtn); |
34 | | - console.log('Search modal:', searchModal); |
35 | | - console.log('Search close:', searchClose); |
36 | | - console.log('Search input:', searchInput); |
37 | | - console.log('Search results:', searchResults); |
38 | | - |
39 | | - // Open search modal |
40 | | - if (searchBtn) { |
41 | | - console.log('Adding click listener to search button'); |
42 | | - searchBtn.addEventListener('click', (e) => { |
43 | | - console.log('Search button clicked!', e); |
44 | | - e.preventDefault(); |
45 | | - e.stopPropagation(); |
46 | | - if (searchModal) { |
47 | | - searchModal.style.display = 'flex'; |
48 | | - if (searchInput) searchInput.focus(); |
49 | | - } |
50 | | - }); |
51 | | - } else { |
52 | | - console.log('Search button not found!'); |
53 | | - } |
54 | | - |
55 | | - // Close search modal |
56 | | - if (searchClose) { |
57 | | - searchClose.addEventListener('click', () => { |
58 | | - if (searchModal) { |
59 | | - searchModal.style.display = 'none'; |
60 | | - if (searchInput) searchInput.value = ''; |
61 | | - if (searchResults) searchResults.innerHTML = ''; |
62 | | - } |
63 | | - }); |
64 | | - } |
65 | | - |
66 | | - // Close on escape key |
67 | | - document.addEventListener('keydown', (e) => { |
68 | | - if (e.key === 'Escape' && searchModal && searchModal.style.display === 'flex') { |
69 | | - searchModal.style.display = 'none'; |
70 | | - if (searchInput) searchInput.value = ''; |
71 | | - if (searchResults) searchResults.innerHTML = ''; |
72 | | - } |
73 | | - }); |
74 | | - |
75 | | - // Close on background click |
76 | | - if (searchModal) { |
77 | | - searchModal.addEventListener('click', (e) => { |
78 | | - if (e.target === searchModal) { |
79 | | - searchModal.style.display = 'none'; |
80 | | - if (searchInput) searchInput.value = ''; |
81 | | - if (searchResults) searchResults.innerHTML = ''; |
82 | | - } |
83 | | - }); |
84 | | - } |
85 | | - |
86 | | - // Search functionality |
87 | | - if (searchInput) { |
88 | | - searchInput.addEventListener('input', () => { |
89 | | - const query = searchInput.value.toLowerCase().trim(); |
90 | | - |
91 | | - if (query.length < 2) { |
92 | | - if (searchResults) searchResults.innerHTML = ''; |
93 | | - return; |
94 | | - } |
95 | | - |
96 | | - // Simple search through page content |
97 | | - const allPosts = document.querySelectorAll('.blog-card, .archive-item'); |
98 | | - const results = []; |
99 | | - |
100 | | - allPosts.forEach(post => { |
101 | | - const title = post.querySelector('.card-title a, .archive-title a'); |
102 | | - const content = post.querySelector('.card-excerpt, .archive-excerpt'); |
103 | | - |
104 | | - if (title && content) { |
105 | | - const titleText = title.textContent.toLowerCase(); |
106 | | - const contentText = content.textContent.toLowerCase(); |
107 | | - |
108 | | - if (titleText.includes(query) || contentText.includes(query)) { |
109 | | - results.push({ |
110 | | - title: title.textContent, |
111 | | - url: title.href, |
112 | | - excerpt: content.textContent.substring(0, 150) + '...' |
113 | | - }); |
114 | | - } |
115 | | - } |
116 | | - }); |
117 | | - |
118 | | - displaySearchResults(results, query); |
119 | | - }); |
120 | | - } |
121 | | - |
122 | | - function displaySearchResults(results, query) { |
123 | | - if (!searchResults) return; |
124 | | - |
125 | | - if (results.length === 0) { |
126 | | - searchResults.innerHTML = '<div class="search-no-results">No posts found</div>'; |
127 | | - return; |
128 | | - } |
129 | | - |
130 | | - const resultsHtml = results.map(post => { |
131 | | - const title = highlightText(post.title, query); |
132 | | - const highlightedDesc = highlightText(post.excerpt, query); |
133 | | - |
134 | | - return ` |
135 | | - <div class="search-result-item"> |
136 | | - <h4><a href="${post.url}">${title}</a></h4> |
137 | | - <p class="search-result-excerpt">${highlightedDesc}</p> |
138 | | - </div> |
139 | | - `; |
140 | | - }).join(''); |
141 | | - |
142 | | - searchResults.innerHTML = resultsHtml; |
143 | | - } |
144 | | - |
145 | | - function highlightText(text, query) { |
146 | | - const regex = new RegExp(`(${query})`, 'gi'); |
147 | | - return text.replace(regex, '<mark>$1</mark>'); |
148 | | - } |
149 | | - |
150 | 25 | // Smooth scroll for anchor links |
151 | 26 | document.querySelectorAll('a[href^="#"]').forEach(anchor => { |
152 | 27 | anchor.addEventListener('click', function(e) { |
|
0 commit comments