Skip to content

Commit 52832f8

Browse files
committed
feat: responsive
1 parent 5464cde commit 52832f8

File tree

1 file changed

+44
-0
lines changed

1 file changed

+44
-0
lines changed

theme/templates/base.html

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,50 @@
3939
});
4040
});
4141
});
42+
43+
document.addEventListener("DOMContentLoaded", () => {
44+
const searchInput = document.getElementById("search-bar");
45+
const resultsContainer = document.createElement("div");
46+
resultsContainer.className = "search-results";
47+
searchInput.parentNode.appendChild(resultsContainer);
48+
49+
let articles = [];
50+
51+
// Load JSON
52+
fetch("{{ SITEURL }}/search.json")
53+
.then(response => response.json())
54+
.then(data => { articles = data; });
55+
56+
// Search logic
57+
searchInput.addEventListener("input", () => {
58+
const query = searchInput.value.toLowerCase().trim();
59+
resultsContainer.innerHTML = "";
60+
61+
if (!query) return;
62+
63+
const words = query.split(/\s+/);
64+
65+
// ✅ At least one word must match title, summary, or tags
66+
const matches = articles.filter(article =>
67+
words.some(word =>
68+
(article.title && article.title.toLowerCase().includes(word)) ||
69+
(article.summary && article.summary.toLowerCase().includes(word)) ||
70+
(article.tags && article.tags.join(" ").toLowerCase().includes(word))
71+
)
72+
);
73+
74+
if (matches.length === 0) {
75+
resultsContainer.innerHTML = "<div>No results found</div>";
76+
} else {
77+
matches.forEach(article => {
78+
const item = document.createElement("div");
79+
item.className = "search-result";
80+
item.innerHTML = `<a href="${article.url}">${article.title}</a>`;
81+
resultsContainer.appendChild(item);
82+
});
83+
}
84+
});
85+
});
4286
</script>
4387

4488
</head>

0 commit comments

Comments
 (0)