Hello. I would like to add a live search component for my website, similar to the one on the MDN website (https://developer.mozilla.org/en-US/).
This is what I have so far:
---
import { SITE_TITLE } from "../consts";
import { getCollection } from 'astro:content';
async function searchPost(term) {
const posts = (await getCollection('blog')).filter(
article => article.data.title.includes(term)
);
return posts
}
---
<header>
<div class="container">
<h2>
<a href="/">{SITE_TITLE}</a>
</h2>
<label for="search-articles">Search for an article:</label>
<input type="search" id="search-articles" placeholder="first post" />
</div>
</header>
<script>
const searchArticles = document.querySelector("#search-articles");
searchArticles.addEventListener("change", event => {
searchPost(event.target.value);
})
</script>
I'm not sure how I would effectively create the search functionality. Can someone help?