From db385b6567a789667df613e2e928daf4a4277ed6 Mon Sep 17 00:00:00 2001 From: satnaing Date: Sat, 28 Jan 2023 00:58:40 +0630 Subject: [PATCH] refactor: update to AstroV2 and remove headings on search result Update imports and types according to Astro v2 content collections. Remove headings on search result for better search accuracy. --- src/components/Search.tsx | 15 +++++++-------- src/pages/search.astro | 17 ++++++++--------- 2 files changed, 15 insertions(+), 17 deletions(-) diff --git a/src/components/Search.tsx b/src/components/Search.tsx index d8e94d1..6437c5d 100644 --- a/src/components/Search.tsx +++ b/src/components/Search.tsx @@ -2,13 +2,12 @@ import Fuse from "fuse.js"; import { useEffect, useRef, useState } from "react"; import Card from "@components/Card"; import slugify from "@utils/slugify"; -import type { Frontmatter } from "src/types"; +import type { BlogFrontmatter } from "@content/_schemas"; -type SearchItem = { +export type SearchItem = { title: string; description: string; - headings: string[]; - frontmatter: Frontmatter; + data: BlogFrontmatter; }; interface Props { @@ -32,7 +31,7 @@ export default function SearchBar({ searchList }: Props) { }; const fuse = new Fuse(searchList, { - keys: ["title", "description", "headings"], + keys: ["title", "description"], includeMatches: true, minMatchCharLength: 2, threshold: 0.5, @@ -108,9 +107,9 @@ export default function SearchBar({ searchList }: Props) { {searchResults && searchResults.map(({ item, refIndex }) => ( ))} diff --git a/src/pages/search.astro b/src/pages/search.astro index 2a2bb10..c4f8ebb 100644 --- a/src/pages/search.astro +++ b/src/pages/search.astro @@ -1,23 +1,22 @@ --- -import { SITE } from "src/config"; +import { getCollection } from "astro:content"; +import { SITE } from "@config"; import Layout from "@layouts/Layout.astro"; import Main from "@layouts/Main.astro"; import Header from "@components/Header.astro"; import Footer from "@components/Footer.astro"; import Search from "@components/Search"; -import type { Frontmatter } from "src/types"; // Retrieve all articles -const posts = await Astro.glob("../contents/**/*.md"); +const posts = await getCollection("blog"); // List of items to search in const searchList = posts - .filter(({ frontmatter }) => !frontmatter.draft) - .map(post => ({ - title: post.frontmatter.title, - description: post.frontmatter.description, - headings: post.getHeadings().map(h => h.text), - frontmatter: post.frontmatter, + .filter(({ data }) => !data.draft) + .map(({ data }) => ({ + title: data.title, + description: data.description, + data, })); ---