Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ node_modules/*
.vercel
.next
.env*
tsconfig.tsbuildinfo

# Generated
public/robots.txt
Expand Down
40 changes: 18 additions & 22 deletions components/nextra/Flexsearch.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This was a change that was made to the types in the intervening versions

import { useRouter } from 'next/router';
import type { SearchData } from 'nextra';
import type { ReactElement, ReactNode } from 'react';
Expand All @@ -36,38 +36,30 @@ import { SearchResult } from './types';
// Diff: Inlined definitions
export const DEFAULT_LOCALE = 'en-US';

type SectionIndex = FlexSearch.Document<
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These are no longer generic, so I stripped them but left the types in place in case they reinstate this behavior later

{
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;
Expand All @@ -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];
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

And then annotated their usage wherever I stripped the types

} = {};

// Diff: Index for blog posts
const blogIndex: BlogIndex = new FlexSearch.Document({
// Associated type is BlogIndex
const blogIndex = new Document({
cache: 100,
tokenize: 'forward',
document: {
Expand Down Expand Up @@ -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: {
Expand All @@ -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: {
Expand Down Expand Up @@ -232,7 +228,7 @@ export function Flexsearch({

// Show the results for the top 5 pages
const pageResults =
pageIndex.search<true>(search, 5, {
pageIndex.search(search, 5, {
enrich: true,
suggest: true,
})[0]?.result || [];
Expand All @@ -247,7 +243,7 @@ export function Flexsearch({

// Show the top 5 results for each page
const sectionResults =
sectionIndex.search<true>(search, 5, {
sectionIndex.search(search, 5, {
enrich: true,
suggest: true,
tag: `page_${result.id}`,
Expand Down Expand Up @@ -324,7 +320,7 @@ export function Flexsearch({
}));

const blogResults =
blogIndex.search<true>(search, 5, {
blogIndex.search(search, 5, {
enrich: true,
suggest: true,
})[0]?.result || [];
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
9 changes: 7 additions & 2 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
"isolatedModules": true,
"jsx": "preserve",
"paths": {
"@/*": ["./*"],
"@/*": ["./*"]
}
},
"include": ["next-env.d.ts", "**/*.ts", "**/*.tsx"],
Expand Down