add the search function to website

This commit is contained in:
PrinOrange
2024-01-06 11:47:18 +08:00
parent acdc77260f
commit 4fdba9d2b9
8 changed files with 1118 additions and 21 deletions

33
lib/search.ts Normal file
View File

@@ -0,0 +1,33 @@
import minisearch from "minisearch";
import { cutForSearch } from "nodejs-jieba";
import { getPostFileContent, sortedPosts } from "./post-process";
function tokenizer(str: string) {
return cutForSearch(str, true);
}
function makeSearchIndex() {
let miniSearch = new minisearch({
fields: ["id", "title", "tags", "subtitle", "summary", "content"],
storeFields: ["id", "title", "tags"],
tokenize: tokenizer,
searchOptions: {
fuzzy: 0.1,
},
});
for (let index = 0; index < sortedPosts.allPostList.length; index++) {
const post = sortedPosts.allPostList[index];
const content = getPostFileContent(post.id);
miniSearch.add({
id: post.id,
title: post.frontMatter.title,
tags: post.frontMatter.tags,
subtitle: post.frontMatter.subtitle,
summary: post.frontMatter.summary,
content: content,
});
}
return miniSearch;
}
export const SearchIndex = makeSearchIndex();