diff --git a/app/models/article.server.ts b/app/models/article.server.ts index e568b41..33ac37f 100644 --- a/app/models/article.server.ts +++ b/app/models/article.server.ts @@ -3,6 +3,7 @@ import type { Database } from "~/services/db.server"; import type { UUID } from "~/utils/uuid"; import { and, eq } from "drizzle-orm"; +import Fuse from "fuse.js"; import { Post } from "~/models/post.server"; import { Markdown } from "~/modules/md.server"; @@ -101,20 +102,13 @@ export class Article extends Post { static async search(services: Services, query: string) { let articles = await Article.list(services); - let words = query - .trim() - .toLowerCase() - .split(/\s+/) - .filter((word) => word.length > 1); + query = query.trim().toLowerCase(); - for (let word of words) { - articles = articles.filter((item) => { - let title = item.title.toLowerCase(); - return title.includes(word); - }); - } + let fuse = new Fuse(articles, { + keys: ["title", "content"], + }); - return articles; + return fuse.search(query).map((result) => result.item); } static async findById(services: Services, id: UUID) { diff --git a/app/models/glossary.server.ts b/app/models/glossary.server.ts index f7bddcd..c66624d 100644 --- a/app/models/glossary.server.ts +++ b/app/models/glossary.server.ts @@ -2,6 +2,8 @@ import type { BaseMeta, PostAttributes } from "~/models/post.server"; import type { Database, Tables } from "~/services/db.server"; import type { UUID } from "~/utils/uuid"; +import Fuse from "fuse.js"; + import { Post } from "~/models/post.server"; interface GlossaryMeta extends BaseMeta { @@ -86,19 +88,12 @@ export class Glossary extends Post { static async search(services: Services, query: string) { let glossary = await Glossary.list(services); - let words = query - .trim() - .toLowerCase() - .split(/\s+/) - .filter((word) => word.length > 1); + query = query.trim().toLowerCase(); - for (let word of words) { - glossary = glossary.filter((item) => { - let term = item.term.toLowerCase(); - return term.includes(word); - }); - } + let fuse = new Fuse(glossary, { + keys: ["term", "title", "definition"], + }); - return glossary; + return fuse.search(query).map((result) => result.item); } } diff --git a/app/models/like.server.ts b/app/models/like.server.ts index 4144d9f..9a29e43 100644 --- a/app/models/like.server.ts +++ b/app/models/like.server.ts @@ -1,6 +1,8 @@ import type { BaseMeta, PostAttributes } from "~/models/post.server"; import type { Database, Tables } from "~/services/db.server"; +import Fuse from "fuse.js"; + import { Post } from "~/models/post.server"; interface Services { @@ -50,20 +52,13 @@ export class Like extends Post { static async search({ db }: Services, query: string) { let likes = await Like.list({ db }); - let words = query - .trim() - .toLowerCase() - .split(/\s+/) - .filter((word) => word.length > 1); + query = query.trim().toLowerCase(); - for (let word of words) { - likes = likes.filter((item) => { - let title = item.title.toLowerCase(); - return title.includes(word); - }); - } + let fuse = new Fuse(likes, { + keys: ["title"], + }); - return likes; + return fuse.search(query).map((result) => result.item); } static override async show({ db }: Services, id: Tables.SelectPost["id"]) { diff --git a/app/models/tutorial.server.ts b/app/models/tutorial.server.ts index 29f2e6c..8534cba 100644 --- a/app/models/tutorial.server.ts +++ b/app/models/tutorial.server.ts @@ -3,6 +3,7 @@ import type { Database } from "~/services/db.server"; import type { UUID } from "~/utils/uuid"; import { and, eq } from "drizzle-orm"; +import Fuse from "fuse.js"; import * as semver from "semver"; import { Markdown } from "~/modules/md.server"; @@ -157,14 +158,11 @@ export class Tutorial extends Post { }); } - for (let word of query.trim()) { - tutorials = tutorials.filter((item) => { - let title = item.title.toLowerCase(); - return title.includes(word); - }); - } + let fuse = new Fuse(tutorials, { + keys: ["title", "content"], + }); - return tutorials; + return fuse.search(query).map((result) => result.item); } static async findById(services: Services, id: UUID) { diff --git a/bun.lockb b/bun.lockb index 55df9b6..8d12a63 100755 Binary files a/bun.lockb and b/bun.lockb differ diff --git a/package.json b/package.json index 3b393d7..816603f 100644 --- a/package.json +++ b/package.json @@ -59,6 +59,7 @@ "dotenv": "^16.4.1", "drizzle-orm": "^0.29.1", "front-matter": "^4.0.2", + "fuse.js": "^7.0.0", "htmlparser2": "^9.0.0", "i18next": "^23.7.19", "i18next-browser-languagedetector": "^7.1.0",