diff --git a/.gitignore b/.gitignore index 50f28da5..b4f171df 100644 --- a/.gitignore +++ b/.gitignore @@ -3,6 +3,7 @@ node_modules/* .vercel .next .env* +tsconfig.tsbuildinfo # Generated public/robots.txt diff --git a/components/nextra/Flexsearch.tsx b/components/nextra/Flexsearch.tsx index 60d4d04a..1de05c0c 100644 --- a/components/nextra/Flexsearch.tsx +++ b/components/nextra/Flexsearch.tsx @@ -23,7 +23,7 @@ import cn from 'clsx'; // flexsearch types are incorrect, they were overwritten in tsconfig.json -import FlexSearch from 'flexsearch'; +import { Document } from 'flexsearch'; import { useRouter } from 'next/router'; import type { SearchData } from 'nextra'; import type { ReactElement, ReactNode } from 'react'; @@ -36,38 +36,30 @@ import { SearchResult } from './types'; // Diff: Inlined definitions export const DEFAULT_LOCALE = 'en-US'; -type SectionIndex = FlexSearch.Document< - { +type SectionIndex = { id: string; url: string; title: string; pageId: string; content: string; display?: string; - }, - ['title', 'content', 'url', 'display'] ->; + } -type PageIndex = FlexSearch.Document< - { +type PageIndex = { id: number; title: string; content: string; - }, - ['title'] ->; + } // Diff: Additional index for blog posts -type BlogIndex = FlexSearch.Document< +type BlogIndex = { id: number; title: string; content: string; url: string; summary: string; - }, - ['title', 'url', 'summary'] ->; + } type Result = { _page_rk: number; @@ -79,11 +71,13 @@ type Result = { // This can be global for better caching. const indexes: { - [locale: string]: [PageIndex, SectionIndex]; + // tuple is PageIndex, SectionIndex + [locale: string]: [Document, Document]; } = {}; // Diff: Index for blog posts -const blogIndex: BlogIndex = new FlexSearch.Document({ +// Associated type is BlogIndex +const blogIndex = new Document({ cache: 100, tokenize: 'forward', document: { @@ -134,7 +128,8 @@ const loadIndexesImpl = async ( // Diff: Load blog data const blogData = await loadBlogData(basePath); - const pageIndex: PageIndex = new FlexSearch.Document({ + // Associated type is PageIndex + const pageIndex = new Document({ cache: 100, tokenize: 'full', document: { @@ -149,7 +144,8 @@ const loadIndexesImpl = async ( }, }); - const sectionIndex: SectionIndex = new FlexSearch.Document({ + // Associated type is SectionIndex + const sectionIndex = new Document({ cache: 100, tokenize: 'full', document: { @@ -232,7 +228,7 @@ export function Flexsearch({ // Show the results for the top 5 pages const pageResults = - pageIndex.search(search, 5, { + pageIndex.search(search, 5, { enrich: true, suggest: true, })[0]?.result || []; @@ -247,7 +243,7 @@ export function Flexsearch({ // Show the top 5 results for each page const sectionResults = - sectionIndex.search(search, 5, { + sectionIndex.search(search, 5, { enrich: true, suggest: true, tag: `page_${result.id}`, @@ -324,7 +320,7 @@ export function Flexsearch({ })); const blogResults = - blogIndex.search(search, 5, { + blogIndex.search(search, 5, { enrich: true, suggest: true, })[0]?.result || []; diff --git a/package.json b/package.json index 81284eef..2f472c61 100644 --- a/package.json +++ b/package.json @@ -26,7 +26,7 @@ "class-variance-authority": "^0.7.0", "clsx": "^2.1.1", "escape-string-regexp": "^5.0.0", - "flexsearch": "^0.7.43", + "flexsearch": "^0.8.143", "js-yaml": "^4.1.0", "next": "^14.2.7", "next-sitemap": "^4.2.3", diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index ae49ccd0..bc99e5fa 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -45,8 +45,8 @@ importers: specifier: ^5.0.0 version: 5.0.0 flexsearch: - specifier: ^0.7.43 - version: 0.7.43 + specifier: ^0.8.143 + version: 0.8.158 js-yaml: specifier: ^4.1.0 version: 4.1.0 @@ -1919,6 +1919,9 @@ packages: flexsearch@0.7.43: resolution: {integrity: sha512-c5o/+Um8aqCSOXGcZoqZOm+NqtVwNsvVpWv6lfmSclU954O3wvQKxxK8zj74fPaSJbXpSLTs4PRhh+wnoCXnKg==} + flexsearch@0.8.158: + resolution: {integrity: sha512-UBOzX2rxIrhAeSSCesTI0qB2Q+75n66rofJx5ppZm5tjXV2P6BxOS3VHKsoSdJhIPg9IMzQl3qkVeSFyq3BUdw==} + focus-visible@5.2.0: resolution: {integrity: sha512-Rwix9pBtC1Nuy5wysTmKy+UjbDJpIfg8eHjw0rjZ1mX4GNLz1Bmd16uDpI3Gk1i70Fgcs8Csg2lPm8HULFg9DQ==} @@ -5220,6 +5223,8 @@ snapshots: flexsearch@0.7.43: {} + flexsearch@0.8.158: {} + focus-visible@5.2.0: {} foreground-child@3.2.1: diff --git a/tsconfig.json b/tsconfig.json index 6b08efde..51b727a3 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -15,7 +15,7 @@ "isolatedModules": true, "jsx": "preserve", "paths": { - "@/*": ["./*"], + "@/*": ["./*"] } }, "include": ["next-env.d.ts", "**/*.ts", "**/*.tsx"],