From 0eb56ac00c02eb0d0f61292332271a3b68c8693e Mon Sep 17 00:00:00 2001 From: zoolsher Date: Thu, 1 Aug 2024 23:25:24 +0800 Subject: [PATCH 1/7] chore: update flexsearch to 0.7.x --- packages/core/package.json | 1 - packages/theme-default/package.json | 2 +- .../Search/logic/providers/LocalProvider.ts | 96 ++++++++++++------- 3 files changed, 63 insertions(+), 36 deletions(-) diff --git a/packages/core/package.json b/packages/core/package.json index 96cd377c8..05c375f96 100644 --- a/packages/core/package.json +++ b/packages/core/package.json @@ -59,7 +59,6 @@ "body-scroll-lock": "4.0.0-beta.0", "copy-to-clipboard": "^3.3.3", "enhanced-resolve": "5.17.0", - "flexsearch": "0.6.32", "github-slugger": "^2.0.0", "hast-util-from-html": "^1.0.0", "hast-util-heading-rank": "^3.0.0", diff --git a/packages/theme-default/package.json b/packages/theme-default/package.json index 86f39f2e1..f95af8fc8 100644 --- a/packages/theme-default/package.json +++ b/packages/theme-default/package.json @@ -40,7 +40,7 @@ "@rspress/shared": "workspace:*", "body-scroll-lock": "4.0.0-beta.0", "copy-to-clipboard": "^3.3.3", - "flexsearch": "0.6.32", + "flexsearch": "0.7.43", "github-slugger": "^2.0.0", "hast-util-from-html": "^1.0.0", "html-to-text": "^9.0.3", diff --git a/packages/theme-default/src/components/Search/logic/providers/LocalProvider.ts b/packages/theme-default/src/components/Search/logic/providers/LocalProvider.ts index 5805afa89..f466998a0 100644 --- a/packages/theme-default/src/components/Search/logic/providers/LocalProvider.ts +++ b/packages/theme-default/src/components/Search/logic/providers/LocalProvider.ts @@ -3,14 +3,20 @@ * Powered by FlexSearch. https://github.com/nextapps-de/flexsearch */ -import type { CreateOptions, Index as SearchIndex } from 'flexsearch'; -import FlexSearch from 'flexsearch'; +import type { + Document, + EnrichedDocumentSearchResultSetUnit, + IndexOptionsForDocumentSearch, +} from 'flexsearch'; +import FlexSearchDocument from 'flexsearch/dist/module/document'; import searchIndexHash from 'virtual-search-index-hash'; import { type PageIndexInfo, SEARCH_INDEX_NAME } from '@rspress/shared'; import type { SearchOptions } from '../types'; import { LOCAL_INDEX, type Provider, type SearchQuery } from '../Provider'; import { normalizeTextCase } from '../util'; +type TFlexSearchDocumentWithType = Document; + interface PageIndexForFlexSearch extends PageIndexInfo { normalizedContent: string; headers: string; @@ -34,11 +40,11 @@ function tokenize(str: string, regex) { } export class LocalProvider implements Provider { - #index?: SearchIndex; + #index?: TFlexSearchDocumentWithType; - #cjkIndex?: SearchIndex; + #cjkIndex?: TFlexSearchDocumentWithType; - #cyrilicIndex?: SearchIndex; + #cyrilicIndex?: TFlexSearchDocumentWithType; async #getPages(lang: string, version: string): Promise { const searchIndexGroupID = `${version}###${lang}`; @@ -63,61 +69,83 @@ export class LocalProvider implements Provider { headers: page.toc.map(header => normalizeTextCase(header.text)).join(' '), normalizedTitle: normalizeTextCase(page.title), })); - const createOptions: CreateOptions = { - tokenize: 'full', - async: true, - doc: { - id: 'routePath', - field: ['normalizedTitle', 'headers', 'normalizedContent'], - }, - cache: 100, - split: /\W+/, - }; + const createOptions: IndexOptionsForDocumentSearch = + { + tokenize: 'full', + document: { + id: 'id', + store: true, + index: ['normalizedTitle', 'headers', 'normalizedContent'], + }, + cache: 100, + // charset: { + // split: /\W+/, + // }, + }; // Init Search Indexes // English Index - this.#index = FlexSearch.create(createOptions); + this.#index = new FlexSearchDocument(createOptions); // CJK: Chinese, Japanese, Korean - this.#cjkIndex = FlexSearch.create({ + this.#cjkIndex = new FlexSearchDocument({ ...createOptions, tokenize: (str: string) => tokenize(str, cjkRegex), }); // Cyrilic Index - this.#cyrilicIndex = FlexSearch.create({ + this.#cyrilicIndex = new FlexSearchDocument({ ...createOptions, tokenize: (str: string) => tokenize(str, cyrillicRegex), }); - this.#index.add(pagesForSearch); - this.#cjkIndex.add(pagesForSearch); - this.#cyrilicIndex.add(pagesForSearch); + for (const item of pagesForSearch) { + this.#index.add(item); + this.#cjkIndex.add(item); + this.#cyrilicIndex.add(item); + } } async search(query: SearchQuery) { const { keyword, limit } = query; - const searchParams = { - query: keyword, + + const options = { + enrich: true as const, limit, - field: ['normalizedTitle', 'headers', 'normalizedContent'], + index: ['normalizedTitle', 'headers', 'normalizedContent'], }; const searchResult = await Promise.all([ - this.#index?.search(searchParams), - this.#cjkIndex?.search(searchParams), - this.#cyrilicIndex.search(searchParams), + this.#index?.search(keyword, limit, options), + this.#cjkIndex?.search(keyword, limit, options), + this.#cyrilicIndex.search(keyword, limit, options), ]); - const flattenSearchResult = searchResult.flat(2).filter(Boolean); + const commbindSeachResult: PageIndexInfo[] = []; + const pushedId: Set = new Set(); + + function insertCommbindSearchResult( + resultFromOneSearchIndex: EnrichedDocumentSearchResultSetUnit[], + ) { + for (const item of resultFromOneSearchIndex) { + // item.field; // ignored + item.result.forEach(resultItem => { + // type of resultItem doesn't match with runtime + const id = resultItem.id as unknown as string; + if (pushedId.has(id)) { + return; + } + // mark the doc is in the searched results + pushedId.add(id); + commbindSeachResult.push(resultItem.doc); + }); + } + } - // There may be duplicate search results when there are multiple languages ​​in the search keyword - const uniqueSearchResult = Array.from( - new Set(flattenSearchResult.map(item => item.id)), - ).map(id => { - return flattenSearchResult.find(item => item.id === id); + searchResult.forEach(searchResultItem => { + insertCommbindSearchResult(searchResultItem); }); return [ { index: LOCAL_INDEX, - hits: uniqueSearchResult, + hits: commbindSeachResult, }, ]; } From 4cb515211540b05b73dc03932389fe0acd714679 Mon Sep 17 00:00:00 2001 From: zoolsher Date: Thu, 1 Aug 2024 23:45:55 +0800 Subject: [PATCH 2/7] chore: update pnpm-lock.yml --- .npmrc | 1 + packages/plugin-api-docgen/package.json | 2 +- packages/plugin-client-redirects/package.json | 2 +- packages/plugin-medium-zoom/package.json | 2 +- packages/plugin-playground/package.json | 2 +- packages/plugin-preview/package.json | 2 +- packages/plugin-rss/package.json | 2 +- packages/plugin-typedoc/package.json | 2 +- pnpm-lock.yaml | 25 ++++++++----------- 9 files changed, 19 insertions(+), 21 deletions(-) diff --git a/.npmrc b/.npmrc index 06e9635e4..037ad54a9 100644 --- a/.npmrc +++ b/.npmrc @@ -1,2 +1,3 @@ registry = 'https://registry.npmjs.org/' strict-peer-dependencies=false +link-workspace-packages=true diff --git a/packages/plugin-api-docgen/package.json b/packages/plugin-api-docgen/package.json index 84f4c11f6..c8a27ff98 100644 --- a/packages/plugin-api-docgen/package.json +++ b/packages/plugin-api-docgen/package.json @@ -45,7 +45,7 @@ "peerDependencies": { "react": ">=17.0.0", "react-router-dom": "^6.8.1", - "@rspress/core": "^1.0.2", + "@rspress/core": "workspace:*", "typescript": "^5.5.3" }, "peerDependenciesMeta": { diff --git a/packages/plugin-client-redirects/package.json b/packages/plugin-client-redirects/package.json index cfc19d4d7..236685f7d 100644 --- a/packages/plugin-client-redirects/package.json +++ b/packages/plugin-client-redirects/package.json @@ -34,7 +34,7 @@ "vitest": "0.34.1" }, "peerDependencies": { - "@rspress/runtime": "^1.0.2" + "@rspress/runtime": "workspace:*" }, "sideEffects": [ "*.css", diff --git a/packages/plugin-medium-zoom/package.json b/packages/plugin-medium-zoom/package.json index 6c18d8b59..cda5fc920 100644 --- a/packages/plugin-medium-zoom/package.json +++ b/packages/plugin-medium-zoom/package.json @@ -34,7 +34,7 @@ "vitest": "0.34.1" }, "peerDependencies": { - "@rspress/runtime": "^1.0.2" + "@rspress/runtime": "workspace:*" }, "sideEffects": [ "*.css", diff --git a/packages/plugin-playground/package.json b/packages/plugin-playground/package.json index f46bc1a94..8ead39c8a 100644 --- a/packages/plugin-playground/package.json +++ b/packages/plugin-playground/package.json @@ -59,7 +59,7 @@ "unist-util-visit": "^4.1.1" }, "peerDependencies": { - "@rspress/core": "^1.0.2", + "@rspress/core": "workspace:*", "react": ">=17.0.0", "react-router-dom": "^6.8.1" }, diff --git a/packages/plugin-preview/package.json b/packages/plugin-preview/package.json index cce3c564f..15885fc81 100644 --- a/packages/plugin-preview/package.json +++ b/packages/plugin-preview/package.json @@ -52,7 +52,7 @@ "peerDependencies": { "react": ">=17.0.0", "react-router-dom": "^6.8.1", - "@rspress/core": "^1.0.2" + "@rspress/core": "workspace:*" }, "files": [ "dist", diff --git a/packages/plugin-rss/package.json b/packages/plugin-rss/package.json index e31a8943f..71edebe70 100644 --- a/packages/plugin-rss/package.json +++ b/packages/plugin-rss/package.json @@ -43,7 +43,7 @@ }, "peerDependencies": { "react": ">=17.0.0", - "rspress": "^1.17.0" + "rspress": "workspace:*" }, "files": [ "dist", diff --git a/packages/plugin-typedoc/package.json b/packages/plugin-typedoc/package.json index 66fca5b41..17a7b91ff 100644 --- a/packages/plugin-typedoc/package.json +++ b/packages/plugin-typedoc/package.json @@ -32,7 +32,7 @@ "vitest": "0.34.1" }, "peerDependencies": { - "rspress": "^1.0.2" + "rspress": "workspace:*" }, "sideEffects": [ "*.css", diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index 77df94257..d74b6441b 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -525,9 +525,6 @@ importers: enhanced-resolve: specifier: 5.17.0 version: 5.17.0 - flexsearch: - specifier: 0.6.32 - version: 0.6.32 github-slugger: specifier: ^2.0.0 version: 2.0.0 @@ -785,7 +782,7 @@ importers: specifier: 2.56.2 version: 2.56.2 '@rspress/core': - specifier: ^1.0.2 + specifier: workspace:* version: link:../core documentation: specifier: 14.0.2 @@ -868,7 +865,7 @@ importers: packages/plugin-client-redirects: dependencies: '@rspress/runtime': - specifier: ^1.0.2 + specifier: workspace:* version: link:../runtime devDependencies: '@modern-js/tsconfig': @@ -949,7 +946,7 @@ importers: packages/plugin-medium-zoom: dependencies: '@rspress/runtime': - specifier: ^1.0.2 + specifier: workspace:* version: link:../runtime medium-zoom: specifier: 1.1.0 @@ -992,7 +989,7 @@ importers: specifier: ^0.2.0 version: 0.2.0 '@rspress/core': - specifier: ^1.0.2 + specifier: workspace:* version: link:../core '@rspress/shared': specifier: workspace:* @@ -1074,7 +1071,7 @@ importers: specifier: 1.0.0-alpha.9 version: 1.0.0-alpha.9(@babel/core@7.24.7)(@rsbuild/core@1.0.0-alpha.9)(solid-js@1.8.16) '@rspress/core': - specifier: ^1.0.2 + specifier: workspace:* version: link:../core '@rspress/shared': specifier: workspace:* @@ -1138,7 +1135,7 @@ importers: specifier: ^4.2.2 version: 4.2.2 rspress: - specifier: ^1.17.0 + specifier: workspace:* version: link:../cli devDependencies: '@rspress/runtime': @@ -1206,7 +1203,7 @@ importers: specifier: workspace:* version: link:../shared rspress: - specifier: ^1.0.2 + specifier: workspace:* version: link:../cli typedoc: specifier: 0.24.8 @@ -1350,8 +1347,8 @@ importers: specifier: ^3.3.3 version: 3.3.3 flexsearch: - specifier: 0.6.32 - version: 0.6.32 + specifier: 0.7.43 + version: 0.7.43 github-slugger: specifier: ^2.0.0 version: 2.0.0 @@ -5892,8 +5889,8 @@ packages: hasBin: true dev: true - /flexsearch@0.6.32: - resolution: {integrity: sha512-EF1BWkhwoeLtbIlDbY/vDSLBen/E5l/f1Vg7iX5CDymQCamcx1vhlc3tIZxIDplPjgi0jhG37c67idFbjg+v+Q==} + /flexsearch@0.7.43: + resolution: {integrity: sha512-c5o/+Um8aqCSOXGcZoqZOm+NqtVwNsvVpWv6lfmSclU954O3wvQKxxK8zj74fPaSJbXpSLTs4PRhh+wnoCXnKg==} dev: false /focus-lock@0.11.6: From 0acac2b8cc1a76c0de159f868a211e72d62906e3 Mon Sep 17 00:00:00 2001 From: zoolsher Date: Fri, 2 Aug 2024 10:33:08 +0800 Subject: [PATCH 3/7] Update packages/theme-default/src/components/Search/logic/providers/LocalProvider.ts Co-authored-by: neverland --- .../src/components/Search/logic/providers/LocalProvider.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/theme-default/src/components/Search/logic/providers/LocalProvider.ts b/packages/theme-default/src/components/Search/logic/providers/LocalProvider.ts index f466998a0..787d71aef 100644 --- a/packages/theme-default/src/components/Search/logic/providers/LocalProvider.ts +++ b/packages/theme-default/src/components/Search/logic/providers/LocalProvider.ts @@ -15,7 +15,7 @@ import type { SearchOptions } from '../types'; import { LOCAL_INDEX, type Provider, type SearchQuery } from '../Provider'; import { normalizeTextCase } from '../util'; -type TFlexSearchDocumentWithType = Document; +type FlexSearchDocumentWithType = Document; interface PageIndexForFlexSearch extends PageIndexInfo { normalizedContent: string; From 21adadff16cffe35b37b5d845afcf82203e72385 Mon Sep 17 00:00:00 2001 From: zoolsher Date: Fri, 2 Aug 2024 10:37:01 +0800 Subject: [PATCH 4/7] chore: rename symbol from TFlexSearchDocumentWithType to FlexSearchDocumentWithType --- .../src/components/Search/logic/providers/LocalProvider.ts | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/packages/theme-default/src/components/Search/logic/providers/LocalProvider.ts b/packages/theme-default/src/components/Search/logic/providers/LocalProvider.ts index 787d71aef..76e1bfffe 100644 --- a/packages/theme-default/src/components/Search/logic/providers/LocalProvider.ts +++ b/packages/theme-default/src/components/Search/logic/providers/LocalProvider.ts @@ -40,11 +40,11 @@ function tokenize(str: string, regex) { } export class LocalProvider implements Provider { - #index?: TFlexSearchDocumentWithType; + #index?: FlexSearchDocumentWithType; - #cjkIndex?: TFlexSearchDocumentWithType; + #cjkIndex?: FlexSearchDocumentWithType; - #cyrilicIndex?: TFlexSearchDocumentWithType; + #cyrilicIndex?: FlexSearchDocumentWithType; async #getPages(lang: string, version: string): Promise { const searchIndexGroupID = `${version}###${lang}`; From ed8670adaae934360ef2f26185596bcb64bc871e Mon Sep 17 00:00:00 2001 From: zoolsher Date: Fri, 2 Aug 2024 14:36:06 +0800 Subject: [PATCH 5/7] chore: use workspace:^ instead of workspace:* --- packages/plugin-api-docgen/package.json | 2 +- packages/plugin-client-redirects/package.json | 2 +- packages/plugin-medium-zoom/package.json | 2 +- packages/plugin-playground/package.json | 2 +- packages/plugin-preview/package.json | 2 +- packages/plugin-rss/package.json | 2 +- pnpm-lock.yaml | 12 ++++++------ 7 files changed, 12 insertions(+), 12 deletions(-) diff --git a/packages/plugin-api-docgen/package.json b/packages/plugin-api-docgen/package.json index c8a27ff98..28511b7f6 100644 --- a/packages/plugin-api-docgen/package.json +++ b/packages/plugin-api-docgen/package.json @@ -45,7 +45,7 @@ "peerDependencies": { "react": ">=17.0.0", "react-router-dom": "^6.8.1", - "@rspress/core": "workspace:*", + "@rspress/core": "workspace:^1.0.2", "typescript": "^5.5.3" }, "peerDependenciesMeta": { diff --git a/packages/plugin-client-redirects/package.json b/packages/plugin-client-redirects/package.json index 236685f7d..6292c26cf 100644 --- a/packages/plugin-client-redirects/package.json +++ b/packages/plugin-client-redirects/package.json @@ -34,7 +34,7 @@ "vitest": "0.34.1" }, "peerDependencies": { - "@rspress/runtime": "workspace:*" + "@rspress/runtime": "workspace:^1.0.2" }, "sideEffects": [ "*.css", diff --git a/packages/plugin-medium-zoom/package.json b/packages/plugin-medium-zoom/package.json index cda5fc920..73f5deed5 100644 --- a/packages/plugin-medium-zoom/package.json +++ b/packages/plugin-medium-zoom/package.json @@ -34,7 +34,7 @@ "vitest": "0.34.1" }, "peerDependencies": { - "@rspress/runtime": "workspace:*" + "@rspress/runtime": "workspace:^1.0.2" }, "sideEffects": [ "*.css", diff --git a/packages/plugin-playground/package.json b/packages/plugin-playground/package.json index 8ead39c8a..8cbd04004 100644 --- a/packages/plugin-playground/package.json +++ b/packages/plugin-playground/package.json @@ -59,7 +59,7 @@ "unist-util-visit": "^4.1.1" }, "peerDependencies": { - "@rspress/core": "workspace:*", + "@rspress/core": "workspace:^1.0.2", "react": ">=17.0.0", "react-router-dom": "^6.8.1" }, diff --git a/packages/plugin-preview/package.json b/packages/plugin-preview/package.json index 15885fc81..f56bb58e3 100644 --- a/packages/plugin-preview/package.json +++ b/packages/plugin-preview/package.json @@ -52,7 +52,7 @@ "peerDependencies": { "react": ">=17.0.0", "react-router-dom": "^6.8.1", - "@rspress/core": "workspace:*" + "@rspress/core": "workspace:^1.0.2" }, "files": [ "dist", diff --git a/packages/plugin-rss/package.json b/packages/plugin-rss/package.json index 71edebe70..bbec47f2e 100644 --- a/packages/plugin-rss/package.json +++ b/packages/plugin-rss/package.json @@ -43,7 +43,7 @@ }, "peerDependencies": { "react": ">=17.0.0", - "rspress": "workspace:*" + "rspress": "workspace:^1.0.2" }, "files": [ "dist", diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index d74b6441b..6f932d8d6 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -782,7 +782,7 @@ importers: specifier: 2.56.2 version: 2.56.2 '@rspress/core': - specifier: workspace:* + specifier: workspace:^1.0.2 version: link:../core documentation: specifier: 14.0.2 @@ -865,7 +865,7 @@ importers: packages/plugin-client-redirects: dependencies: '@rspress/runtime': - specifier: workspace:* + specifier: workspace:^1.0.2 version: link:../runtime devDependencies: '@modern-js/tsconfig': @@ -946,7 +946,7 @@ importers: packages/plugin-medium-zoom: dependencies: '@rspress/runtime': - specifier: workspace:* + specifier: workspace:^1.0.2 version: link:../runtime medium-zoom: specifier: 1.1.0 @@ -989,7 +989,7 @@ importers: specifier: ^0.2.0 version: 0.2.0 '@rspress/core': - specifier: workspace:* + specifier: workspace:^1.0.2 version: link:../core '@rspress/shared': specifier: workspace:* @@ -1071,7 +1071,7 @@ importers: specifier: 1.0.0-alpha.9 version: 1.0.0-alpha.9(@babel/core@7.24.7)(@rsbuild/core@1.0.0-alpha.9)(solid-js@1.8.16) '@rspress/core': - specifier: workspace:* + specifier: workspace:^1.0.2 version: link:../core '@rspress/shared': specifier: workspace:* @@ -1135,7 +1135,7 @@ importers: specifier: ^4.2.2 version: 4.2.2 rspress: - specifier: workspace:* + specifier: workspace:^1.0.2 version: link:../cli devDependencies: '@rspress/runtime': From 96452273090c397cd01f7ef81368a7a2955248f7 Mon Sep 17 00:00:00 2001 From: zoolsher Date: Fri, 2 Aug 2024 14:37:19 +0800 Subject: [PATCH 6/7] chore: use workspace:^ instead of workspace:* --- packages/plugin-typedoc/package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/plugin-typedoc/package.json b/packages/plugin-typedoc/package.json index 17a7b91ff..81d8dbe59 100644 --- a/packages/plugin-typedoc/package.json +++ b/packages/plugin-typedoc/package.json @@ -32,7 +32,7 @@ "vitest": "0.34.1" }, "peerDependencies": { - "rspress": "workspace:*" + "rspress": "workspace:^1.0.2" }, "sideEffects": [ "*.css", From f7ecfd5aec2aa0348f9c297bd42696892d8850d0 Mon Sep 17 00:00:00 2001 From: zoolsher Date: Fri, 2 Aug 2024 14:38:29 +0800 Subject: [PATCH 7/7] chore: update pnpm lock --- pnpm-lock.yaml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index 6f932d8d6..1e55efbcf 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -1203,7 +1203,7 @@ importers: specifier: workspace:* version: link:../shared rspress: - specifier: workspace:* + specifier: workspace:^1.0.2 version: link:../cli typedoc: specifier: 0.24.8