Our great sponsors
-
SurveyJS
Open-Source JSON Form Builder to Create Dynamic Forms Right in Your App. With SurveyJS form UI libraries, you can build and style forms in a fully-integrated drag & drop form builder, render them in your JS app, and store form submission data in any backend, inc. PHP, ASP.NET Core, and Node.js.
-
iconify
Universal icon framework. One syntax for FontAwesome, Material Design Icons, DashIcons, Feather Icons, EmojiOne, Noto Emoji and many other open source icon sets (over 150 icon sets and 200k icons). SVG framework, React, Vue and Svelte components!
- {post.frontmatter.title}a> {post.frontmatter.description} li> ))} ul> ); } export default Search;
Note: The source code for this project is available on this GitHub repository.
To do this, install the Astro Tailwind integration, and the typography plugin to modify the styles in markdown rendered pages, like the posts.
For now, don't worry about the styles. We'll be applying Tailwind CSS later in the tutorial.
I'll also use a search icon from Iconify, one of the largest collections of open-source icons.
import Fuse from 'fuse.js'; import { useState } from 'react'; // Configs fuse.js // https://fusejs.io/api/options.html const options = { keys: ['frontmatter.title', 'frontmatter.description', 'frontmatter.slug'], includeMatches: true, minMatchCharLength: 2, threshold: 0.5, }; function Search({ searchList }) { // User's input const [query, setQuery] = useState(''); const fuse = new Fuse(searchList, options); // Set a limit to the posts: 5 const posts = fuse .search(query) .map((result) => result.item) .slice(0, 5); function handleOnSearch({ target = {} }) { const { value } = target; setQuery(value); } return ( <> Searchlabel> {query.length > 1 && (
Found {posts.length} {posts.length === 1 ? 'result' : 'results'} for '{query}' p> )}
{posts && posts.map((post) => (
Fortunately, the Astro web framework has an API, where you can fetch the markdown or MDX files you need. After fetching all the data, we'll able to search for a specific query provided by our users.