mirror of
https://github.com/andwati/pwnforfunandprofit.xyz.git
synced 2026-12-01 05:08:45 +00:00
Merge pull request #4 from andwati/dev
feat: Implement search functionality with elasticlunr integration and…
This commit is contained in:
@@ -0,0 +1,126 @@
|
||||
// Search overlay
|
||||
.search-overlay {
|
||||
position: fixed;
|
||||
top: 0;
|
||||
left: 0;
|
||||
right: 0;
|
||||
bottom: 0;
|
||||
background-color: rgba(0, 0, 0, 0.8);
|
||||
backdrop-filter: blur(4px);
|
||||
z-index: 9999;
|
||||
display: none;
|
||||
align-items: flex-start;
|
||||
justify-content: center;
|
||||
padding: 2rem;
|
||||
overflow-y: auto;
|
||||
|
||||
&.is-active {
|
||||
display: flex;
|
||||
}
|
||||
}
|
||||
|
||||
.search-modal {
|
||||
background-color: var(--bg-color);
|
||||
border: 1px solid var(--border-color);
|
||||
border-radius: 0.5rem;
|
||||
max-width: 42rem;
|
||||
width: 100%;
|
||||
margin-top: 10vh;
|
||||
box-shadow: 0 20px 60px rgba(0, 0, 0, 0.5);
|
||||
overflow: hidden;
|
||||
}
|
||||
|
||||
.search-header {
|
||||
display: flex;
|
||||
align-items: center;
|
||||
gap: 0.5rem;
|
||||
padding: 1rem;
|
||||
border-bottom: 1px solid var(--border-color);
|
||||
}
|
||||
|
||||
.search-input {
|
||||
flex: 1;
|
||||
font-size: 1.125rem;
|
||||
padding: 0.5rem;
|
||||
background: transparent;
|
||||
border: none;
|
||||
color: var(--text-color);
|
||||
outline: none;
|
||||
|
||||
&::placeholder {
|
||||
color: var(--text-muted);
|
||||
}
|
||||
}
|
||||
|
||||
.search-close {
|
||||
background: transparent;
|
||||
border: none;
|
||||
color: var(--text-muted);
|
||||
cursor: pointer;
|
||||
padding: 0.25rem;
|
||||
display: flex;
|
||||
align-items: center;
|
||||
justify-content: center;
|
||||
border-radius: 0.25rem;
|
||||
transition: background-color 0.2s ease;
|
||||
|
||||
&:hover {
|
||||
background-color: var(--border-color);
|
||||
color: var(--text-color);
|
||||
}
|
||||
}
|
||||
|
||||
.search-results {
|
||||
max-height: 60vh;
|
||||
overflow-y: auto;
|
||||
padding: 0.5rem;
|
||||
}
|
||||
|
||||
.search-result-item {
|
||||
display: block;
|
||||
padding: 1rem;
|
||||
border-radius: 0.375rem;
|
||||
text-decoration: none;
|
||||
color: var(--text-color);
|
||||
transition: background-color 0.2s ease;
|
||||
|
||||
&:hover {
|
||||
background-color: var(--border-color);
|
||||
}
|
||||
}
|
||||
|
||||
.search-result-title {
|
||||
font-size: 1rem;
|
||||
font-weight: 600;
|
||||
margin: 0 0 0.5rem 0;
|
||||
color: var(--accent-color);
|
||||
}
|
||||
|
||||
.search-result-snippet {
|
||||
font-size: 0.875rem;
|
||||
color: var(--text-muted);
|
||||
margin: 0;
|
||||
line-height: 1.5;
|
||||
}
|
||||
|
||||
.search-no-results {
|
||||
padding: 2rem;
|
||||
text-align: center;
|
||||
color: var(--text-muted);
|
||||
}
|
||||
|
||||
.search-footer {
|
||||
padding: 0.75rem 1rem;
|
||||
border-top: 1px solid var(--border-color);
|
||||
font-size: 0.75rem;
|
||||
color: var(--text-muted);
|
||||
text-align: center;
|
||||
|
||||
kbd {
|
||||
padding: 0.125rem 0.375rem;
|
||||
background-color: var(--border-color);
|
||||
border-radius: 0.25rem;
|
||||
font-family: var(--font-mono);
|
||||
font-size: 0.75rem;
|
||||
}
|
||||
}
|
||||
@@ -11,4 +11,5 @@
|
||||
@import 'feed';
|
||||
@import 'article';
|
||||
@import 'footer';
|
||||
@import 'search';
|
||||
@import 'utilities';
|
||||
|
||||
Vendored
+10
File diff suppressed because one or more lines are too long
@@ -0,0 +1,102 @@
|
||||
document.addEventListener('DOMContentLoaded', () => {
|
||||
if (typeof elasticlunr === 'undefined') return;
|
||||
|
||||
const searchOverlay = document.getElementById('search-overlay');
|
||||
const searchInput = document.getElementById('search-input');
|
||||
const searchResults = document.getElementById('search-results');
|
||||
const searchToggle = document.querySelector('.search-toggle');
|
||||
const searchClose = document.getElementById('search-close');
|
||||
|
||||
let searchIndex = null;
|
||||
|
||||
// Load search index
|
||||
fetch('/search_index.en.js')
|
||||
.then(response => response.json())
|
||||
.then(data => {
|
||||
searchIndex = elasticlunr.Index.load(data);
|
||||
})
|
||||
.catch(err => console.error('Failed to load search index:', err));
|
||||
|
||||
// Open search
|
||||
const openSearch = () => {
|
||||
if (!searchOverlay) return;
|
||||
searchOverlay.classList.add('is-active');
|
||||
setTimeout(() => searchInput?.focus(), 100);
|
||||
};
|
||||
|
||||
// Close search
|
||||
const closeSearch = () => {
|
||||
if (!searchOverlay) return;
|
||||
searchOverlay.classList.remove('is-active');
|
||||
searchInput.value = '';
|
||||
searchResults.innerHTML = '';
|
||||
};
|
||||
|
||||
// Perform search
|
||||
const performSearch = (query) => {
|
||||
if (!searchIndex || !query) {
|
||||
searchResults.innerHTML = '';
|
||||
return;
|
||||
}
|
||||
|
||||
const results = searchIndex.search(query, {
|
||||
fields: {
|
||||
title: { boost: 2 },
|
||||
body: { boost: 1 }
|
||||
},
|
||||
expand: true
|
||||
});
|
||||
|
||||
if (results.length === 0) {
|
||||
searchResults.innerHTML = '<div class="search-no-results">No results found</div>';
|
||||
return;
|
||||
}
|
||||
|
||||
const html = results.slice(0, 10).map(result => {
|
||||
const doc = searchIndex.documentStore.getDoc(result.ref);
|
||||
const snippet = doc.body.substring(0, 150);
|
||||
return `
|
||||
<a href="${doc.url}" class="search-result-item">
|
||||
<h3 class="search-result-title">${doc.title}</h3>
|
||||
<p class="search-result-snippet">${snippet}...</p>
|
||||
</a>
|
||||
`;
|
||||
}).join('');
|
||||
|
||||
searchResults.innerHTML = html;
|
||||
};
|
||||
|
||||
// Event listeners
|
||||
if (searchToggle) {
|
||||
searchToggle.addEventListener('click', openSearch);
|
||||
}
|
||||
|
||||
if (searchClose) {
|
||||
searchClose.addEventListener('click', closeSearch);
|
||||
}
|
||||
|
||||
if (searchOverlay) {
|
||||
searchOverlay.addEventListener('click', (e) => {
|
||||
if (e.target === searchOverlay) closeSearch();
|
||||
});
|
||||
}
|
||||
|
||||
if (searchInput) {
|
||||
searchInput.addEventListener('input', (e) => {
|
||||
performSearch(e.target.value.trim());
|
||||
});
|
||||
}
|
||||
|
||||
// Keyboard shortcuts
|
||||
document.addEventListener('keydown', (e) => {
|
||||
// Escape to close
|
||||
if (e.key === 'Escape' && searchOverlay?.classList.contains('is-active')) {
|
||||
closeSearch();
|
||||
}
|
||||
// Ctrl/Cmd + K to open
|
||||
if ((e.ctrlKey || e.metaKey) && e.key === 'k') {
|
||||
e.preventDefault();
|
||||
openSearch();
|
||||
}
|
||||
});
|
||||
});
|
||||
@@ -14,6 +14,10 @@
|
||||
{% include "partials/footer.html" %}
|
||||
</div>
|
||||
|
||||
{% include "partials/search.html" %}
|
||||
|
||||
<script src="{{ get_url(path="js/elasticlunr.min.js") }}"></script>
|
||||
<script src="{{ get_url(path="js/search.js") }}"></script>
|
||||
<script src="{{ get_url(path="js/theme.js") }}"></script>
|
||||
{% block scripts %}{% endblock scripts %}
|
||||
</body>
|
||||
|
||||
@@ -15,17 +15,13 @@
|
||||
</div>
|
||||
|
||||
<div class="nav-controls">
|
||||
{% if config.extra.enable_search %}
|
||||
<button class="search-toggle" aria-label="Search">
|
||||
<svg viewBox="0 0 24 24" width="18" height="18" stroke="currentColor" stroke-width="2" fill="none" stroke-linecap="round" stroke-linejoin="round"><circle cx="11" cy="11" r="8"></circle><line x1="21" y1="21" x2="16.65" y2="16.65"></line></svg>
|
||||
</button>
|
||||
{% endif %}
|
||||
{% if config.extra.enable_theme_toggle %}
|
||||
<button id="theme-toggle" class="theme-toggle hover-scale" aria-label="Toggle Dark Mode">
|
||||
<svg class="sun-icon" viewBox="0 0 24 24" width="18" height="18" stroke="currentColor" stroke-width="2" fill="none" stroke-linecap="round" stroke-linejoin="round"><circle cx="12" cy="12" r="10"></circle><path d="M12 2a10 10 0 0 0 0 20z" fill="currentColor"></path></svg>
|
||||
<svg class="moon-icon" viewBox="0 0 24 24" width="18" height="18" stroke="currentColor" stroke-width="2" fill="none" stroke-linecap="round" stroke-linejoin="round"><circle cx="12" cy="12" r="10"></circle><path d="M12 2a10 10 0 0 0 0 20z" fill="currentColor"></path></svg>
|
||||
</button>
|
||||
{% endif %}
|
||||
<button id="nav-toggle" class="nav-toggle" aria-label="Toggle navigation" aria-expanded="false">
|
||||
<svg class="hamburger-icon" viewBox="0 0 24 24" width="22" height="22" stroke="currentColor" stroke-width="2" fill="none" stroke-linecap="round"><line x1="3" y1="6" x2="21" y2="6"/><line x1="3" y1="12" x2="21" y2="12"/><line x1="3" y1="18" x2="21" y2="18"/></svg>
|
||||
<svg class="close-icon" viewBox="0 0 24 24" width="22" height="22" stroke="currentColor" stroke-width="2" fill="none" stroke-linecap="round"><line x1="18" y1="6" x2="6" y2="18"/><line x1="6" y1="6" x2="18" y2="18"/></svg>
|
||||
|
||||
@@ -0,0 +1,14 @@
|
||||
<div id="search-overlay" class="search-overlay">
|
||||
<div class="search-modal">
|
||||
<div class="search-header">
|
||||
<input type="search" id="search-input" class="search-input" placeholder="Search posts..." autocomplete="off" />
|
||||
<button id="search-close" class="search-close" aria-label="Close search">
|
||||
<svg viewBox="0 0 24 24" width="20" height="20" stroke="currentColor" stroke-width="2" fill="none" stroke-linecap="round"><line x1="18" y1="6" x2="6" y2="18"/><line x1="6" y1="6" x2="18" y2="18"/></svg>
|
||||
</button>
|
||||
</div>
|
||||
<div id="search-results" class="search-results"></div>
|
||||
<div class="search-footer">
|
||||
<kbd>ESC</kbd> to close
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
Reference in New Issue
Block a user