|
| 1 | +(() => { |
| 2 | + 'use strict'; |
| 3 | + |
| 4 | + let searchIndex = null; |
| 5 | + let searchData = null; |
| 6 | + const resultsContainer = document.getElementById('search-results'); |
| 7 | + const searchInput = document.getElementById('search-input'); |
| 8 | + const searchForm = document.getElementById('search-form'); |
| 9 | + |
| 10 | + // Get base URL from the search form action |
| 11 | + const baseUrl = searchForm.action.replace(/\/search\/$/, ''); |
| 12 | + |
| 13 | + // Load search data and build index |
| 14 | + async function loadSearchIndex() { |
| 15 | + if (searchIndex) return; |
| 16 | + |
| 17 | + const response = await fetch(`${baseUrl}/search.json`); |
| 18 | + searchData = await response.json(); |
| 19 | + |
| 20 | + searchIndex = lunr(function() { |
| 21 | + this.ref('url'); |
| 22 | + this.field('title', { boost: 10 }); |
| 23 | + this.field('content'); |
| 24 | + |
| 25 | + searchData.forEach(doc => this.add(doc)); |
| 26 | + }); |
| 27 | + } |
| 28 | + |
| 29 | + // Escape HTML to prevent XSS |
| 30 | + function escapeHtml(text) { |
| 31 | + const div = document.createElement('div'); |
| 32 | + div.textContent = text; |
| 33 | + return div.innerHTML; |
| 34 | + } |
| 35 | + |
| 36 | + // Extract excerpt around first match and highlight terms |
| 37 | + function getExcerpt(content, terms) { |
| 38 | + const lowerContent = content.toLowerCase(); |
| 39 | + let position = -1; |
| 40 | + |
| 41 | + for (const term of terms) { |
| 42 | + const pos = lowerContent.indexOf(term.toLowerCase()); |
| 43 | + if (pos !== -1 && (position === -1 || pos < position)) { |
| 44 | + position = pos; |
| 45 | + } |
| 46 | + } |
| 47 | + |
| 48 | + if (position === -1) position = 0; |
| 49 | + |
| 50 | + const start = Math.max(0, position - 60); |
| 51 | + const end = Math.min(content.length, position + 200); |
| 52 | + let excerpt = content.substring(start, end); |
| 53 | + |
| 54 | + if (start > 0) excerpt = '…' + excerpt; |
| 55 | + if (end < content.length) excerpt += '…'; |
| 56 | + |
| 57 | + // Highlight matching terms |
| 58 | + let highlighted = escapeHtml(excerpt); |
| 59 | + for (const term of terms) { |
| 60 | + const regex = new RegExp(`(${term.replace(/[.*+?^${}()|[\]\\]/g, '\\$&')})`, 'gi'); |
| 61 | + highlighted = highlighted.replace(regex, '<mark>$1</mark>'); |
| 62 | + } |
| 63 | + |
| 64 | + return highlighted; |
| 65 | + } |
| 66 | + |
| 67 | + // Display search results |
| 68 | + function displayResults(results, query) { |
| 69 | + if (!results.length) { |
| 70 | + resultsContainer.innerHTML = `<p class="search-no-results">No results found for "${escapeHtml(query)}"</p>`; |
| 71 | + return; |
| 72 | + } |
| 73 | + |
| 74 | + const terms = query.toLowerCase().split(/\s+/).filter(t => t.length > 1); |
| 75 | + const count = results.length; |
| 76 | + const plural = count === 1 ? '' : 's'; |
| 77 | + |
| 78 | + let html = `<p class="search-summary">Found ${count} result${plural} for "${escapeHtml(query)}"</p>`; |
| 79 | + html += '<ul class="search-results-list">'; |
| 80 | + |
| 81 | + for (const result of results) { |
| 82 | + const doc = searchData.find(d => d.url === result.ref); |
| 83 | + if (!doc) continue; |
| 84 | + |
| 85 | + const excerpt = getExcerpt(doc.content, terms); |
| 86 | + const path = doc.url.replace(/^\/csswg-wiki/, '').replace(/\/$/, '') || '/'; |
| 87 | + |
| 88 | + html += ` |
| 89 | + <li class="search-result"> |
| 90 | + <a href="${escapeHtml(doc.url)}" class="search-result-title">${escapeHtml(doc.title)}</a> |
| 91 | + <span class="search-result-path">${escapeHtml(path)}</span> |
| 92 | + <p class="search-result-excerpt">${excerpt}</p> |
| 93 | + </li>`; |
| 94 | + } |
| 95 | + |
| 96 | + html += '</ul>'; |
| 97 | + resultsContainer.innerHTML = html; |
| 98 | + } |
| 99 | + |
| 100 | + // Perform search |
| 101 | + async function doSearch(query) { |
| 102 | + if (!query || query.length < 2) { |
| 103 | + resultsContainer.innerHTML = '<p class="search-hint">Enter at least 2 characters to search.</p>'; |
| 104 | + return; |
| 105 | + } |
| 106 | + |
| 107 | + resultsContainer.innerHTML = '<p class="search-loading">Searching…</p>'; |
| 108 | + |
| 109 | + try { |
| 110 | + await loadSearchIndex(); |
| 111 | + |
| 112 | + let results; |
| 113 | + try { |
| 114 | + results = searchIndex.search(query); |
| 115 | + } catch { |
| 116 | + // Handle Lunr parse errors by doing a simpler search |
| 117 | + results = searchIndex.search(query.replace(/[^\w\s]/g, '')); |
| 118 | + } |
| 119 | + |
| 120 | + displayResults(results, query); |
| 121 | + } catch (err) { |
| 122 | + resultsContainer.innerHTML = '<p class="search-error">Error loading search index.</p>'; |
| 123 | + console.error(err); |
| 124 | + } |
| 125 | + } |
| 126 | + |
| 127 | + // Handle form submission |
| 128 | + searchForm.addEventListener('submit', e => { |
| 129 | + e.preventDefault(); |
| 130 | + const query = searchInput.value.trim(); |
| 131 | + history.replaceState(null, '', `?q=${encodeURIComponent(query)}`); |
| 132 | + doSearch(query); |
| 133 | + }); |
| 134 | + |
| 135 | + // Check for query parameter on page load |
| 136 | + const params = new URLSearchParams(window.location.search); |
| 137 | + const initialQuery = params.get('q'); |
| 138 | + if (initialQuery) { |
| 139 | + searchInput.value = initialQuery; |
| 140 | + doSearch(initialQuery); |
| 141 | + } |
| 142 | +})(); |
0 commit comments