From d275f607a3f2851cf4355b922019a25227a7b011 Mon Sep 17 00:00:00 2001 From: Sat Naing Date: Sun, 25 Sep 2022 19:44:36 +0630 Subject: [PATCH] Add headings in search result --- src/components/Search.tsx | 3 ++- src/pages/search.astro | 9 +++++---- 2 files changed, 7 insertions(+), 5 deletions(-) diff --git a/src/components/Search.tsx b/src/components/Search.tsx index 82f3273..b7098af 100644 --- a/src/components/Search.tsx +++ b/src/components/Search.tsx @@ -8,6 +8,7 @@ import type { Frontmatter } from "src/types"; type SearchItem = { title: string; description: string; + headings: string[]; frontmatter: Frontmatter; }; @@ -31,7 +32,7 @@ export default function SearchBar({ searchList }: Props) { }; const fuse = new Fuse(searchList, { - keys: ["title", "description"], + keys: ["title", "description", "headings"], includeMatches: true, minMatchCharLength: 2, threshold: 0.5, diff --git a/src/pages/search.astro b/src/pages/search.astro index b312757..fd75ad9 100644 --- a/src/pages/search.astro +++ b/src/pages/search.astro @@ -15,10 +15,11 @@ const posts = await Astro.glob("../contents/*.md"); // List of items to search in const searchList = posts .filter(({ frontmatter }) => !frontmatter.draft) - .map(({ frontmatter }) => ({ - title: frontmatter.title, - description: frontmatter.description, - frontmatter, + .map((post) => ({ + title: post.frontmatter.title, + description: post.frontmatter.description, + headings: post.getHeadings().map((h) => h.text), + frontmatter: post.frontmatter, })); ---