mirror of
https://github.com/ChenQihan666/MyBlog-Next.git
synced 2026-08-14 11:29:33 +08:00
Add headings in search result
This commit is contained in:
@@ -8,6 +8,7 @@ import type { Frontmatter } from "src/types";
|
|||||||
type SearchItem = {
|
type SearchItem = {
|
||||||
title: string;
|
title: string;
|
||||||
description: string;
|
description: string;
|
||||||
|
headings: string[];
|
||||||
frontmatter: Frontmatter;
|
frontmatter: Frontmatter;
|
||||||
};
|
};
|
||||||
|
|
||||||
@@ -31,7 +32,7 @@ export default function SearchBar({ searchList }: Props) {
|
|||||||
};
|
};
|
||||||
|
|
||||||
const fuse = new Fuse(searchList, {
|
const fuse = new Fuse(searchList, {
|
||||||
keys: ["title", "description"],
|
keys: ["title", "description", "headings"],
|
||||||
includeMatches: true,
|
includeMatches: true,
|
||||||
minMatchCharLength: 2,
|
minMatchCharLength: 2,
|
||||||
threshold: 0.5,
|
threshold: 0.5,
|
||||||
|
|||||||
@@ -15,10 +15,11 @@ const posts = await Astro.glob<Frontmatter>("../contents/*.md");
|
|||||||
// List of items to search in
|
// List of items to search in
|
||||||
const searchList = posts
|
const searchList = posts
|
||||||
.filter(({ frontmatter }) => !frontmatter.draft)
|
.filter(({ frontmatter }) => !frontmatter.draft)
|
||||||
.map(({ frontmatter }) => ({
|
.map((post) => ({
|
||||||
title: frontmatter.title,
|
title: post.frontmatter.title,
|
||||||
description: frontmatter.description,
|
description: post.frontmatter.description,
|
||||||
frontmatter,
|
headings: post.getHeadings().map((h) => h.text),
|
||||||
|
frontmatter: post.frontmatter,
|
||||||
}));
|
}));
|
||||||
---
|
---
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user