37 lines
1 KiB
JavaScript
37 lines
1 KiB
JavaScript
// Simple Search
|
|
const searchInput = document.getElementById('search');
|
|
const results = document.getElementById('results');
|
|
|
|
if (searchInput && results) {
|
|
const posts = [
|
|
{{ range $index, $page := where site.RegularPages "Type" "in" site.Params.mainSections }}
|
|
{{ if $index }},{{ end }}
|
|
{
|
|
title: "{{ $page.Title | safeJS }}",
|
|
url: "{{ $page.RelPermalink }}",
|
|
content: {{ $page.Plain | safeJS | jsonify }}
|
|
}
|
|
{{ end }}
|
|
];
|
|
|
|
searchInput.addEventListener('input', (e) => {
|
|
const query = e.target.value.toLowerCase();
|
|
results.innerHTML = '';
|
|
|
|
if (query.length < 2) return;
|
|
|
|
const matches = posts.filter(post => {
|
|
return post.title.toLowerCase().includes(query) ||
|
|
post.content.toLowerCase().includes(query);
|
|
});
|
|
|
|
matches.forEach(post => {
|
|
const li = document.createElement('li');
|
|
const a = document.createElement('a');
|
|
a.href = post.url;
|
|
a.textContent = post.title;
|
|
li.appendChild(a);
|
|
results.appendChild(li);
|
|
});
|
|
});
|
|
}
|