From 33135529c277431db78ed374eb83de23667ecc38 Mon Sep 17 00:00:00 2001 From: weareoutman Date: Fri, 23 Oct 2020 09:49:02 +0800 Subject: [PATCH] feat: add a plugin option of `searchResultContextMaxLength` --- README.md | 27 ++++++++++--------- src/client/theme/SearchBar/SearchBar.tsx | 3 +-- .../theme/SearchBar/fetchIndexes.spec.ts | 9 ++----- src/client/theme/SearchBar/fetchIndexes.ts | 2 +- src/client/theme/SearchBar/index.ts | 2 +- src/client/utils/SuggestionTemplate.spec.ts | 1 + .../utils/__mocks__/proxiedGenerated.ts | 3 +++ src/client/utils/highlightStemmed.spec.ts | 4 ++- src/client/utils/highlightStemmed.ts | 3 ++- src/client/utils/proxiedGenerated.ts | 1 + src/declarations.ts | 1 + src/server/utils/generate.spec.ts | 11 +++++++- src/server/utils/generate.ts | 7 +++-- src/server/utils/processPluginOptions.spec.ts | 4 +++ src/server/utils/processPluginOptions.ts | 1 + src/shared/interfaces.ts | 1 + 16 files changed, 51 insertions(+), 29 deletions(-) create mode 100644 src/client/utils/__mocks__/proxiedGenerated.ts create mode 100644 src/client/utils/proxiedGenerated.ts diff --git a/README.md b/README.md index 860ba38d..ebe43353 100644 --- a/README.md +++ b/README.md @@ -64,19 +64,20 @@ yarn add nodejieba ## Plugin Options -| Name | Type | Default | Description | -| --------------------------- | ------------------ | --------- | ------------------------------------------------------------------------------------------------------------------------------ | -| indexDocs | boolean | `true` | Whether to index docs. | -| indexBlog | boolean | `true` | Whether to index blog. | -| indexPages | boolean | `false` | Whether to index pages. | -| docsRouteBasePath | string | `"/docs"` | Base route path of docs. Slash at beginning is not required. | -| blogRouteBasePath | string | `"/blog"` | Base route path of blog. Slash at beginning is not required. | -| language | string \| string[] | `"en"` | All [lunr-languages](https://github.com/MihaiValentin/lunr-languages) supported languages, + `zh` 🔥. | -| hashed | boolean | `false` | Whether to add a hashed query when fetching index (based on the content hash of all `*.md`) | -| docsDir | string | `"docs"` | The dir of docs to get the content hash, it's relative to the dir of your project. | -| blogDir | string | `"blog"` | Just like the `docsDir` but applied to blog. | -| removeDefaultStopWordFilter | boolean | `false` | Sometimes people (E.g., us) want to keep the English stop words as indexed, since they maybe are relevant in programming docs. | -| searchResultLimits | number | `8` | Limit the search results. | +| Name | Type | Default | Description | +| ---------------------------- | ------------------ | --------- | ------------------------------------------------------------------------------------------------------------------------------ | +| indexDocs | boolean | `true` | Whether to index docs. | +| indexBlog | boolean | `true` | Whether to index blog. | +| indexPages | boolean | `false` | Whether to index pages. | +| docsRouteBasePath | string | `"/docs"` | Base route path of docs. Slash at beginning is not required. | +| blogRouteBasePath | string | `"/blog"` | Base route path of blog. Slash at beginning is not required. | +| language | string \| string[] | `"en"` | All [lunr-languages](https://github.com/MihaiValentin/lunr-languages) supported languages, + `zh` 🔥. | +| hashed | boolean | `false` | Whether to add a hashed query when fetching index (based on the content hash of all `*.md`) | +| docsDir | string | `"docs"` | The dir of docs to get the content hash, it's relative to the dir of your project. | +| blogDir | string | `"blog"` | Just like the `docsDir` but applied to blog. | +| removeDefaultStopWordFilter | boolean | `false` | Sometimes people (E.g., us) want to keep the English stop words as indexed, since they maybe are relevant in programming docs. | +| searchResultLimits | number | `8` | Limit the search results. | +| searchResultContextMaxLength | number | `50` | Set the max length of characters of each search result to show. results. | ## Custom Styles diff --git a/src/client/theme/SearchBar/SearchBar.tsx b/src/client/theme/SearchBar/SearchBar.tsx index 26653dd5..c6a9e560 100644 --- a/src/client/theme/SearchBar/SearchBar.tsx +++ b/src/client/theme/SearchBar/SearchBar.tsx @@ -8,8 +8,7 @@ import { EmptyTemplate } from "../../utils/EmptyTemplate.js"; import { SearchResult } from "../../../shared/interfaces"; // This file is auto generated while building. -import { searchResultLimits } from "../../../../generated.js"; - +import { searchResultLimits } from "../../utils/proxiedGenerated"; import "./SearchBar.css"; async function fetchAutoCompleteJS(): Promise { diff --git a/src/client/theme/SearchBar/fetchIndexes.spec.ts b/src/client/theme/SearchBar/fetchIndexes.spec.ts index 00cd6945..fa16aa47 100644 --- a/src/client/theme/SearchBar/fetchIndexes.spec.ts +++ b/src/client/theme/SearchBar/fetchIndexes.spec.ts @@ -2,13 +2,8 @@ import lunr from "lunr"; import { fetchIndexes } from "./fetchIndexes"; jest.mock("lunr"); -jest.mock( - "../../../../generated.js", - () => ({ - indexHash: "abc", - }), - { virtual: true } -); +jest.mock("../../utils/proxiedGenerated"); + const mockLunrIndexLoad = (jest.spyOn( lunr.Index, "load" diff --git a/src/client/theme/SearchBar/fetchIndexes.ts b/src/client/theme/SearchBar/fetchIndexes.ts index 8eff85a6..8a2b88b6 100644 --- a/src/client/theme/SearchBar/fetchIndexes.ts +++ b/src/client/theme/SearchBar/fetchIndexes.ts @@ -6,7 +6,7 @@ import { } from "../../../shared/interfaces"; // This file is auto generated while building. -import { indexHash } from "../../../../generated.js"; +import { indexHash } from "../../utils/proxiedGenerated"; interface SerializedIndex { documents: SearchDocument[]; diff --git a/src/client/theme/SearchBar/index.ts b/src/client/theme/SearchBar/index.ts index afc5796c..021e67e6 100644 --- a/src/client/theme/SearchBar/index.ts +++ b/src/client/theme/SearchBar/index.ts @@ -1,4 +1,4 @@ -import "../../../../generated.js"; +import "../../utils/proxiedGenerated"; import SearchBar from "./SearchBar"; export default SearchBar; diff --git a/src/client/utils/SuggestionTemplate.spec.ts b/src/client/utils/SuggestionTemplate.spec.ts index 9316ef44..9334c004 100644 --- a/src/client/utils/SuggestionTemplate.spec.ts +++ b/src/client/utils/SuggestionTemplate.spec.ts @@ -1,6 +1,7 @@ import { SuggestionTemplate } from "./SuggestionTemplate"; jest.mock("./icons"); +jest.mock("./proxiedGenerated"); describe("SuggestionTemplate", () => { test("page title", () => { diff --git a/src/client/utils/__mocks__/proxiedGenerated.ts b/src/client/utils/__mocks__/proxiedGenerated.ts new file mode 100644 index 00000000..e2dac823 --- /dev/null +++ b/src/client/utils/__mocks__/proxiedGenerated.ts @@ -0,0 +1,3 @@ +export const indexHash = "abc"; +export const searchResultLimits = 8; +export const searchResultContextMaxLength = 50; diff --git a/src/client/utils/highlightStemmed.spec.ts b/src/client/utils/highlightStemmed.spec.ts index 6e006fb9..2f6e4be8 100644 --- a/src/client/utils/highlightStemmed.spec.ts +++ b/src/client/utils/highlightStemmed.spec.ts @@ -5,6 +5,8 @@ import { } from "../../shared/interfaces"; import { highlightStemmed, splitIntoChunks } from "./highlightStemmed"; +jest.mock("./proxiedGenerated"); + describe("highlightStemmed", () => { test.each<[string, MetadataPosition[], string[], number | undefined, string]>( [ @@ -158,7 +160,7 @@ describe("splitIntoChunks", () => { ])( "splitIntoChunks('%s', %j, %j, 0, 0) should return %j", (text, positions, tokens, chunks, chunkIndex) => { - const chunkIndexRef: ChunkIndexRef = {}; + const chunkIndexRef = {} as ChunkIndexRef; expect( splitIntoChunks(text, positions, tokens, 0, 0, chunkIndexRef) ).toEqual(chunks); diff --git a/src/client/utils/highlightStemmed.ts b/src/client/utils/highlightStemmed.ts index 526c1378..7312622e 100644 --- a/src/client/utils/highlightStemmed.ts +++ b/src/client/utils/highlightStemmed.ts @@ -6,12 +6,13 @@ import { import { escapeHtml } from "./escapeHtml"; import { highlight } from "./highlight"; import { looseTokenize } from "./looseTokenize"; +import { searchResultContextMaxLength } from "./proxiedGenerated"; export function highlightStemmed( content: string, positions: MetadataPosition[], tokens: string[], - maxLength = 50 + maxLength = searchResultContextMaxLength ): string { const chunkIndexRef: ChunkIndexRef = { chunkIndex: -1, diff --git a/src/client/utils/proxiedGenerated.ts b/src/client/utils/proxiedGenerated.ts new file mode 100644 index 00000000..16a267c4 --- /dev/null +++ b/src/client/utils/proxiedGenerated.ts @@ -0,0 +1 @@ +export * from "../../../generated.js"; diff --git a/src/declarations.ts b/src/declarations.ts index 6415b4f7..d952c3f7 100644 --- a/src/declarations.ts +++ b/src/declarations.ts @@ -20,4 +20,5 @@ declare module "autocomplete.js" { declare module "*/generated.js" { export const indexHash: string | undefined; export const searchResultLimits: number; + export const searchResultContextMaxLength: number; } diff --git a/src/server/utils/generate.spec.ts b/src/server/utils/generate.spec.ts index 934ecc9b..fa83d676 100644 --- a/src/server/utils/generate.spec.ts +++ b/src/server/utils/generate.spec.ts @@ -21,6 +21,7 @@ describe("generate", () => { 'import lunr from "lunr";', 'export const indexHash = "abc";', "export const searchResultLimits = 8;", + "export const searchResultContextMaxLength = 50;", ], ], [ @@ -31,6 +32,7 @@ describe("generate", () => { 'require("./client/shared/lunrLanguageZh").lunrLanguageZh(lunr);', 'export const indexHash = "abc";', "export const searchResultLimits = 8;", + "export const searchResultContextMaxLength = 50;", ], ], [ @@ -41,6 +43,7 @@ describe("generate", () => { 'require("lunr-languages/lunr.es")(lunr);', 'export const indexHash = "abc";', "export const searchResultLimits = 8;", + "export const searchResultContextMaxLength = 50;", ], ], [ @@ -52,6 +55,7 @@ describe("generate", () => { 'require("lunr-languages/lunr.multi")(lunr);', 'export const indexHash = "abc";', "export const searchResultLimits = 8;", + "export const searchResultContextMaxLength = 50;", ], ], [ @@ -64,10 +68,15 @@ describe("generate", () => { 'require("lunr-languages/lunr.multi")(lunr);', 'export const indexHash = "abc";', "export const searchResultLimits = 8;", + "export const searchResultContextMaxLength = 50;", ], ], ])("generate({ language: %j }) should work", (language, contents) => { - generate({ language, searchResultLimits: 8 } as ProcessedPluginOptions); + generate({ + language, + searchResultLimits: 8, + searchResultContextMaxLength: 50, + } as ProcessedPluginOptions); expect(mockWriteFileSync).toBeCalledWith( expect.stringContaining("generated.js"), contents.join("\n") diff --git a/src/server/utils/generate.ts b/src/server/utils/generate.ts index ef13b831..dd9f64a6 100644 --- a/src/server/utils/generate.ts +++ b/src/server/utils/generate.ts @@ -4,7 +4,7 @@ import { ProcessedPluginOptions } from "../../shared/interfaces"; import { getIndexHash } from "./getIndexHash"; export function generate(config: ProcessedPluginOptions): void { - const { language, searchResultLimits } = config; + const { language, searchResultLimits, searchResultContextMaxLength } = config; const indexHash = getIndexHash(config); const contents: string[] = ['import lunr from "lunr";']; if (language.length > 1 || language.some((item) => item !== "en")) { @@ -25,7 +25,10 @@ export function generate(config: ProcessedPluginOptions): void { } contents.push(`export const indexHash = ${JSON.stringify(indexHash)};`); contents.push( - `export const searchResultLimits = ${JSON.stringify(searchResultLimits)};` + `export const searchResultLimits = ${JSON.stringify(searchResultLimits)};`, + `export const searchResultContextMaxLength = ${JSON.stringify( + searchResultContextMaxLength + )};` ); fs.writeFileSync( diff --git a/src/server/utils/processPluginOptions.spec.ts b/src/server/utils/processPluginOptions.spec.ts index af818b3c..8757af33 100644 --- a/src/server/utils/processPluginOptions.spec.ts +++ b/src/server/utils/processPluginOptions.spec.ts @@ -19,6 +19,7 @@ describe("processPluginOptions", () => { language: ["en"], removeDefaultStopWordFilter: false, searchResultLimits: 8, + searchResultContextMaxLength: 50, }, ], [ @@ -35,6 +36,7 @@ describe("processPluginOptions", () => { language: ["en", "zh"], removeDefaultStopWordFilter: false, searchResultLimits: 8, + searchResultContextMaxLength: 50, }, ], [ @@ -42,6 +44,7 @@ describe("processPluginOptions", () => { docsDir: "src/docs", blogDir: "src/blog", searchResultLimits: 5, + searchResultContextMaxLength: 30, }, { blogRouteBasePath: "blog", @@ -55,6 +58,7 @@ describe("processPluginOptions", () => { language: ["en"], removeDefaultStopWordFilter: false, searchResultLimits: 5, + searchResultContextMaxLength: 30, }, ], ])("processPluginOptions(...) should work", (options, config) => { diff --git a/src/server/utils/processPluginOptions.ts b/src/server/utils/processPluginOptions.ts index 6867ae6f..aac226fb 100644 --- a/src/server/utils/processPluginOptions.ts +++ b/src/server/utils/processPluginOptions.ts @@ -18,6 +18,7 @@ export function processPluginOptions( blogDir: "blog", removeDefaultStopWordFilter: false, searchResultLimits: 8, + searchResultContextMaxLength: 50, }, options ) as ProcessedPluginOptions; diff --git a/src/shared/interfaces.ts b/src/shared/interfaces.ts index 8594db75..8278d29d 100644 --- a/src/shared/interfaces.ts +++ b/src/shared/interfaces.ts @@ -112,6 +112,7 @@ export interface PluginOptions { blogDir?: string; removeDefaultStopWordFilter?: boolean; searchResultLimits?: number; + searchResultContextMaxLength?: number; } export type ProcessedPluginOptions = Required & {